Lecture 23:  Sorting 3 – Shell Sort

 

Objectives of this lecture

q       Lean the weakness of Insertion Sorting Method

q       Lear an improvement of insertion sorting method – shell sort.

 

Problem of Insertion Sort:

q       There are actually two things that are considered when evaluating the efficiency of a sorting algorithm: number of comparisons and the number of moves (swapping of elements).

q       As explained earlier, the number of comparisons required in both selection sort and insertion sort is about n*n except for an ordered list in which the insertion sort requires about n comparisons.

q       However, if we consider the number of moves involved - which can be very tasking especially when the data items are records, insertion sort turn out to be worst than even selection sort.

q       In the case of selection sort, we only need to make one swap per iteration (swap the minimum element with the one at the beginning). 

q       In the case of insertion sort, each time we pick an element from the source sub-list, we have to move all the elements in the target sub-list that are bigger than the element to be inserted, to the next cell. 

q       For example, the following table shows the number of moves required for a list of 13 elements:

 

 

Original:

28

81

36

47

17

13

55

65

23

18

67

38

3

Moves

step1:

28

81

36

47

17

13

55

65

23

18

67

38

3

0

step2:

28

36

81

47

17

13

55

65

23

18

67

38

3

1

step3:

28

36

47

81

17

13

55

65

23

18

67

38

3

1

step4:

17

28

36

47

81

13

55

65

23

18

67

38

3

4

step5:

13

17

28

36

47

81

55

65

23

18

67

38

3

5

step6:

13

17

28

36

47

55

81

65

23

18

67

38

3

1

step7:

13

17

28

36

47

55

65

81

23

18

67

38

3

1

step8:

13

17

23

28

36

47

55

65

81

18

67

38

3

6

step9:

13

17

18

23

28

36

47

55

65

81

67

38

3

7

step10:

13

17

18

23

28

36

47

55

65

67

81

38

3

1

step11:

13

17

18

23

28

36

38

47

55

65

67

81

3

5

step12:

3

13

17

18

23

28

36

38

47

55

65

67

81

12

 

Total number of moves = 44

 


Shell Sort

q       This is a simple variation of insertion sort that can dramatically improve the processing time

q       The aim of the shell sort, (named after its inventor, Donald Shell, 1959) is to reduce the number of moves required in insertion sort.

q       When looking for a place to insert an item, shell sort first look at an element that is some distance d from the right hand side of the target sub-list.  d for example can be initially ½ or 1/3 of the list size.

q       On succeeding passes, d is reduced by some factor and the sorting is re-applied. 

q       By the time d reduces to one (equivalent do the normal insert sort) the items gets nearly sorted and the number of moves is drastically reduced.

q       The implementation is as follows:

 

void shell_sort(int list[])

{  int d=MAX_SIZE;

 

    do

    { d = d/3+1;

      ins_sort2(list,d);

    } while (d>1);

}

 

void ins_sort2(int list[], int d)

{ int i, j, temp;

 

  for (i=d; i<MAX_SIZE; i++)

  { temp=list[i];

    j=i;

   

    while ((j-d>=0) && list[j-d]>temp)

    { list[j]=list[j-d];

      j=j-d;

    }

    list[j]=temp;

  }

}

 

 


q       The following table shows the trace of the shell sort function:

 

 

Original:

28

81

36

47

17

13

55

65

23

18

67

38

3

Moves

 

For d=5

 

 

 

 

 

 

 

 

 

 

 

 

 

 

step1:

13

81

36

47

17

28

55

65

23

18

67

38

3

1

step2:

13

55

36

47

17

28

81

65

23

18

67

38

3

1

step3:

13

55

36

47

17

28

81

65

23

18

67

38

3

0

step4:

13

55

36

23

17

28

81

65

47

18

67

38

3

1

step5:

13

55

36

23

17

28

81

65

47

18

67

38

3

0

step6:

13

55

36

23

17

28

81

65

47

18

67

38

3

0

step7:

13

38

36

23

17

28

55

65

47

18

67

81

3

2

step8:

13

38

3

23

17

28

55

36

47

18

67

81

65

2

 

For d=2

 

 

 

 

 

 

 

 

 

 

 

 

 

 

step1:

3

38

13

23

17

28

55

36

47

18

67

81

65

1

step2:

3

23

13

38

17

28

55

36

47

18

67

81

65

1

step3:

3

23

13

38

17

28

55

36

47

18

67

81

65

0

step4:

3

23

13

28

17

38

55

36

47

18

67

81

65

1

step5:

3

23

13

28

17

38

55

36

47

18

67

81

65

0

step6:

3

23

13

28

17

36

55

38

47

18

67

81

65

1

step7:

3

23

13

28

17

36

47

38

55

18

67

81

65

1

step8:

3

18

13

23

17

28

47

36

55

38

67

81

65

4

step9:

3

18

13

23

17

28

47

36

55

38

67

81

65

0

step10:

3

18

13

23

17

28

47

36

55

38

67

81

65

0

step11:

3

18

13

23

17

28

47

36

55

38

65

81

67

1

 

For d= 1

 

 

 

 

 

 

 

 

 

 

 

 

 

 

step1:

3

18

13

23

17

28

47

36

55

38

65

81

67

0

step2:

3

13

18

23

17

28

47

36

55

38

65

81

67

1

step3:

3

13

18

23

17

28

47

36

55

38

65

81

67

0

step4:

3

13

17

18

23

28

47

36

55

38

65

81

67

2

step5:

3

13

17

18

23

28

47

36

55

38

65

81

67

0

step6:

3

13

17

18

23

28

47

36

55

38

65

81

67

0

step7:

3

13

17

18

23

28

36

47

55

38

65

81

67

1

step8:

3

13

17

18

23

28

36

47

55

38

65

81

67

0

step9:

3

13

17

18

23

28

36

38

47

55

65

81

67

2

step10:

3

13

17

18

23

28

36

38

47

55

65

81

67

0

step11:

3

13

17

18

23

28

36

38

47

55

65

81

67

0

step12:

3

13

17

18

23

28

36

38

47

55

65

67

81

1

 

Total number of moves = 24