Presentation is loading. Please wait.

Presentation is loading. Please wait.

Shell Sort. Invented by Donald Shell in 1959, the shell sort is the most efficient of the O(n²) class of sorting algorithms. Of course, the shell sort.

Similar presentations


Presentation on theme: "Shell Sort. Invented by Donald Shell in 1959, the shell sort is the most efficient of the O(n²) class of sorting algorithms. Of course, the shell sort."— Presentation transcript:

1 Shell Sort

2 Invented by Donald Shell in 1959, the shell sort is the most efficient of the O(n²) class of sorting algorithms. Of course, the shell sort is also the most complex of the O(n²) algorithms. It is an improved version of the insertion sort.

3 Shell Sort Shell sort is a generalization of insertion sort, with two observations in mind: – Insertion sort is efficient if the input is "almost sorted". – Insertion sort is inefficient, on average, because it moves values just one position at a time. Shell sort improves insertion sort by comparing elements separated by a gap of several positions. This lets an element take "bigger steps" toward its expected position. Multiple passes over the data are taken with smaller and smaller gap sizes. The last step of Shell sort is a plain insertion sort, but by then, the array of data is guaranteed to be almost sorted.

4 Shell Sort – the idea The idea of Shellsort is the following: arrange the data sequence in a two-dimensional array sort the columns of the array The effect is that the data sequence is partially sorted. The process above is repeated, but each time with a narrower array, i.e. with a smaller number of columns. In the last step, the array consists of only one column. In each step, the “sorted-ness” of the sequence is increased, until in the last step it is completely sorted. However, the number of sorting operations necessary in each step is limited, due to the pre-sortedness of the sequence obtained in the preceding steps.

5 Shell Sort – the idea Example: Let 3 7 9 0 5 1 6 8 4 2 0 6 1 5 7 3 4 9 8 2 be the data sequence to be sorted. First, it is arranged in an array with 7 columns (left), then the columns are sorted (right): 3 7 9 0 5 1 63 3 2 0 5 1 5 8 4 2 0 6 1 57 4 4 0 6 1 6 7 3 4 9 8 28 7 9 9 8 2 Data elements 8 and 9 have now already come to the end of the sequence, but a small element (2) is also still there. In the next step, the sequence is arranged in 3 columns, which are again sorted:

6 Shell Sort – the idea 3 3 20 0 1 0 5 1 1 2 2 5 7 4 3 3 4 4 0 6 4 5 6 1 6 8 5 6 8 7 9 97 7 9 8 2 8 9 Now the sequence is almost completely sorted. When arranging it in one column in the last step, it is only a 6, an 8 and a 9 that have to move a little bit to their correct position.

7 Shell Sort – the implementation Actually, the data sequence is not arranged in a two-dimensional array, but held in a one- dimensional array that is indexed appropriately. For instance, data elements at positions 0, 5, 10, 15 etc. would form the first column of an array with 5 columns. The "columns" obtained by indexing in this way are sorted with Insertion Sort, since this method has a good performance with presorted sequences.

8 Sorting in Place – Shell Sort 776214253014569841288218097055 0123456789101112131415 h = 13

9 Sorting in Place – Shell Sort h = 13 776214253014569841288218097055 962142530145698412882180777055 0123456789101112131415

10 Sorting in Place – Shell Sort h = 13 776214253014569841288218097055 962142530145698412882180777055 0123456789101112131415

11 Sorting in Place – Shell Sort h = 13 776214253014569841288218097055 962142530145698412882180777055 962142530145698412882180777055 0123456789101112131415

12 Sorting in Place – Shell Sort h = 13 776214253014569841288218097055 962142530145698412882180777055 962142530145698412882180777055 0123456789101112131415

13 Sorting in Place – Shell Sort h = 13 776214253014569841288218097055 962142530145698412882180777055 962142530145698412882180777055 962142530145698412882180777055 0123456789101112131415

14 Sorting in Place – Shell Sort 962142530145698412882180777055 h = 4 0123456789101112131415

15 Sorting in Place – Shell Sort 962142530145698412882180777055 962142530145698412882180777055 914 2530625698412882180777055 914 2530625698412882180777055 914 2530625698412882180777055 414 2596256983012882180777055 412142591456983062882180777055 412142591456983062882180777055 412142191456253062889880777055 412142191456253062889880777055 412142191456253062889880777055 412142191456253062709880778855 412142191456253062705580778898 0123456789101112131415 h = 4

16 Sorting in Place – Shell Sort 412142191456253062705580778898 0123456789101112131415 h = 1

17 Sorting in Place – Shell Sort 412142191456253062705580778898 412142191456253062705580778898 412142191456253062705580778898 412142191456253062705580778898 491214211456253062705580778898 491214 2156253062705580778898 491214 2156253062705580778898 491214 2125563062705580778898 491214 2125305662705580778898 491214 2125305662705580778898 491214 2125305662705580778898 491214 2125305556627080778898 491214 2125305556627080778898 491214 2125305556627077808898 491214 2125305556627077808898 0123456789101112131415 h = 1

18 Advantage of Shell Sort Faster than the ordinary insertion sort More efficient exchange of elements that are far from their proper position Shell sort 776214253014569841288218097055 0123456789101112131415 Insertion sort 41214 21253056627780889897055 0123456789101112131415 12 elements to be shifted 1 element to be shifted

19 Efficiency of Shell Sort Efficiency depends of the values of h Values of h recommended by Knuth in 1969 1 4 13 40 121 364 1093 3280 9841... – Start with 1, then multiply by 3 and add 1 – Less than O(n 3/2 ) comparisons Values of h recommended by Shell in 1959 1 2 4 8 16 32 64 128 256 512 1024 2048... – Bad sequence because elements in odd positions are not compared to elements in even positions until the final pass – Worst case efficiency of O(n 2 ) – smallest values in even positions and largest values in odd positions

20 Example Shell Sort void shell_sort(int A[], int size) { int i, j, incrmnt, temp; incrmnt = size / 2; while (incrmnt > 0) { for (i=incrmnt; i < size; i++) { j = i; temp = A[i]; while ((j >= incrmnt) && (A[j-incrmnt] > temp)) { A[j] = A[j - incrmnt]; j = j - incrmnt; } A[j] = temp; } incrmnt /= 2; }


Download ppt "Shell Sort. Invented by Donald Shell in 1959, the shell sort is the most efficient of the O(n²) class of sorting algorithms. Of course, the shell sort."

Similar presentations


Ads by Google