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

Analysisof Shellsort Algorithms

Uploaded by

Ricardo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Analysisof Shellsort Algorithms

Uploaded by

Ricardo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

See discussions, stats, and author profiles for this publication at: https://www.researchgate.

net/publication/333917544

ANALYSIS OF SHELLSORT ALGORITHMS

Article · June 2019


DOI: 10.26483/ijarcs.v10i3.643

CITATIONS READS

0 4,398

3 authors, including:

Avik Mitra Jash Kothari


Indian Statistical Institute Kolkata Heritage Institute of Technology
12 PUBLICATIONS 17 CITATIONS 1 PUBLICATION 0 CITATIONS

SEE PROFILE SEE PROFILE

All content following this page was uploaded by Avik Mitra on 21 June 2019.

The user has requested enhancement of the downloaded file.


DOI: http://dx.doi.org/10.26483/ijarcs.v10i3.6433
ISSN No. 0976-5697
Volume 10, No. 3, May-June 2019
International Journal of Advanced Research in Computer Science
RESEARCH PAPER
Available Online at www.ijarcs.info
ANALYSIS OF SHELLSORT ALGORITHMS

Avik Mitra Jash Kothari


BCA Department Final Year Student, BCA
The Heritage Academy The Heritage Academy
Kolkata, India Kolkata, India

Annesa Ganguly
Final year student, BCA
The Heritage Academy
Kolkata, India

Abstract: Shellsort is a comparison sort that uses insertion sort at each iteration to make a list of interleaved elements nearly sorted so that at the
last iteration the list is almost sorted. The time complexity of Shellsort is dependent upon the method of interleaving (called increment sequence)
giving variants of Shellsort. However, the problem of finding proper of interleaving to achieve the minimum time complexity of O(n log n) is
still open. In this paper, we have analyzed the performance of variants of Shellsort based on their time complexity. Our measure of time
complexity is independent of the machine configuration and considers all the operations of a sorting process. We found that the interleaving
method or increment sequence proposed by Sedgewick performs best among the analyzed variants.

Keywords: Shellsort; increment sequence; variants; survey; time complexity; algorithm; data structure

I. INTRODUCTION A[ z + SkipLength[i] ] = Temp


Shellsort [1] is an in-situ comparison sort algorithm where
at each iteration, each list of interleaved elements from the list }//End of For-loop.
A[0, ..., n], are sorted by insertion sort; each list of interleaved
elements forms disjoint sets of elements. The interleaving is }//End of For-loop
reduced in subsequent iteration, until it becomes 1, in which
case insertion sort gets applied to the whole, now nearly sorted, }//End of Shellsort
list A. The sorting algorithm is oblivion to the data [2] and one
of its implementation is (sorting in non-decreasing order): The sequence of interleaving (h1, h2, ..., hk) with h1>h2> ...>
hk=1, is called increment or offset sequence and each hi is
called skip length. For [1], the increment sequence is (
Void Shellsort (A[0, ..., n-1], SkipLength[0, ...., k-1]) ⌊ n/2 ⌋ , ⌊ n/4 ⌋ , … , 1). There are many variants of the
//SkipLength[x] > SkipLength[x+1] original Shellsort algorithm depending upon the increment
sequence [2]-[15]; the time complexity of the algorithm is also
//SkipLength[k-1] = 1, SkipLength[i] = hi+1 dependent upon the increment sequence. Finding optimal
increment sequence that will minimize the time complexity of
{ the Shellsort is still an open problem [12]. In addition to this,
data-oblivion property of the sorting algorithm makes it an
For i = 0 to (k-1), step by (+1), do: //For each skip length attractive solution for deployment in those systems where a
dataset, distributed over multiple nodes in a network, needs to
{ be arranged in certain order. In this paper we will compare the
Shellsort variants in terms of the time complexities. Unlike
For j = 0, j < SkipLength[i], step by (+1), do: [16]-[18] that compared the shellsort variants based on a
parameter that includes number of swaps or number of
{ comparisons, we have defined a parameter that have included
these factors and also the time consumed in checking
Temp = A[j]. //Assignment operation – A[j] is assigned conditions for loop to run. We believe that our parameter for
to Temp. comparison is closer to the general definition of theoretical
time complexity of an algorithm. The rest of this paper is
For z = j – SkipLength[i], z ≥ 0 and A[z] > Temp, step organized as follows: section II will make a brief description of
by (-SkipLength[i]), do: the variants of Shellsort. The framework used to make the
comparison among the Shellsort variants and findings are
{ discussed in section III. We conclude our paper in section IV,
followed by references.
A[z + Skiplength[i] ] = A[z]

} //End of For-loop

© 2015-19, IJARCS All Rights Reserved 48


Avik Mitra et al, International Journal of Advanced Research in Computer Science, 10 (3), May-June 2019,48-50

II. SURVEY OF SHELLSORT VARIANTS complexity Ω(n5/3) (2) (n1/2, n1/4, 1) with time complexity
O(n3/2) (3) (n11/16, n7/16, n3/16, 1) with time complexity Ω(n 21/16).
At i-th iteration of Shellsort, list A gets subdivided into hi 1 1
sublists each of size ⌊ n/ hi ⌋ [12], and insertion sort is applied [13] with increment sequence ( ⌈ n +1⌉ , ⌈ n ⌉ , 1) obtained
3 3

to each of these lists. So, the time complexity of the sorting time complexity of O(n 5/3). Genetic algorithm used in [14] to
algorithm depends upon the time complexity of sorting each of generate two increment sequences based on the size of the list
the sublists. Moreover, since each of these sublists gets sorted A: 7-tuple increment sequence is used when size of A is
resulting partially sorted list, therefore, at subsequent iterations between 1000 and 1 million; 15-tuple increment sequence is
it is expected that there will be less swaps than the number of used when size of A exceeds 1 million. [15] uses binary search
comparisons. The sequence proposed by Shell [1] uses algorithm for each skip length so that the time complexity of
⌊ log n ⌋ length sequence. The time complexity in worst case the algorithm becomes O(n log n); the algorithm implicitly
is proved to be O(n2) when n is a power of 2. To reduce the uses reverse of the increment sequence
time complexity, [3] proposed that even skip length should be
9 hi
replaced by next odd number, resulting time complexity of hi =⌈ {9(9 /4)i−4 } /5 ⌉ such that < n∧i ≥ 0. The
O(n3/2). [4] also achieved the same worst-case complexity using 4
n n discussed variants did not achieve the lower bound
the increment sequence ( + 1,... ⌊ log n ⌋ + 1, 1). [5] obtained
2 2 Θ(n logn) [20] of a comparison sort algorithm. During
3 writing of this paper this bound is probabilistically achieved
a tighter bound of Θ(n ) using reverse of the following
2
by [2] and [17] proved that to achieve the lower bound the
generated sequence (note that mod represents modular length of the increment sequence will be Θ ¿, which is yet to
operation): be found.
Most of the time complexities of the Shellsort variants have
considered either number of swaps or number of comparisons,
⌊log (n−1) ⌋
For i =1 to 2 ; i = i*2 //Double value of i at each except [14] and [21] which have additionally used a
iteration specialized machine for actual time taken. However, during
run of algorithm associated variables like use of counter
{ variables and temporary variables, using instruction for
increment or decrement etc., adds up the time complexity and
J=i their contribution to the time complexity is proportional to the
number of times a loop, using these, runs. In the next section
Do{ Store J // Note J we define a parameter measuring of time complexity where we
include these factors and using this parameter we make
J = (3*J)/2 //Integer division
compare the performances of the Shellsort variants.
} while ( J mod 3 == 0 and J < n) //End of Do-while loop
III. COMPARATIVE ANALYSIS OF SHELLSORT
}//End of For-loop VARIANTS

We first define the parameter for the comparison followed by


the methodology for comparison and results obtained.
The problem of moving an element to its rightful place is
reduced to Frobenius problem [19] by [6] where it is derived A. Defining Complexity
that at each iteration, the skip length should not be a linear Time complexity of an algorithm B, C(B), consisting (I1, ... It)
combination of the skip length of the next iterations, thus instructions running (n1, ... nt) times respectively can be
avoiding unnecessary relocation of same element in the list; defined as:
this resulted better time complexity of O(n 5/4). Time t
complexity of 3-tuple increment sequence (h, k, 1) is studied C ( B )=∑ ( I ¿ ¿ j× n j) ¿ .
in [7] where the exact time complexities for each of the three j=1
iterations are derived. [8] proposed the increment sequences – .. (1)
hk =1 and hi=3hi+1 + 1 where h1 is such that 3h0 ≥ n, and (2, 1), We can, therefore, measure C(B) by using a global variable
whereas, [9] proposed the reverse of following increment which is initially set to 0; the variable gets incremented by one
sequence (3hi ≥ n) using the Frobenius problem: for each execution of the instructions.
i

hi =
{ i

8.2i−6. 2
i+1
2
2
9.2 −9. 2 +1 if s is even

+1 if s isodd
The definition (1) includes the number of comparisons,
number of exchanges and the number of times associated
variables are used. The definition is also independent of
underlying platform used for implementation of the algorithm
B.
Using this increment sequence, O(n 4/3) time complexity is
achieved. 3-tuple increment sequence for Shellsort are again B. Methodology for Comparison
explored in [10] where the increment sequence (n7/15, n1/5, 1) is We compare average case complexities of the Shellsort
proposed, getting a time complexity of O(n 23/15). [11] proposed variants. We have selected the variants [1][3]-[5][7]-[13] and
increment sequence where hi = 2i-1 (i ≥ 1) until hi ≥ n; the [15]. We have not taken [2] as it is a probabilistic algorithm
achieved time complexity is almost same as in [3]. [12] where sorting is not guaranteed (though the probability of
analyzed three increment sequences: (1) (n1/3, 1) with time getting a sorted list is very high). The increment sequence

© 2015-19, IJARCS All Rights Reserved 49


400000 DLShell
PapernovSta-
sevich
350000 LazarusFrank
Avik Mitra et al, International Journal of Advanced Research in Computer Science, 10 (3), May-June 2019,48-50
Pratt
generation of [6] is complex as it requires checking co-prime 300000 ACCYao
of two numbers, thus it can take more time to generate the Knuth1
increment sequence than actual sorting if the size of list to be Knuth2
250000 Sedgewick
sorted is high. Therefore we have not considered the variant in
our analysis. Based on similar reason (time complexity of JansonKnuth
genetic algorithms are generally high), [14] is also not 200000 Hibbard
considered. Vitnayi1
We have implemented the selected Shellsort variants in Vitnayi2
Java programming language as generating random list of 150000 Vitnayi3
elements is easier in it. Each of the variants is implemented in a
Weiss
separate class. Objects are created for each of these variants in
100000 Tokuda
the main method and executed. During implementation we
have not considered the time complexity of increment sequence
generation as we are more inclined towards the time
complexity of sorting (section I). To find time complexity of 50000
each Shellsort variant and size n of the list to be sorted, 1000
random lists are generated, the average of these 1000 runs are
0
taken. n is varied from 500 to 10000 with interval of 500, that
is, 20 values of n is taken. To ensure fairness of analysis among 500 2000 3500 5000 6500 8000 9500
the selected variants, same 1000 lists for given n is fed into all
the variants. Therefore, our analysis has used same 20 × 1000 =
20000 lists, for all the variants.
C. Results
We have plotted the obtained average of average-case time Figure 1. Comparison of Shellsort variants
complexity in vertical axis and size of dataset in horizontal
axis. The mapping of the labels used in plots is given in table I. IV. CONCLUSION
The variation of time complexity with lists’ size is shown in
figure 1. To avoid overflow during measuring the time We have compared average case time complexity of
complexity of a variant, the variable used for the measure is variants of Shellsort algorithm and observed that the variants
incremented by 0.010 for a run of an observed instruction. So, that uses increment sequences of length close to 1 tends to
the time complexities in figure are scaled down by 100. perform worse than those variants whose length of increment
From figure 1, it can be observed that the second increment sequences is proportional or close to log n. Based on this fact
sequence in [8] performed worst as it has used only two skip- increment sequence proposed by Sedgewick performed best
lengths and hence it nearly reduced to insertion sort which has among the other variants that we have considered. Therefore,
time complexity O(n2). [9] performed best among the selected we suggest use of Sedgewick’s increment sequence for use in
variants since: (1) the possibility that two consecutive skip practical field.
length in increment sequences are co-primes is high, thereby
avoiding unnecessary movement of an element of list; (2) the V. REFERENCES
number of skip lengths for given n is close to log n as
suggested by [17]. On similar reason, [1] and [15] (though [15] [1] D.L.Shell, “A High Speed Sorting Procedure”, Communications
performed better than [1]) performed nearly as good as [9]. of the ACM, volume 2, issue 7, pp 30-32, July 1959. DOI:
10.1145/368370.368387.
Table I. Labelling of Increment Sequences for Plot [2] Michael T. Goodrich, “Randomized Shellsort: A Simple Data-
Increment Sequence Label Oblivious Sorting Algorithm”, Journal of the ACM, volume 58,
[1] DLShell issue 6, 2001.
[4] PapernovStasevich [3] R.M.Frank, R.B. Lazarus, “A High Speed Sorting Procedure”,
[3] LazarusFrank Communications of the ACM, volume 3, issue 1, pp 20-22,
[5] Pratt January 1960.
[7] ACCYao
[4] A.A.Papernov and G.Stasevich, “A Method of Information
[8] Knuth1, Knuth2
Sorting in Computer Memories”, Problems in Information
[9] Sedgewick
Transmission, volume 1, issue 3, pp 63-75, 1965.
[10] JansonKnuth
[11] Hibbard [5] V.R.Pratt, “Shellsort and Sorting Networks”, No. Stan-CS-72-
[12] Vitnayi1, Vitnayi2, Vitnayi3 260, Standford University CA Department of Computer Science,
[13] Weiss February 1972. Available from:
[15] Tokuda https://apps.dtic.mil/dtic/tr/fulltext/u2/740110.pdf
[6] Janet Incerpi and Robert Sedgewick, “ Improved Upper Bounds
on Shellsort”, Journal of Computer and System Sciences,
volume 31, issue 2, pp 210-224, October 1985.
[7] Andrew Chi-Chih Yao, “An Analysis of (h, k, 1) – Shellsort”,
Journal of Algorithms, volume 1, issue 1, pp 14-50, March 1980.
[8] Donald. E. Knuth, “Art of Computer Programming: volume 3
Sorting and Searching”, Second Edition, Addison-Wesley, 1998.
[9] Robert Sedgewick, “A New Upper Bound for Shellsort”, Journal
of Algorithms, volume 7, issue 2, pp 159-173, June 1986.

© 2015-19, IJARCS All Rights Reserved 50


Avik Mitra et al, International Journal of Advanced Research in Computer Science, 10 (3), May-June 2019,48-50

[10] Svante Janson and Donald E. Knuth, “Shellsort with Three [16] Bronislava Brejova, “Analyzing Variants of Shellsort”,
Increments”, Random Structures and Algorithms, volume 10, Information Processing Letters, volume 79, issue 5, pp 223-227,
issue 1, pp 125-142, January 1997. September 2001.
[11] Thomas N. Hibbard, “An Empirical Study of Minimal Storage [17] Tao Jing, Ming Li, Paul Vitanyi, “Average-case Complexity of
Sorting”, Communications of the ACM, volume 6, issue 5, pp Shellsort”, International Colloquium on Automata, Languages
206-213, 1963. and Programming, 1999.
[12] Paul Vitnayi, “On the Average-case Complexity of Shellsort”, [18] Janet Incerpi and Robert Sedgewick, “Practical Variations of
Random Structures and Algorithms, volume 52, issue 2, pp 354- Shellsort”, Doctoral Dissertation, INRIA, 1986.
363, 2018. [19] J.L.Ramirez-Alfonsin, “Complexity of the Frobenius Problem”,
[13] M.A. Weiss, “Shellsort with Constant Number of Increments”, Combinatorica, volume 16, issue 1, pp 143-147, March 1996.
Algorithmica, volume 16, issue 6, pp 649-654, December 1996. [20] Thomas H. Cormen, Charles E Leiserson, Ronald R. Rivest,
[14] Richard Simpson, Shashidhar Yachavaram, “Faster Shellsort Clifford Stein, “Introduction to Algorithms”, 3rd Edition, MIT
Sequences: A Genetic Algorithm Application”, Computers and Press and Prentice Hall of India, February 2010.
Their Applications, 1999. [21] D. Ghoshdastidar and Mohit Kumar Roy, “A Study on the
[15] Naoyuki. Tokuda, “An Improved Shellsort”, IFIP 12th World Evaluation of Shell’s sorting Technique”, The Computer
Computer Congress on Algorithms, Software, Architecture – Journal, volume 18, issue 3, pp 234-235, 1975.
Information Processing, 1992.
[22]

© 2015-19, IJARCS All Rights Reserved 51

View publication stats

You might also like