0% found this document useful (0 votes)
41 views

Enhanced Shell Sorting Algorithm: Basit Shahzad, and Muhammad Tanvir Afzal

The document summarizes and compares different sorting algorithms, including bubble sort, heap sort, insertion sort, and shell sort. It then focuses on describing shell sort and an enhanced version of shell sort proposed in the paper. Standard shell sort reduces the number of swaps compared to insertion sort by sorting subsets of the array with larger and larger increments between elements. The paper proposes calculating the increments (h values) differently to further reduce the number of swaps. Testing on an example array of 38 elements, standard shell sort requires 367 swaps while the enhanced version reduces this to under 60%.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views

Enhanced Shell Sorting Algorithm: Basit Shahzad, and Muhammad Tanvir Afzal

The document summarizes and compares different sorting algorithms, including bubble sort, heap sort, insertion sort, and shell sort. It then focuses on describing shell sort and an enhanced version of shell sort proposed in the paper. Standard shell sort reduces the number of swaps compared to insertion sort by sorting subsets of the array with larger and larger increments between elements. The paper proposes calculating the increments (h values) differently to further reduce the number of swaps. Testing on an example array of 38 elements, standard shell sort requires 367 swaps while the enhanced version reduces this to under 60%.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

World Academy of Science, Engineering and Technology 27 2007

Enhanced Shell Sorting Algorithm


Basit Shahzad, and Muhammad Tanvir Afzal

memory, most implementations use an in-place sort that works


Abstract—Many algorithms are available for sorting the by moving the current item past the already sorted items and
unordered elements. Most important of them are Bubble sort, Heap repeatedly swapping it with the preceding item until it is in
sort, Insertion sort and Shell sort. These algorithms have their own place.
pros and cons. Shell Sort which is an enhanced version of insertion
Like the bubble sort, the insertion sort has a complexity of
sort, reduces the number of swaps of the elements being sorted to
minimize the complexity and time as compared to insertion sort. O(n2). Although it has the same complexity, the insertion sort
Shell sort improves the efficiency of insertion sort by quickly shifting is a little over twice as efficient as the bubble sort.
values to their destination. Average sort time is O(n1.25), while worst- In the following example, it is calculated that how many
case time is O(n1.5). It performs certain iterations. In each iteration it swaps are required in Insertion sort. Following is the list of 38
swaps some elements of the array in such a way that in last iteration elements the Insertion sort algorithm is applied, in order to see
when the value of h is one, the number of swaps will be reduced. that how much swaps are required for this algorithm to bring
Donald L. Shell invented a formula to calculate the value of ‘h’. this
work focuses to identify some improvement in the conventional Shell the elements in order.
sort algorithm. “Enhanced Shell Sort algorithm” is an improvement 20,10,51,92,25,57,48,37,12,86,33,1,113,1,2,228,27,82,60,
in the algorithm to calculate the value of ‘h’. It has been observed 100,12,52,3,1,85,65,14,41,71,17,25,62,14,2,0,83,49,32
that by applying this algorithm, number of swaps can be reduced up When Insertion Sort was applied on the given array then
to 60 percent as compared to the existing algorithm. In some other number of swaps calculated for it are 367.
cases this enhancement was found faster than the existing algorithms
available.
B. Shell Sort
Keywords—Algorithm, Computation, Shell, Sorting. The first diminishing increment sort. On each pass, ‘i’ sets
of n/i items are sorted, typically with insertion sort. On each
I. INTRODUCTION succeeding pass, i is reduced until it is 1 for the last pass. A
good series of i values is important to efficiency [1].
S ORTING has been a profound area for the algorithmic
researchers. And many resources are invested to suggest a
more working sorting algorithm. For this purpose many
Invented by Donald Shell in 1959, the shell sort is the most
efficient of the O(n2) class of sorting algorithms [4]. Of
course, the shell sort is also the most complex of the O(n2)
existing sorting algorithms were observed in terms of the
algorithms.
efficiency of the algorithmic complexity. Shell sort and
The shell sort is a "diminishing increment sort", better
insertion sort algorithms were observed to be both economical
known as a "comb sort" to the unwashed programming
and efficient [1,2]
masses. The algorithm makes multiple passes through the list,
The comparison of “Enhanced Shell sort” with Insertion
and each time sorts a number of equally sized sets using the
sort and Shell sort is made. In Shell sort the numbers of swaps
insertion sort [5]. The size of the set to be sorted gets larger
are reduced as compared to Insertion sort and in “Enhanced
with each pass through the list, until the set consists of the
Shell Sort” the numbers of swaps are further reduced as
entire list. (Note that as the size of the set increases, the
compared to Shell sort.
number of sets to be sorted decreases.) This sets the insertion
sort up for an almost-best case run each iteration with a
A. Insertion Sort
complexity that approaches O(n) [9,10].
The insertion sort, as its name suggests, inserts each item The items contained in each set are not contiguous, rather ,
into its proper place in the final list. The simplest if there are i sets then a set is composed of every i-th element.
implementation of this requires two list structures: the source For example, if there are 3 sets then the first set would contain
list and the list into which sorted items are inserted. To save the elements located at positions 1, 4, 7 and so on. The second
set would contain the elements located at positions 2, 5, 8, and
Manuscript received October 9, 2001. so on; while the third set would contain the items located at
Basit Shahzad is senior Lecturer at the COMSATS Institute of Information positions 3, 6, 9, and so on.[6]
Technology, Islamabad-Pakistan where he is working the software and
algorithms research group. He has published several research papers and has The size of the sets used for each iteration has a major
keen interest in the area of algorithms (e-mail: impact on the efficiency of the sort. The algorithm provides
[email protected]). efficient execution for medium-size lists.
Tanvir Afzal is undertaking his PhD research in the Institute of Information
Systems and Computer Media at the Graz University of Technology, Austria
Along with the benefit of being robust, the algorithm is
(e-mail: [email protected]). considered to be somewhat complex and not nearly as

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

A. For 10 Elements G. For 300 Elements


100,37,12,86,2,127,62,14,3,9 25,7,9,5,36,1,100,37,12,86,2,127,62,14,3,9,30,14,90,1,20,
Number of swaps in Insertion sort 30 30,74,48,30,14,90,1,20,30,74,9,5,36,1,100,37,12,86,2,127,
Number of Swaps in Shell sort 30 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 Enhanced Shell sort 12. 12,86,2,127,62,14,3,9,30,14,90,1,20,30,74,48,37,40,7,101,
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
B. For 25 Elements
2,14,3,9,30,14,90,1,20,30,74,48,14,3,9,30,14,90,1,20,30,74,
100,37,12,86,2,127,62,14,3,9,30,14,90,1,20,30,74,48,37,40, 9,5,36,1,100,37,12,86,2,127,62,14,3,9,30,14,90,10,52,63,1,
7,101,200 2,23,12,25,7,9,5,36,1,100,37,12,86,2,127,62,14,3,9,30,14,
Number of swaps in Insertion sort 111 90,1,20,30,74,48,37,40,7,101,200,4,9,14,3,9,30,14,90,1,20,
Number of Swaps in Shell sort 47 30,74,48,30,14,90,1,20,30,74,9,5,36,1,100,37,12,86,2,127,
Number of swaps in Enhanced Shell sort 31. 62,14,3,9,30,14,90,10,52,63,1,2,23,12,25,7,9,5,36,1,100,37,
C. For 35 Elements 12,86,25,7,9,5,36,1,100,37,12,86,2,127,62,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, 1,20,30,74,48,30,14,90,1,20,30,74,9,5,36,1,100,37,12,86,2,
90,1,20,30,74,48,37,40,7,101,200,4,9 127,62,14,3,9,30,14,90,10,52,63
Number of swaps in Insertion sort 225 Number of swaps in Insertion sort 21144
Number of Swaps in Shell sort 109 Number of Swaps in Shell sort 1503
Number of swaps in Enhanced Shell sort 56. Number of swaps in Enhanced Shell sort 928.

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

1000 Fig. 3 Elements-Swap Ratio

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

to decrease the number of comparisons as well as number of


swaps to a minimum level in shortest possible time, thus
decreasing the CPU execution time as well as saving the
system memory.
Enhanced shell sort algorithm offers least number of swaps
on any size of data. The efficiency and working of this
algorithm improves as the size of data grows.
This algorithm has clearly stated simple and easy formula
that calculates the values of ‘h’ in shorter possible time than
shell sort algorithm, regardless of number of elements in an
array.
This algorithm improves the performance of the existing
algorithms up to 60% in some cases. However, in other cases
it produced better results in terms of reducing the number of
swaps than the existing algorithms. Comparison with existing
algorithms is given in the discussion section.
It has been observed that investing on the computer
hardware has less significance as compared to investment on
the improvement of algorithms in order to improve efficiency.
The improved shell sorting algorithms ensure that the number
of swaps are reduced in this case and hence provide an
efficient way of execution resulting in less computing
resources and quick execution.

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

You might also like