shell sort. invented by donald shell in 1959, the shell sort is the most efficient of the o(n) class...

20
Shell Sort

Upload: berenice-mills

Post on 19-Jan-2018

243 views

Category:

Documents


0 download

DESCRIPTION

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.

TRANSCRIPT

Page 1: 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

Shell Sort

Page 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

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.

Page 3: 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

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.

Page 4: 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

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.

Page 5: 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

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 58 4 2 0 6 1 5 7 4 4 0 6 1 67 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:

Page 6: 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

Shell Sort – the idea

3 3 2 0 0 10 5 1 1 2 25 7 4 3 3 44 0 6 4 5 61 6 8 5 6 87 9 9 7 7 98 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.

Page 7: 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

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.

Page 8: 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

Sorting in Place – Shell Sort

77 62 14 25 30 14 56 98 4 12 88 21 80 9 70 55

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15h = 13

Page 9: 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

Sorting in Place – Shell Sort

h = 1377 62 14 25 30 14 56 98 4 12 88 21 80 9 70 55

9 62 14 25 30 14 56 98 4 12 88 21 80 77 70 55

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Page 10: 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

Sorting in Place – Shell Sort

h = 1377 62 14 25 30 14 56 98 4 12 88 21 80 9 70 55

9 62 14 25 30 14 56 98 4 12 88 21 80 77 70 55

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Page 11: 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

Sorting in Place – Shell Sort

h = 1377 62 14 25 30 14 56 98 4 12 88 21 80 9 70 55

9 62 14 25 30 14 56 98 4 12 88 21 80 77 70 55

9 62 14 25 30 14 56 98 4 12 88 21 80 77 70 55

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Page 12: 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

Sorting in Place – Shell Sort

h = 1377 62 14 25 30 14 56 98 4 12 88 21 80 9 70 55

9 62 14 25 30 14 56 98 4 12 88 21 80 77 70 55

9 62 14 25 30 14 56 98 4 12 88 21 80 77 70 55

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Page 13: 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

Sorting in Place – Shell Sort

h = 1377 62 14 25 30 14 56 98 4 12 88 21 80 9 70 55

9 62 14 25 30 14 56 98 4 12 88 21 80 77 70 55

9 62 14 25 30 14 56 98 4 12 88 21 80 77 70 55

9 62 14 25 30 14 56 98 4 12 88 21 80 77 70 55

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Page 14: 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

Sorting in Place – Shell Sort

9 62 14 25 30 14 56 98 4 12 88 21 80 77 70 55

h = 4 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Page 15: 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

Sorting in Place – Shell Sort

9 62 14 25 30 14 56 98 4 12 88 21 80 77 70 55

9 62 14 25 30 14 56 98 4 12 88 21 80 77 70 55

9 14 14 25 30 62 56 98 4 12 88 21 80 77 70 55

9 14 14 25 30 62 56 98 4 12 88 21 80 77 70 55

9 14 14 25 30 62 56 98 4 12 88 21 80 77 70 55

4 14 14 25 9 62 56 98 30 12 88 21 80 77 70 55

4 12 14 25 9 14 56 98 30 62 88 21 80 77 70 55

4 12 14 25 9 14 56 98 30 62 88 21 80 77 70 55

4 12 14 21 9 14 56 25 30 62 88 98 80 77 70 55

4 12 14 21 9 14 56 25 30 62 88 98 80 77 70 55

4 12 14 21 9 14 56 25 30 62 88 98 80 77 70 55

4 12 14 21 9 14 56 25 30 62 70 98 80 77 88 55

4 12 14 21 9 14 56 25 30 62 70 55 80 77 88 98

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15h = 4

Page 16: 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

Sorting in Place – Shell Sort

4 12 14 21 9 14 56 25 30 62 70 55 80 77 88 98

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15h = 1

Page 17: 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

Sorting in Place – Shell Sort

4 12 14 21 9 14 56 25 30 62 70 55 80 77 88 98

4 12 14 21 9 14 56 25 30 62 70 55 80 77 88 98

4 12 14 21 9 14 56 25 30 62 70 55 80 77 88 98

4 12 14 21 9 14 56 25 30 62 70 55 80 77 88 98

4 9 12 14 21 14 56 25 30 62 70 55 80 77 88 98

4 9 12 14 14 21 56 25 30 62 70 55 80 77 88 98

4 9 12 14 14 21 56 25 30 62 70 55 80 77 88 98

4 9 12 14 14 21 25 56 30 62 70 55 80 77 88 98

4 9 12 14 14 21 25 30 56 62 70 55 80 77 88 98

4 9 12 14 14 21 25 30 56 62 70 55 80 77 88 98

4 9 12 14 14 21 25 30 56 62 70 55 80 77 88 98

4 9 12 14 14 21 25 30 55 56 62 70 80 77 88 98

4 9 12 14 14 21 25 30 55 56 62 70 80 77 88 98

4 9 12 14 14 21 25 30 55 56 62 70 77 80 88 98

4 9 12 14 14 21 25 30 55 56 62 70 77 80 88 98

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15h = 1

Page 18: 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

Advantage of Shell Sort

Faster than the ordinary insertion sort More efficient exchange of elements that are far from

their proper position

Shell sort

77 62 14 25 30 14 56 98 4 12 88 21 80 9 70 55

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Insertion sort

4 12 14 14 21 25 30 56 62 77 80 88 98 9 70 55

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

12 elements to be shifted

1 element to be shifted

Page 19: 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

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 19591 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

Page 20: 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

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; }

}