1 / 20

Shell Sort

Shell Sort. 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 is also the most complex of the O ( n ² ) algorithms. It is an improved version of the insertion sort. Shell Sort.

Download Presentation

Shell Sort

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Shell Sort

  2. 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 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 6 3 3 2 0 5 1 5 8 4 2 0 6 1 5 7 4 4 0 6 1 6 7 3 4 9 8 2 8 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 2 0 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 9 7 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 h = 13 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

  9. Sorting in Place – Shell Sort h = 13 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

  10. Sorting in Place – Shell Sort h = 13 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

  11. Sorting in Place – Shell Sort h = 13 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

  12. Sorting in Place – Shell Sort h = 13 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

  13. Sorting in Place – Shell Sort h = 13 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

  14. Sorting in Place – Shell Sort h = 4 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

  15. Sorting in Place – Shell Sort h = 4 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

  16. Sorting in Place – Shell Sort h = 1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

  17. Sorting in Place – Shell Sort h = 1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

  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 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1 element to be shifted Insertion sort 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 12 elements 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(n3/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(n2) – 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; } }

More Related