Enhanced Shell Sorting Algorithm: Basit Shahzad, and Muhammad Tanvir Afzal
Enhanced Shell Sorting Algorithm: Basit Shahzad, and Muhammad Tanvir Afzal
66
World Academy of Science, Engineering and Technology 27 2007
efficient as the merge, heap, and quick sorts are [2]. used.
It has been observed that Shell sort is a non-stable in-place Hs-1=Ceil (hs/2)
sort. Shell sort improves on the efficiency of insertion sort by For example for 100 elements of array the proposed values
quickly shifting values to their destination. Average sort time of ‘h’ will be
is O(n1.25), while worst-case time is O(n1.5). {50, 25, 13, 7, 4, 2, 1}
Various spacing may be used to implement the shell sort. But the values of ‘h’ for standard shell sort algorithm for
Typically, the array is sorted with large spacing, then the the same 100 elements are.
spacing is reduced, and the array is sorted again. On the final {13,4,1}
sort, spacing is one. Although the shell sort is easy to Now let’s take the same example as discussed in Insertion
comprehend, formal analysis is difficult. In particular, optimal sort and Shell sort to calculate the number of swaps in
spacing values elude theoreticians. Knuth has experimented Enhanced Shell sort algorithm.
with several values and recommends that spacing ‘h’ for an 20,10,51,92,25,57,48,37,12,86,33,1,113,1,2,228,27,82,60,
array of size N be based on the following formula: 100,12,52,3,1,85,65,14,41,71,17, 25,62,14,2,0,83,49,32
Let h1 = 1, hs+1 = 3hs + 1, and stop with ht when ht+2 ≥ N. Numbers of elements are 38 and now the values of h for
Thus, values of h are computed as follows: Enhanced Shell Sort will be
h1 = 1 {20, 10, 5, 3, 2, 1}
h2 = (3 x 1) + 1 = 4 In this case, the numbers of swaps are only 85.
h3 = (3 x 4) + 1 = 13
h4 = (3 x 13) + 1 = 40 II. COMPARISON OF THREE TECHNIQUES
h5 = (3 x 40) + 1 = 121 Now the comparison for the three techniques is made here
To sort 100 items we first find an ‘hs’ such that hs ≥ 100. for the same problem.
For 100 items, h5 is selected. The final value (ht) is two steps TABLE I
lower, or h3. Therefore sequence for the values of ‘h’ will be COMPARISON
13-4-1. Once the initial ‘h’ value has been determined, Insertion Sort Shell Sort Enhanced Shell Sort
subsequent values may be calculated using the formula
367 170 85
hs-1 = floor(hs / 3).
Let’s calculate the number of swaps for the same problem
by using Shell sort as discussed in Insertion sort. It is apparent that Shell sort reduces the number of swaps
20,10,51,92,25,57,48,37,12,86,33,1,113,1,2,228,27,82,60, up to 50 % as compared to the number of swaps in Insertion
100,12,52,3,1,85,65,14,41,71,17, 25,62,14,2,0,83,49,32 sort and Enhanced Shell Sort reduces the number of swaps
The algorithmic implementation showed that the series of further up to 50 % as compared to the number of swaps in
‘h’ is {4,1}, and the swaps required for the above values Shell Sort, thus improving the efficiency of the algorithm.
under Shell sort algorithm are 170.
400
C. Enhanced Shell Sort Algorithm
350
Enhanced Shell Sort algorithm works in the same way as
existing Shell Sort algorithm. Calculating the value of ‘h’ is a 300
Insertion Sort
key step in the execution of shell sort. The value of ‘h’ in 250
conventional shell sort is determined by the formula:
200 Shell Sort
Let h1 = 1, hs+1 = 3hs + 1, and stop with ht when ht+2 ≥ N.
By using this existing formula the shell sort algorithm 150
reduces the number of swaps up to 50 % as compared to that Enhanced Shell
100 Sort
of Insertion Sort.
Enhanced Shell Sort algorithm focuses to improve the 50
efficiency of the existing algorithm .Efficiency in the existing 0
algorithm can be improved by choosing the appropriate values Sorting Techniques
of ‘h’. Selection of the proper value of ‘h’ is a key point to
make it more efficient. Because before comparing all elements
of array with each other, it sounds good to arrange elements to Fig. 1 Comparison of Sorting Techniques
some extent so that when the spacing factor is ‘1’ the number
of swaps could be reduced maximally [7,8]. III. DETAIL DISCUSSION OF RESULTS
Enhance Shell Sort introduces a new mechanism for It is needful that the comparison of execution in terms of
calculating the value of h. The formula is given below to numbers of swaps required for each algorithm are made on a
calculate the first spacing for ‘h’. wider variety of data, in order to ensure and establish results
H= Ceil (n/2).,n is the total number of elements in the array. concretely.
To calculate the next values of h the following formula is
67
World Academy of Science, Engineering and Technology 27 2007
D. For 65 Elements
12,86,2,127,62,14,3,9,30,14,90,1,20,30,74,48,14,3,9,30,14,
H. For500 Elements
90,1,20,30,74,9,5,36,1,100,37,12,86,2,127,62,14,3,9,30,14,
37,12,86,2,127,62,14,3,9,30,14,90,1,20,30,74,48,37,40,7,10 25,7,9,5,36,1,100,37,12,86,2,127,62,14,3,9,30,14,90,1,20,
1,200,4,9 30,74,48,30,14,90,1,20,30,74,9,5,36,1,100,37,12,86,2,127,
Number of swaps in Insertion sort 917 62,14,3,9,30,14,90,10,52,63,1,2,23,12,25,7,9,5,36,1,100,37,
Number of Swaps in Shell sort 202 12,86,2,127,62,14,3,9,30,14,90,1,20,30,74,48,37,40,7,101,
Number of swaps in Enhanced Shell sort 105. 200,4,9,14,3,9,30,14,90,1,20,30,74,9,5,36,1,100,37,1,41,14,
85,2,10,52,63,1,2,23,12,25,7,9,5,36,1,100,37,12,86,2,127,6
E. For 100 Elements 2,14,3,9,30,14,90,1,20,30,74,48,14,3,9,30,14,90,1,20,30,74,
1,41,14,85,2,10,52,63,1,2,23,12,25,7,9,5,36,1,100,37,12,86, 9,5,36,1,100,37,12,86,2,127,62,14,3,9,30,14,90,10,52,63,1,
2,127,62,14,3,9,30,14,90,1,20,30,74,48,14,3,9,30,14,90,1, 2,23,12,25,7,9,5,36,1,100,37,12,86,2,127,62,14,3,9,30,14,
20,30,74,9,5,36,1,100,37,12,86,2,127,62,14,3,9,30,14,90,10 90,1,20,30,74,48,37,40,7,101,200,4,9,14,3,9,30,14,90,1,20,
,52,63,1,2,23,12,25,7,9,5,36,1,100,37,12,86,2,127,62,14,3,9 30,74,48,30,14,90,1,20,30,74,9,5,36,1,100,37,12,86,2,127,
,30,14,90,1,20,30,74,48,37,40,7,101,200,4,9 62,14,3,9,30,14,90,10,52,63,1,2,23,12,25,7,9,5,36,1,100,37,
Number of swaps in Insertion sort 2181 12,86,25,7,9,5,36,1,100,37,12,86,2,127,62,14,3,9,30,14,90,
Number of Swaps in Shell sort 756 1,20,30,74,48,30,14,90,1,20,30,74,9,5,36,1,100,37,12,86,2,
Number of swaps in Enhanced Shell sort 239 127,62,14,3,9,30,14,90,10,52,63,1,41,14,85,2,10,52,63,1,2,
23,12,25,7,9,5,36,1,100,37,12,86,2,127,62,14,3,9,30,14,90,
F. For 200 Elements
1,20,30,74,48,14,3,9,30,14,90,1,20,30,74,9,5,36,1,100,37,
1,41,14,85,2,10,52,63,1,2,23,12,25,7,9,5,36,1,100,37,12,86, 12,86,2,127,62,14,3,9,30,14,90,10,52,63,1,2,23,12,25,7,9,
2,127,62,14,3,9,30,14,90,1,20,30,74,48,14,3,9,30,14,90,1,2 5,36,1,100,37,12,86,2,127,62,14,3,9,30,14,90,1,20,30,74,48
0,30,74,9,5,36,1,100,37,12,86,2,127,62,14,3,9,30,14,90,10, ,37,40,7,101,200,4,9,1,2,23,12,25,7,9,5,36,1,100,37,12,86,2
52,63,1,2,23,12,25,7,9,5,36,1,100,37,12,86,2,127,62,14,3,9, , 127,62,14,3,9,30,14,90,1,20,30,74,48,37,40,7,101,200,4,9,
30,14,90,1,00,37,12,86,2,127,62,14,3,9,20,30,74,48,37,40,7 12,86,2,127,62,14,3,9,30,14,90,1,20,30,74,48,14,3,9,30,14,
,101,200,4,900,37,12,86,2,127,62,14,3,9,30,14,90,1,20,30, 90,1,20,30,74,9,5,36,1,100,37,12,86,2,127,62,14,3,9,30,14,
74,48,37,40,7,101,200,12,86,2,127,62,14,3,9,30,14,90,1,20, 37,12,86,2,127,62,14,3,9,30,14,90,1,20,30,74,48,37,40,7,10
30,74,48,14,3,9,30,14,90,1,20,30,74,9,5,36,1,100,37,12,86, 1,200,4,9
2,127,62,14,3,9,30,14,37,12,86,2,127,62,14,3,9,30,14,90,1,
20,30,74,48,37,40,7,101,200,4,9
Number of swaps in Insertion sort 4200 Number of swaps in Insertion sort 35144
Number of Swaps in Shell sort 1541 Number of Swaps in Shell sort 2703
Number of swaps in Enhanced Shell sort 471. Number of swaps in Enhanced Shell sort1781.
68
World Academy of Science, Engineering and Technology 27 2007
TABLE II 40000
COMPARISON OF INSERTION, SHELL AND ENHANCED SHELL SORT
35000
CASES Insertion Shell Enhanced Shell Sort
A 30 30 12 30000
B 111 47 31
Number of Swaps
C 225 109 56
25000 Insertion
D 917 202 105
E 2181 756 239 Sort
F 4200 1541 471 20000 Shell Sort
G 21144 1803 928
H 35144 2703 1781 15000 Enhanced
Shell Sort
2500 10000
5000
2000
0
100 200 300 400 500
1500
Num ber of Elem ents
IV. DISCUSSION
500
Robert Sedgewick in his paper[11] opens discussion for the
performance issues of the Shell sort algorithm and claims that
0 finding a sequence that leads to running times 25% lower than
a b c d e the best known certainly would be of practical interest,
Running time can be reduced with the reduction in number of
Cases
comparisons for the algorithm. We have reduced the number
of comparisons up to 60% in some ideal cases but in many
Insertion Sort Shell Sort cases up to 20%. We executed three algorithms (Insertion sort,
Enhanced Shell Sort Shell sort and Enhanced Shell sort ) on same set of data and
found some interesting results as can be seen in the
Fig. 2 Comparison Graph comparisons of the algorithms for different cases in Figure 2.
The performance of this algorithm would be investigated in
The enhanced shell sorting algorithm is a key towards future for N= 104 or 106. But its performance has been proved
achieving the excellence in the algorithms to provide the for N= 103 that is useful in normal cases. Some attempts in the
efficient solutions. This has been done by decreasing the past [12][13][14] have shown great concerns about
swaps required to sort the array of numbers, considerably. performance. Marcin Ciura [12] shows the results for 128
The results show that the new algorithm provides a much elements where the data was in sorted form after taking
efficient way to sort the data and hence causes to save the 535.71 swaps but in our case 200 elements have gone through
computational resources. It has been observed that the the sorting process from only 471 swaps. These are the
enhanced shell sorting algorithm can solve the problem in standard swaps for 200 elements in our case. They do not
almost 20 times less swaps as compared to insertion sort and change with the change in data but the number of comparisons
in almost half swaps as compared to the shell sorting may vary in certain cases. But overall performance remains
algorithm. Fig. 3 shows the detailed overview of the number better than available performance enhancements techniques.
of swaps required to sort different number of elements.
V. CONCLUSION
This work focuses to provide an enhancement in existing
algorithm. Shell sort algorithm gives an average number of
comparisons but produces a problem that it does not give least
number of swaps. It has been observed that number of swaps
produced by Shell Sort can be further reduced. The motivation
for reducing the number of swaps is to economically and
effectively use the computational resources that are available
in terms of processor speed, memory and storage.
Enhanced shell sort algorithm provides a powerful solution
69
World Academy of Science, Engineering and Technology 27 2007
ACKNOWLEDGMENT
I feel it my principal obligation to thank all my friends who
helped me in identifying the need to work in this area. I am
also thankful to all those who appreciated this work and
provided me a boost to think and to work more effectively in
this area of sorting.
REFERENCES
[1] http://www.nist.gov/dada/html/shellsort.htm
[2] http://linux.wku.edu/~lamonml/algor/sort/shell.html
[3] http://oopweb.com/Algorithms/Documents/Sman/VolumeFrames.html?/
Algorithms/Documents/Sman/Volume/ShellSort_files/s_shl.htm
[4] Yedidyah Langsam, Moshe J. Augenstan, Aadrew M. Tenenbaum, “Data
Structures using C and C++” , 2nd ed, Pearson Education, pp360-366
[5] Larry Nyhoff, “An introduction to Data Structures”,2nd ed, pp: 581-585
[6] linux.wku.edu/~lamonml/algor/sort/sort.html
[7] www.cs.hope.edu/alganim/ccaa/shellsort.html
[8] www.inf.fhflensburg.de/lang/algorithmen/sortieren/shell/shellen.htm
[9] www.math.grin.edu/~stone/events/scheme-workshop/shellsort.html
[10] www.cs.odu.edu/~zeil/cs361/Lectures-f02/06sorting/shell/shell.html
[11] Robert Sedgewick, “Analysis of Shellsort and Related Algorithms”,
Proceedings of the Fourth Annual European Symposium on
Algorithms},1996, pp: 1-11
[12] Marcin Ciura “Best Increments for the Average Case of
Shellsort”,Proceedings of the 13th International Symposium on
Fundamentals of Computation Theory,2001, pp: 106—117
[13] Jiang, T., Li, M., Vit´anyi, P.: The average-case complexity of Shellsort.
Lecture Notes in Computer Science 1644 (1999), 453–462.
[14] Janson, S., Knuth, D. E.: Shellsort with three increments. Random
Structures and Algorithms 10 (1997), 125–142.
70