Sorting Algorithms A Comparative Study1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

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

net/publication/315662067

Sorting Algorithms – A Comparative Study

Article  in  International Journal of Computer Science and Information Security, · December 2016

CITATIONS READS
7 17,934

3 authors, including:

Muhammad Idrees
Government College University Faisalabad
5 PUBLICATIONS   8 CITATIONS   

SEE PROFILE

Some of the authors of this publication are also working on these related projects:

Computational Intelligence for Software Engineering View project

All content following this page was uploaded by Muhammad Idrees on 27 March 2017.

The user has requested enhancement of the downloaded file.


International Journal of Computer Science and Information Security (IJCSIS),
Vol. 14, No. 12, December 2016

Sorting Algorithms – A Comparative Study


Naeem Akhter, Muhammad Idrees, Furqan-ur-Rehman
[email protected], [email protected], [email protected]

Abstract – Sorting is nothing but alphabetizing, scheduling are examples of sorting. Thomas
categorizing, arranging or putting items in an Cormen, Charlese Ronald, Clifford Stein and
ordered sequence. It is a key fundamental Rivest Leiserson (2001) state “An algorithms is
operation in the field of computer science. It is any well-defined computational procedure that
of extreme importance because it adds takes some value, or set of values, as input and
usefulness to data. In this papers, we have produces some value or set of values as
compared five important sorting algorithms output”[2]. A number of sorting algorithms are
(Bubble, Quick, Selection, Insertion and Merge). available with pros and cons. Alfred Aho, John
We have developed a program in C# and Hopcroft and Jeffrey Ullman (1974) has
experimented with the input values 1-150, 1-300 classified algorithms on the basis of
and 1-950. The performance and efficiency of computational complexity, number of swaps,
these algorithms in terms of CPU time stability, usage of extra resources and recursion
consumption has been recorded and presented in [3]. The items to be sorted may be in various
tabular and graphical form. forms i.e. random as a whole, already sorted,
very small or extremely large in numer, sorted in
Key words: Quick Sort, Insertion Sort, Bubble reverse order etc. There is no algorithm which is
Sort, Merge Sort, Selection Sort, stopwatch. best for sorting all types of data. We must be
familiar with sorting algorithms in terms of their
1. Introduction suitability in a particular situation. In this paper
Arranging or sorting things or items is not an we are going to compare five (Bubble, Quick,
overnight development. Its footprints can be Insertion, Selection and Merge) sorting
traced back in 7th century BCE. Abdul Wahab algorithms for their CPU time consumption on a
and O.Issa (2009) state that King of Assyria given input in best, worst and average cases.
used the idea of arranging clay tablets for Royal Rest of the paper comprises of: 2. Related Work
Library sorted according to their shape[1]. 3. Methodology 4. Experiments 5. Results and
Sorting is not a jaguar leap but it has emerged in Discussion
parallel with the development of human mind. In 6. Conclusion 7. Future Work 8. References.
computer science, alphabetizing, arranging,
categoring or putting data items in an ordered 2. Related Work
sequence on the basis of similar properties is Jehad Alnihoud and Rami Mansi (2010) state
called sorting. Sorting is of key importance that sorting is graded as a fundamental problem
because it optimizes the usefulness of data. We in computer science[4]. Sonal Beniwal and
can observe plenty of sorting examples in our Deepti Groover (2013), after comparing Bubble,
daily life, e.g. we can easily find required items Heap, Insertion, Merge and Quick sort
in a shopping maal or utility store because the algorithms have concluded that quality of a good
items are kept categorically. Finding a word sorting algorithm is not only the speed but other
from dictionary is not a tideous task because all factors like, length, code complexity, stability,
the words are given in sorted form. Similarly, performance consistency and data type handling
finding a telephone number, name or address should also be taken into consideration[5]. Rohit
from a telephone directory is also very easy due Joshi etc (2013) in [6] have analyzed the time
to the blessings of sorting. In computer science, complexity of Bucket Sort, Counting Sort, Radix
sorting is one of the most important fundamental Sort on input 1,2,3….10 and concluded that non-
operations because of its pivotal applications. comparison based algorithms are better by O(n)
Priority scheduling and shortest job first instead of comparison based O(n log n). Aliyu

Page ‐ 1 
 
930 https://sites.google.com/site/ijcsis/
ISSN 1947-5500
International Journal of Computer Science and Information Security (IJCSIS),
Vol. 14, No. 12, December 2016

Ahmed and Dr. Zirra (2013) in [7] have Merge, Quick and Heap sort algorithms are
compared Insertion and Quick sort algorithms faster than the remaining two when the input
on both integer and character arrays concluded size is very large. In [12] Pankaj Sareen (2013)
that performance of insertion short is better than has compared Bubble, Insertion, Selection,
quick sort. Both algorithms are for sorting small Merge and Quick sort algorithms on the input
number of items. CPU time consumption while values 10, 100, 1000 and 10000. The
sorting integer arrays is very low as compared to comparison was based on average case only.
character arrays. In [8] Nidhi Chhajed and The average running time of all the algorithms
Simarjeet Singh Bhatia (2013) have compared was noted on these inputs and presented
Quick, Heap and Insertion Sort algorithms in graphically. It was concluded that the most
terms of time complexity and various efficient algorithm was Quick Sort. In [13 & 14]
performance factors. Random numbers between several studies (Thomas Cormen, et. al. 2009;
10,000 to 30,000 have been used as input data. Juliana Pena Ocampo, 2008) suggest state that
They have concluded that insertion sort all algorithms perform O(n2) in their worst case,
algorithm is slower than heap and quick sort. All Quick Sort is the only algorithm which have
of them have worst case time complexity as N2. average and best runtime of O(nLogn). It means
But quick sort performed better than the other that Quick Sort is better than Insertion, Bubble
two. As the input increases insertion sort and Selection sort in average case, for
performs very poorly and shows exponential sufficiently large input.
growth. In [9] Ashutosh Bharadwaj and
Shailendra Mishra (2013) have compared 3. Methodology
Insertion, Bubble, Selection, Merge and Index Empirical comparison is always machine
Sort algorithms on 10, 100 and 10001000 inputs dependent. It is essential to explicitly describe
and concluded that performance of Index Sort is the machine used for experiments in particular to
better on all input values. It consumes lesser facilitate the researchers who intend to reverify
CPU time than others on small input. It takes the results. A program developed in C# has
more CPU time than Selection, Merge and been used for calculation of CPU time taken by
Bubble Sort for larger input. Jehad Hammad each algorithm. The data set used for this
(2015) states in his comparative study on purpose consists of 1-150, 1-300, and 1-950 in
HornerEval, Linear Search, Towers, Binary best, worst and average cases. The program was
Search, Insertion, Max, Min, MaxMin, Merge, executed on Windows 7 (64 bit), Service Pack 1,
Quick, SelectionSort, Heap, Bubble and Gnome Computer used for this purpose was CPU T7100
Sorting algorithms on 5000, 10000, 20000 and @ 1.80 GHz, Intel(R) Core(TM)2 Duo. Memory
30000 input values that Gnome sorting installed was 2.00 GB. Consumption of CPU
algorithm is the quickest in best case. Selection time from all the algorithm was noted using
sort is quicker than bubble sort and gnome sort Stopwatch after running the program on all the
on random data. He has further analyzed a inputs. The results were calculated after
drawback of selection sort which continues tabulation and then their graphical representation
sorting the items it they are already arranged, was developed using MS Excl.
while gnome and bubble sort algorithms swap
the items if required[10]. In [11] Gaurav Kumar 4. Experiments & Results
etc (2013) have compared Bubble, Insertion, Stopwatch provides high accuracy when used for
Quick, Merge and Heap sorting algorithms on comparison of algorithms for their efficiency.
100000, 300000, 500000, 700000, 1000000, But it is not 100% accurate. In [15] Thomas
1500000 input values. After comparing the data Maierhofer (2010) states that Stopwatch may
empirically the algorithms were ranked in the provide results 25%-30% different for the same
following order on the basis of their speed. code excuted repeatedly on the same machine.
1. Merge We have run our program 5-times on the same
2. Quick input and have taken the average of 5-results to
3. Heap achieve better accuracy as in [16] Pankaj Sareen
4. Insertion (2013) has run his program five times on the
5. Bubble same input to calculate average running time.

Page ‐ 2 
 
931 https://sites.google.com/site/ijcsis/
ISSN 1947-5500
International Journal of Computer Science and Information Security (IJCSIS),
Vol. 14, No. 12, December 2016

TABLE – 1 TABLE – 3
Comparison on input 1 to 150 Best Case Comparison on input 1 to 150 Average Case
Time Consumption in microseconds Time Consumption in microseconds
Algo on input Value 1-150 (Best Case) Algo on input Value 1-150 (Average Case)
1 2 3 4 5 Average 1 2 3 4 5 Average
Bubble 2260  2301  2106  2460  2377  2300.80  Bubble 4317  4070  3795  3769  4026  3995.40 
Quick 2941  3261  2897  3018  2927  3008.80  Quick 4442  4311  4205  4085  5653  4539.20 
Selection 1845  2389  2279  2003  1865  2076.20  Selection 2845  2910  2847  2791  3629  3004.40 
Insertion 1467  2018  1570  1370  1429  1570.80  Insertion 2386  2365  2565  2321  2898  2507.00 
Merge 3763  4525  4248  5695  4510  4548.20  Merge 5579  5664  5734  6183  6803  5992.60 

Graph – 1 Graph – 3
Comparison on input 1 to 150 Best Case  Comparison on input 1 to 150 Average Case
5992.60

5000 4548.2 6000.00


4500
5000.00 4539.20
4000 3995.40
3500 3008.8
4000.00
3000 3004.40
2300.8 2076.2 2507.00
2500 3000.00
2000 1570.8
1500 2000.00
1000 1000.00
500
0 0.00
Bubble Quick Selection Insertion Merge Bubble Quick Selection Insertion Merge

TABLE – 2 The results are stating that on input 1 to 150


Comparison on input 1 to 150 Worst Case Insertion Sort algorithm performed bettern than
Time Consumption in microseconds all others in best, worst and average cases.
Algo on input Value 1-150 (Worst Case) Similarly, performance of Merge Sort was noted
1 2 3 4 5 Average as worst amongst all. Performance of Bubble
Bubble 2846  2876  2724  2719  2901  2813.20 
Sort was acceptable, it remained at No. 2 in
worst and average cases. Quick Sort remained at
Quick 2946  3117  3052  3467  3298  3176.00 
No. 4 in all cases. Selection sort can be ranked
Selection 1904  1909  2262  2018  2216  2061.80  No. 2 out of 5. Following is the comparison
Insertion 1744  2108  1919  1800  1766  1867.40  graph showing each algorithms’ performance
Merge 4708  3877  4077  4081  4130  4174.60  behavior on all three cases (best, worst and
average) on input 1-150.
Graph – 2
Comparison on input 1 to 150 Worst Case Comparison Graph: 1-3
5992.60
4539.20

4174.60 6000
4548.2

4500.00
4174.60
3995.40

4000.00 5000
3176.00

3176.00
3004.40

3500.00
2813.20

3008.8

2507.00

3000.00 2813.20 4000


2300.8

2061.80
2076.2

1867.40

2500.00 2061.80
3000
1570.8

2000.00 1867.40
1500.00 2000
1000.00
1000
500.00
0.00 0
Bubble Quick Selection Insertion Merge Bubble Quick Selection Insertion Merge

Page ‐ 3 
 
932 https://sites.google.com/site/ijcsis/
ISSN 1947-5500
International Journal of Computer Science and Information Security (IJCSIS),
Vol. 14, No. 12, December 2016

TABLE – 4 TABLE – 6
Comparison on input 1 to 300 Best Case Comparison on input 1 to 300 Average Case
Time Consumption in microseconds Time Consumption in microseconds
Algo on input Value 1-300 (Best Case) Algo on input Value 1-300 (Average Case)
1 2 3 4 5 Average 1 2 3 4 5 Average
Bubble 3741  3746  3419  3582  3642  3626.00  Bubble 6774  6882  7091  7341  6935  7004.60 
Quick 4452  4159  3977  3975  4098  4132.20  Quick 4530  4632  4507  4513  4215  4479.40 
Selection 2956  2627  2699  2627  2782  2738.20  Selection 4458  4171  4195  4021  4100  4189.00 
Insertion 1323  1319  1333  1324  1616  1383.00  Insertion 3852  3505  3485  3314  3929  3617.00 
Merge 4602  5014  4334  4311  4729  4598.00  Merge 6571  7140  6305  6850  6638  6700.80 

Graph – 4 Graph – 6
Comparison on input 1 to 300 Best Case Comparison on input 1 to 300 Average Case

5000 8000 7004.6


4132.2 4598
4000 3626 6700.8
6000
2738.2 4479.4
3000 4189
4000 3617
2000
1383 2000
1000
0 0
Bubble Bubble
Quick Quick
Selection Selection
Insertion Insertion
Merge Merge

TABLE – 5 The results are verifying that on the data from 1


Comparison on input 1 to 300 Worst Case to 300 the performance of Bubble Sort remained
Time Consumption in microseconds poorest amongst all. It consumed more time than
Algo on input Value 1-300 (Worst Case) all others for best, worst and average cases.
1 2 3 4 5 Average Insertion Sort was best of the lot in best case
Bubble
(1383 microseconds) and average case (3617
5438  4959  4901  4703  4854  4971.00 
microseconds) but Selection Sort left behind
Quick 4080  4033  5582  4155  3970  4364.00  others in worst case (3019 microseconds). Quick
Selection 2974  2975  2994  3300  2852  3019.00  Sort can be ranked No. 3 out of 5. Following is
Insertion 3418  3197  3339  3372  3043  3273.80  the comparison graph of all cases (input 1-300).
Merge 5301  4491  4835  4645  5177  4889.80 
Comparison Graph: 4-6
Graph – 5
7004.6

8000
6700.8

Comparison on input 1 to 300 Worst Case 7000


4971

4971 6000
4479.4

4889.8
4132.2

4889.8
4364

5000 4364
4598
4189

5000
3626

4000
3273.8
3617
2738.2

3019 3273.8 4000


3019

3000
3000
2000
2000
1383

1000
0 1000
Bubble 0
Quick
Selection Bubble
Insertion Quick
Merge Selection
Insertion
Merge

Page ‐ 4 
 
933 https://sites.google.com/site/ijcsis/
ISSN 1947-5500
International Journal of Computer Science and Information Security (IJCSIS),
Vol. 14, No. 12, December 2016

TABLE – 7 TABLE – 9
Comparison on input 1 to 950 Best Case Comparison on input 1 to 950 Average Case
Time Consumption in microseconds Time Consumption in microseconds
Algo on input Value 1-950 (Best Case) Algo on input Value 1-950 (Average Case)
1 2 3 4 5 Average 1 2 3 4 5 Average
Bubble 21744  23575  18279  21566  19441  20921  Bubble 44499  57172  45139  44252  51804  48573.20 
Quick 17857  18492  17946  18682  19954  18586  Quick 5013  5609  5332  5034  5403  5278.20 
Selection 11998  12301  12884  12054  12414  12330  Selection 17906  18356  17925  18104  18199  18098.00 
Insertion 1369  1812  1639  1400  1465  1537  Insertion 15427  25973  26047  15326  15985  19751.60 
Merge 15137  15161  11863  15196  15399  14551  Merge 17672  23301  17500  17310  16836  18523.80 

Graph – 7 Graph – 9
Comparison on input 1 to 950 Best Case  Comparison on input 1 to 950 Average Case
25000 50000 48573.2
20921
18586.2
20000 40000

15000 14551.2 30000


12330.2
20000 19751.6
10000 18098
18523.8
5000 10000 5278.2
1537 0
0
Bubble Bubble Quick
Quick Selection
Selection Insertion
Insertion Merge
Merge

TABLE – 8 The results are presenting the facts that on the


Comparison on input 1 to 950 Worst Case input from 1 to 950 Bubble Sort consumed most
Time Consumption in microseconds time than all other competitors in all cases. It
Algo on input Value 1-950 (Worst Case) took 21921.20, 36539.40 and 48573.20
microseconds for best, worst and average cases
1 2 3 4 5 Average
respectively. Insertion Sort consumed only
Bubble 34646  38896  36366  33158  39631  36539.4  1570.80 micrseconds in best case (least than all
Quick 16995  17836  18877  16575  18050  17666.6  others). In worst case, Selection Sort performed
Selection 15569  14590  15392  14702  14508  14952.2  best, took 14952.20 microseconds. In average
Insertion 19747  20374  21884  19465  20996  20493.2  case, Quick Sort was significantly the best
Merge 11629  13399  14876  11387  12917  12841.6 
which consumed only 5278.2 microseconds.

Graph – 8 Comparison Graph: 7-9


Comparison on input 1 to 950 Worst Case 
36539.4
48573.2

40000 50000
36539.4
35000
40000
30000
20921

17666.6

20493.2

25000 30000
19751.6
18586

14952.2
18098

18523.8

20000 20493.2
17666.6 14952.2
12330

12841.6

20000
14551

15000
5278.2

10000 12841.6 10000


1537

5000
0 0
Bubble Bubble Quick
Quick Selection
Selection Insertion
Insertion Merge
Merge

Page ‐ 5 
 
934 https://sites.google.com/site/ijcsis/
ISSN 1947-5500
International Journal of Computer Science and Information Security (IJCSIS),
Vol. 14, No. 12, December 2016

5. Discussion Table-11: Ranking of Algorithms (input 1-300)


We have made a comparative study of five Input Rank Best Worst Average
sorting algorithms (Bubble, Quick, Insertion, Case Case Case
Selection and Merge) on input values 1-150, 1- 1 Insertion  Selection  Insertion 
300 and 1-950. We run our program for 2 Selection  Insertion  Selection 
1-300 3 Bubble  Quick  Quick 
calculating best, worst and average cases. After
performing experiments on our pre-defined data 4 Quick  Merge  Merge 
5 Merge  Bubble  Bubble 
set (1-150, 1-300 and 1-950) the algorithms
performed varyingly. At a small input of 1-150
Insertion Sort performed the best, Merge Sort Table-12: Ranking of Algorithms (input 1-950)
was poorest amongst all at the same input. Input Rank Best Worst Average
Performance of Bubble Sort was also not bad. It Case Case Case
1 Insertion  Merge  Quick 
performed better than Selection, Insertion and
2 Selection  Selection  Selection 
Merge sort in best case. It also performed better 1-950 3 Merge  Quick  Merge 
than Quick and Merge Sort in worst and average 4 Quick  Insertion  Insertion 
cases. Bubble Sort was proved the worst 5 Bubble  Bubble  Bubble 
algorithm when the input was increased to 1-
950. Behavior of Merge Sort is almost opposite 6. Conclusion
to Bubble Sort. It was worst of the lot at input 1- We concluded that Bubble sort is not a bad
150, it performed reasonably well on input 1- choice when input size is small (less than 100)
300. It was graded best at input 1-950 with worst because it is a simple-to-implement algorithm.
case. Selection Sort performed significantly well But as the input size increases, more efficiency
on varying inputs and turned up to remain at is required for sorting. Bubble sort is easily
position 1 or 2 on all input values in all cases. dominated by more efficienct algorithms on
There is no considerable change in the Selection larger input size. Merge Sort algorithm is more
Sort’s performance with the smaller or larger complex than Bubble Sort. It performs poorly on
input values. If we analyze the behavior of small input but enhances its performance in
Quick Sort, it was ranked 4 with input 1-150. It parallel with the increment in input size.
was ranked 4 in best case and ranked 3 in worst Insertion Sort algorithm was graded the best of
and average cases with input 1-300. When the the lot with small inputs in all cases even with
input was increased to 1-950 it was ranked 2 and large input in best case. Selection Sort was least
3 in best and worst cases respectively but effected by the variation in input values. Quick
significantly dominated all others in average Sort is complex to implement, shows poor
case. It took 4.79% of the total time consumed results on small inputs (less than 100), average
by five algorithms in average case with input 1- results on medium-sized input (more than 100
950. We have thoroughly analyzed the behavior but less than 500) but signicantly dominates
of all the algorithms on our data set in best, other algorithms in average case with large
worst and average cases. From the results inputs (more than 500).
obtained after experiments, we have ranked all
the algorithms according to their efficiency on 7. Future Work
our data set in all the cases(best, worst and 1. More sorting algorithms like Merge Insertion
average. Following is the ranking of all the sort, Odd-Even sort, Heap sort, Cycle sort,
algorithms: Cocktail sort, Timsort and Smoothsort can also
be included in the comparison using the same
Table-10: Ranking of Algorithms (input 1-150) input data with the outcome of detailed
Input Rank Best Worst Average comparative analysis.
Case Case Case 2. In this paper, we compared all sorting
1 Insertion  Selection  Selection  algorithms which are comparison-based.
2 Selection  Bubble  Bubble 
1-150 Comaparative study of the algorithms which use
3 Bubble  Merge  Merge 
comparison technique and those which don’t use
4 Quick  Quick  Quick 
5 Merge  Insertion  Insertion  this technique (Counting sort, LSD Radix sort,

Page ‐ 6 
 
935 https://sites.google.com/site/ijcsis/
ISSN 1947-5500
International Journal of Computer Science and Information Security (IJCSIS),
Vol. 14, No. 12, December 2016

MSD Radix sort and Bucket sort, etc) can be [9] Bharadwaj, A., & Mishra, S. Comparison
carried out. of Sorting Algorithms based in Input
3. Performance comaprison of the sorting Sequences, International Journal of
algorithms can be made on the same input with Computer Applications, Vol. 78, No. 14
integers and characters separately. All of them (2013).
can be ranked according to their efficiency on
[10] Hammad, J. A Comparative Study between
both type of inputs separately with analysis of
Various Sorting Algorithms, International
their behavior on varying input size.
Journal of Computer Science and Network
Security (IJCSNS), Vol 15, No. 3 (2015).
8. References:
[1] Wahab, A., Issa, O.A. Fundamentals of [11] Kumar, G. & Ghugh, H. Empirical Study of
Library & Information Sciences, (1st ed.). Complexity Graphs for Sorting Algorithms,
Cataloguing-in-Publication Data, Ilorin International Journal of Computer,
Communication and Information
(2009).
Technology (IJCCIT), Vol. 1, No. 1 (2013).
[2] Cormen, T.H., Leiserson, C.E., & Rivest, [12] Sareen, P. Comparison of Sorting
R.L. Introduction to Algorithms (2nd ed.). Algorithms (On the Basis of Average
Prentice Hall of India private limited, New Time), International Journal of Advanced
Delhi-110001 (2001).  Research in Computer Science & Software
Engineering, Vol. 3, Issue 3 (2013).
[3] Aho A., Hopcroft J., and Ullman J. The
Design and Analysis of Computer [13] Cormen, T. H., Leiserson, C.E., & Rivest,
Algorithms, Addison Wesley (1974). R.L. Introduction to Algorithms (3rd ed.).
[4] Alnihoud, J., & Rami, R. An Enhancement MIT Press and McGraw-Hill (2009). 
of Major Sorting Algorithms, The [14] Pena, J. O. An empirical comparison of the
Intational Arab Journal of Information runtime of five sorting algorithms,
Technology, Vol. 7, No. 1, January (2010). International Baccalaureate Extended Essay
[5] Ben, S. & Deepti, Gr. Comparison of (2008).
Various Sorting Algorithms: A Review, [15] Thomas Maierhofer Performance Tests:
International Journal of Emerging Research Precise Run Time Measurements with
in Management & Technology, Vol., 2,
System.Diagnostics.Stopwatch. (2010),
Issue 5 (2013).
[6] Joshi, R., Panwar, G.P., Pathak, P. (2013). Retrieved from
Analysis of Non-Comparison Based http://www.codeproject.com/Articles/6196
Sorting Algorithms: A Review, 4/Performance-Tests-Precise-Run-Time-
International Journal of Emerging Research Measurements-wi
in Management & Technology.
[16] Sareen, P. Comparison of Sorting
[7] Ahmed M. Aliyu, Dr. P.B. Zirra. A Algorithms (On the Basis of Average
Comparative Analysis of Sorting Time), International Journal of Advanced
Algorithms on Integer and Character Research in Computer Science & Software
Arrays, The International Journal of Engineering, Vol. 3, Issue 3 (2013).
Engineering and Science (IJES), Vol 2,
Issue 7 (2013).
[8] Chhajed, N., & Bhatia, S.S. A Comparison
Based Analysis of Different Types of
Sorting Algorithms with their
Performances, Indian Journal of Research
Paripex, Vol. 2, Issue. 3 (2013).

Page ‐ 7 
 
936 https://sites.google.com/site/ijcsis/
ISSN 1947-5500
View publication stats

You might also like