1. The Shell sort (invented by Dr. Donald Shell) is a variation of the bubble/exchange sort. Instead of comparing adjace
Posted: Fri Jul 08, 2022 6:16 am
This is my solution.
but i need someone to check it once for errors.
Also, i need part 3 of the problem. Thanks in advance.
1. The Shell sort (invented by Dr. Donald Shell) is a variation of the bubble/exchange sort. Instead of comparing adjacent values, the Shell sort adapts the partitioning concept from the binary search to determine a "gap" across which values are compared before any swap takes place. In the first pass, the gap is half the size of the array. For each subsequent pass, the gap size is cut in half. For the final pass(es), the gap size is 1, so it would be the same as a bubble sort. The passes continue until no swaps occur. Below is the same set of values as per the bubble sort example in Chapter 18 (p. 667), showing the first pass of the Shell sort: 9 6 9 12 3 1 7 968 12 3 1 Aume 968 12 3 17 9 3 8 12 6 1 7 93 1 12 68 7 7 6 1 12 93 8 7 The pseudo-code for the Shell sort is as follows: -size of array is 7, so gap would be 3 -9 and 12 are already in order, so no swap -6 and 3 are not in order, so swap -8 and 1 are not in order, so swap -12 and 7 are not in order, so swap end of pass 1
gapaize / 2 do until gap <= 0 awapflag = true do until swapflag is false awapflag = false for 0 to size gap if num[ยป] > num [a+ gapl end-if end-for gap=gap/2 end-do end-do awap num[s] with num(s+ gap] awapflag = true Modify Sorting.java to include a shellsort method that implements the above algorithm. Include an output of the array any time a swap occurs to demonstrate that your code works correctly. For the driver, create an Integer array using an initializer list to reproduce the above example and two additional random sets of 10 and 20 integers. 2. The bubble sort algorithm shown in Chapter 18 is less efficient than it could be. If a pass is made through the list without exchanging any elements, the list is sorted and there is no reason to continue. Create a copy of the bubbleSort method called bubblesort2 that implements this algorithm so that is will stop as soon as it recognizes that the list is sorted. Do not use a break statement! Include outputs of the array for both sorts for each pass through the array so you can demonstrate that the code is working correctly. The driver should test both methods with a random set of 10 integers and an already sorted set of 10 integers,
Hint: You have to introduce a swapflag that is set true if a swap occurs on a given pass. Replace the outer "for loop" with a "while loop" that tests the swapflag and takes care of "index counting within the loop. Hint: Remember that arrays are passed by reference in Java. Use Arrays.copyor () or a similar method to test each sort method on the exact same data content in separate arrays. 3. Modify the shellsort, bubbleSort, and bubbleSort2 methods from above by adding code to each to tally the total number of comparisons made between the elements being sorted (i.e., just the compareTo () calls; ignore relational operators between indexes). Also tally and report the number of swaps that occur. Determine and report the total execution time of each algorithm, Comment out the outputs of each swap or pass for this final step. Execute each of these sort algorithms against the same list, recording information for the total number of comparisons and total execution time. The driver should construct lists of size 10, 100, and 1000-both in random and already in sorted order. Use the Integer wrapper class for the array type. Use a spreadsheet to present the test cases you have prepared, along with the comparisons, swaps, and execution time. Describe how the data obtained relates to the theoretical discussion of algorithm efficiency presented in the chapter. Hint: Remember that arrays are passed by reference in Java. Use Arrays.copyor () or a similar method to test each sort method on the exact same data content in separate arrays.
public class Sorting static void bubbleSort(int[] arr) { //sorting array elements using bubble sort { { ( } { int n- int temp = 0; for(int i=0; i<n; i++) //Printing the sorted array System.out.println("Array after bubble sort: "); for(int i=0;i<arr.length; i++){ System.out.println(arr); arr.length; for(int j-1; j < (n-i); j++) if(arr[j-1]> arr[j]) //swap elements temp = arr[j-1]; arr[j-1] arr[j]; arr[j] = temp; }}} do{ static void shellSort(int a[]) int gap-num.length/2, temp; boolean swapflag; do{ swapflag-true; swapflag-false; for(int s-0;s<num.length-gap;s++){ if(num[s]>num[s+gap]){ temp-num[s]; num[s]-num[s+gap]; num[s+gap]-temp; swapflag-true; for(int t-0;t<num.length; t++){ Sysytem.out.print(num[t]+"") I
swapflag-true; for(int t-0; t<num.length;t++){ Sysytem.out.print(num[t]+"") System.out.println(); while(swapflag); gap-gap/2; } }}} } while (gap>0); }}} public static void bubbleSort2(int a[]) int k-0; for(int i=0;i<a.length(); i++) { { if(a>a[i+1] { if(k--0) { for(int i=0; i < a.length; i++) { { { k++; } } for(int j-1; j <(a.length)-1; j++) if(a[j-1]> a[j]) //swap elements int temp else{ a[i-1]; a[j-1] - a[j]; //Printing the sorted array System.out.println("Array after bubble sort:"); for(int i=0;i<a.length; i++){ System.out.println(a); a[j] temp; }}} System.out.println("List is already in sorted order"); public clarc Dejuanf H
public class Driver ( public static void main(String[] args) { System.out.println("Array1: "); int[] num1-{9,6,8,12,3,1,7); int[] x1-Arrays.copyOf(num1); Sorting.shellSort(num1); bubbleSort (x1); System.out.println("Array2: "); int[] num2-(19,8,28,13,14,5,2,4,21,1); int[] x2-Arrays.copyOf(num2); int[] y-Arrays.copyOf(num2); //for bubbleSort2 Sorting.shellSort (num2); bubbleSort (x2); bubbleSort2(y); System.out.println("Array3: "); int[] num3-(44,3,33,47,15,2,41,7,17,9,20,6,21,40,5,23,50,30,29,12); int[] x3-Arrays.copyOf(num3); I Sorting, shellSort (num3); bubbleSort (x3); int[] yi-(1,2,3,4,5,6,7,8,9,10); //for bubbleSort2 working for already sorted array bubbleSort2(yl);