Running head: WRITTEN ASSIGNMENT UNIT 6
Written Assignment UNIT 6
CS 3303 Data Structures and Algorithms
Chinu Singla
Author Note
XXXX, Department of Computer Science, University of the People
Running head: WRITTEN ASSIGNMENT UNIT 6
Code:
import jeliot.io.*;
class ShellSort
static void printArray(int array[])
int l = array.length; for (int i=0; i<l; ++i) System.out.print(array[i] + " "); System.out.println(); 1 int sort(int arr[])
int cou=0; int n = arr.length; for (int takePart = n/2; takePart > 0; takePart /= 2)
for (int i = takePart; i < n; i += 1)
int temp = arr[i]; int j; for (j = i; j >= takePart && arr[j - takePart] > temp; j -= takePart) arr[j] = arr[j - takePart]; cou+=1; arr[j] = temp; }
return cou;
public static void main(String args[]) 1 int arr[] = {12,9,4,99,120,1,3,10,23,45,75,69,31,88,101,14,29,91,2,0,77}; ShellSort ob = new ShellSort(); int count=ob.sort(arr);
System.out.println(count+" Exchanges during sorting"); System.out.print("Values after the sort:"); printArray(arr);
Running head: WRITTEN ASSIGNMENT UNIT 6
Asymptotic Analysis:
Primarily, this assignment required 21 integers to be sorted. Out of the sorting methods allowed.
I utilized Shell Sort algorithm. I was able to sort the integers with a smaller number of
exchanges, i.e., 66, compared to the insertion sorting method which took 114 exchanges.
Generally, Shell Sort algorithm relies upon the notion that insertion sort does very well if the
array is nearly sorted. In simple words, insertion sort does well if it does not have to move each
item too far due to lots of comparisons. The idea evolves as repeatedly do insertion sort on all
elements at fixed, decreasing distances apart: hk, h-1, ..., h= 1. Since the last increment
used should be 1, it means that regular insertion sort is done at the last step, ensuring that the
array will be sorted.
The time complexity of Shell Sort is dependent on the gap sequence, and its best-case time
complexity is 0 (n*log n).For the worst case, it will be 0 (n*log2 n). Using the sorting
algorithm Shell Sort as compared to insertion sort, it is noted that it renders the correct sort
results in lesser number of exchanges. Therefore, Shell Sort is more efficient algorithm than
Insertion Sort. Another better algorithm could be quick sort; however, it carries more potential
for errors.
Output: Console 66 Exchanges during sorting Values after the sort:0 1 2 3 4 9 10 12 14 23 29 31 45 69 75 77 88 91 99 101 120