8 elementary sorts-shell

16
Shellsort Shellsort is a simple extension of insertion sort that gains speed by allowing exchanges of entries that are far apart. to produce partially sorted arrays that can be efficiently sorted by insertion sort The idea is to rearrange the array to give it the property that taking every hth entry (starting anywhere) yields a sorted sequence. Such an array is said to be h-sorted. (cont.)

Upload: irdginfo

Post on 18-Jul-2015

99 views

Category:

Education


0 download

TRANSCRIPT

Shellsort

Shellsort is a simple extension of insertion sort that gains speed by allowing exchanges of entries that are far apart.

to produce partially sorted arrays that can be efficiently sorted by insertion sort

The idea is to rearrange the array to give it the property that taking every hth entry (starting anywhere) yields a sorted sequence.

Such an array is said to be h-sorted.

(cont.)

Shellsort (cont.)

Shellsort (cont.)

h-sorting:

By h-sorting for some large values of h, we can move entries in the array long distances and thus make it easier to h-sort for smaller values of h.

The number of compares used by shellsort with the increments 1, 4, 13, 40, 121, 364, ... is bounded by a small multiple of N times the number of increments used.

The number of compares used by shellsort with the increments 1, 4, 13, 40, 121, 364, ... is O(N3/2).

Shellsort (cont.)

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

Shellsort (cont.)

h = 13

77 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

Shellsort (cont.)

h = 13

77 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

Shellsort (cont.)

h = 13

77 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

Shellsort (cont.)

h = 13

77 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

Shellsort (cont.)

h = 13

77 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

Shellsort (cont.)

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

Shellsort (cont.)

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

Shellsort (cont.)

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

Shellsort (cont.)

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

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

Shellsort (cont.)

Shellsort (cont.)

void shell_sort(int A[], int size) {

int i, j, incrmnt, temp; incrmnt = (size / 3) +1; 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 = (incrmnt / 3) +1; }}