# DS13_Ch06b - 4 Shellsort - by Donald Shell Example Sort: 81...

This preview shows pages 1–4. Sign up to view the full content.

§4 Shellsort ---- by Donald Shell 〖 Example 〖 Sort: 81 94 11 96 12 35 17 95 28 58 41 75 15 96 28 12 58 81 35 41 94 17 75 11 95 15 5 -sort 96 41 94 11 28 58 35 75 95 12 81 17 15 3 -sort 1 -sort 96 41 94 11 28 58 35 75 95 12 81 17 15 Define an increment sequence h 1 < h 2 < … < h t ( h 1 = 1 ) at each phase for k = t , t - 1, …, 1 Note: An h k -sorted file that is then h k - 1 -sorted remains h k -sorted. Note: An h k -sorted file that is then h k - 1 -sorted remains h k -sorted. 1/10

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
§4 Shellsort Shell’s increment sequence: h t = N / 2 , h k = h k +1 / 2 void Shellsort( ElementType A[ ], int N ) { int i, j, Increment; ElementType Tmp; for ( Increment = N / 2; Increment > 0; Increment /= 2 ) /*h sequence */ for ( i = Increment; i < N; i++ ) { /* insertion sort */ Tmp = A[ i ]; for ( j = i; j >= Increment; j - = Increment ) if ( Tmp < A[ j - Increment ] ) A[ j ] = A[ j - Increment ]; else break ; A[ j ] = Tmp; } /* end for-I and for-Increment loops */ } 2/10
§4 Shellsort Worst-Case Analysis: Theorem 〖 The worst-case running time of Shellsort, using Shell’s increments, is Θ ( N 2 ) . 〖 Example 〖 A bad case: 1 9 2 10 3 11 4 12 5 13 6 14 7 15 8 16 1 9 2 10 3 11 4 12 5 13 6 14 7 15 8 16 8 -sort 1 9 2 10 3 11 4 12 5 13 6 14 7 15 8 16 4 -sort 1 9 2 10 3 11 4 12 5 13 6 14 7 15 8 16 2 -sort 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 1 -sort

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

## DS13_Ch06b - 4 Shellsort - by Donald Shell Example Sort: 81...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online