Shellsort improved insertion sort
Shellsort insertion sort: -most moves of data are a single step shellsort: -long moves in first loop, then shorter moves in later loops -uses same basic logic as insertion
Insertion pseudocode a: array of integers, length n for (i = 1; i < n; i++) temp = a[i] j = i while ( a[j-1] > temp ) a[j] = a[j-1] j- - a[j] = temp temp
Shellsort many small ‘parallel’ insertion sorts reduce number of parallel sorts, round by round last round is pure insertion sort (but data is ‘almost’ sorted)
Shellsort pseudocode // h is “step size” h = 1 while ( h <= n ) h = h*3 + 1 while ( h > 1 ) h = h / 3 for (i = h; i < n; i++) temp = a[i] j = i while ( j >= h && a[j-h] > temp ) a[j] = a[j-h] j = j-h a[j] = temp
Shellsort example ASORTINGEXAMPLE AEORTINGEXAMPLS AEAGEINMPLORTXS AAEEGILMNOPRSTX 13 h 4 h 1 h
Shellsort performance formal analysis unsolved (as far as I know) estimates: O(n 3/2 ), possibly better depends on sequence of h values – should be relatively prime