Shell Sort - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

Shell Sort

Description:

... the shell sort is also the most complex of the O(n ) algorithms. ... First, it is arranged in an array with 7 columns (left), then the columns are sorted (right) ... – PowerPoint PPT presentation

Number of Views:1495
Avg rating:3.0/5.0
Slides: 21
Provided by: charle113
Category:
Tags: algorithms | shell | sort

less

Transcript and Presenter's Notes

Title: Shell Sort


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
77 62 14 25 30 14 56 98 4 12 88 21 80 9 70 55
9
Sorting in Place Shell Sort
h 13
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
10
Sorting in Place Shell Sort
h 13
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
11
Sorting in Place Shell Sort
h 13
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
12
Sorting in Place Shell Sort
h 13
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
13
Sorting in Place Shell Sort
h 13
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
14
Sorting in Place Shell Sort
h 4
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
9 62 14 25 30 14 56 98 4 12 88 21 80 77 70 55
15
Sorting in Place Shell Sort
h 4
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
16
Sorting in Place Shell Sort
h 1
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
4 12 14 21 9 14 56 25 30 62 70 55 80 77 88 98
17
Sorting in Place Shell Sort
h 1
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
77 62 14 25 30 14 56 98 4 12 88 21 80 9 70 55
1 element to be shifted
Insertion sort
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
4 12 14 14 21 25 30 56 62 77 80 88 98 9 70 55
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 gt 0)
  • for (iincrmnt i lt size i)
  • j i
  • temp Ai
  • while ((j gt incrmnt) (Aj-incrmnt gt
    temp))
  • Aj Aj - incrmnt
  • j j - incrmnt
  • Aj temp
  • incrmnt / 2
Write a Comment
User Comments (0)
About PowerShow.com