100 likes | 474 Views
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++)
E N D
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 30 temp 33 39 42 43 14 16 21 27 33 39 42 43 30 32 11 56 26 19 42 10
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 A S O R T I N G E X A M P L E 13 h A E O R T I N G E X A M P L S 4 h A E A G E I N M P L O R T X S 1 h A A E E G I L M N O P R S T X
Shellsort performance • formal analysis unsolved (as far as I know) • estimates: O(n3/2), possibly better • depends on sequence of h values – should be relatively prime