DS Exam 1 Question 5
Comparing to quick sort
Best case efficiency is O(nlog^2n) and worst case of O(n^2) compared to quick sort of best case O(nlogn) and worst case O(n^2)
Empirical results
H=3H+1 shows average efficiency of shell sort in terms of number of comparisons which is O(n(log(n)^2) which is almost O(n^1.5). The best case efficiency is O(nlog(n)), similar to quick sort. Worst case is O(n^2), similar to quick sort.
Impact on performance for shell sort
Increment sequences impact performance. With a por sequence, such as H is a decreasing power of 2, large values in odd positions will not be sorted until H = 1. This requires large movements for extreme elements at the ends and reduce efficiency. Many elements will be compared repeatedly which is unnecessary.
Describe Shell sort
It's a modified insertion sort that improves the general efficiency by exchanging items far apart. The list uses an increment sequence where large gap value H is declared and an insertion sort is performed on every Hth element of the list. This is done for each fractional set of H-spaced elements within the list and the list is h-sorted. The process is repeated on smaller and smaller gaps until H is reduced to 1.
The number of repetitions of segmented insertion sort
O(logN). Outer loop of each SIS is O(n). Inner loop depends on the current order of the data within that segment. The total number of comparisons in this case is O(Anlog(n)) where A is unknown.
Comparing shell to insertion
Shell takes advantage of improved efficiency of insertion sort for mostly sorted list. Gap rearrangements allow elements to move long distances in the original list, reducing large amounts of disorder quickly and leaves less work for smaller h-sort steps. Shell sort breaks the O(n^2) worst case efficiency
Worst case efficienciesfor shell sort
Shell: N/(2^k): O(N^1) [when N=2^p] Hibbard: (2^k)-1: O(N^(3/2)) Knuth: ((3^k)-1)/2: O(N^(3/2))
Stability of shell sort
Since there are many different incremental sequences that can be applied, this is unstable and it's hard to configure an estimated efficiency analysis.
Pseudocode for shell sort
int i, j, h, temp; for (h=N/2; h>0; h/=2){ for (i = h; i < N; i++){ temp = A[i]; for (j = i; j>= h && A[j-h] > temp; j-=h) A[j] = A[j-h];} A[j] = temp;}