Professional Documents
Culture Documents
HTTPS://SITES.GOOGLE.COM/SITE/JOURNALOFCOMPUTING/
WWW.JOURNALOFCOMPUTING.ORG 193
Abstract— Quicksort is an algorithm that is most suitable for average case scenarios. It has been refined to the extent that it is a sorting
algorithm of choice in a wide variety of practical sorting applications. The most efficient refinement of Quicksort for average case scenario is
the Median-of-Three Sort. There are, however, some average case scenarios in which Median-of-Three Sort algorithm is not so efficient.
This paper presents an Improved Median-of-Three Sort which uses a modified diminishing increment sorting for a better performance in these
situations. The results of the implementation and experimentation of this approach compared with Quicksort and Median-of-Three Sort shows
that the Improved Median-of-Three Sort is more efficient in these scenarios.
.
Index Terms— Quicksort, Median-of Three Sort, Improved Median-of-Three Sort, Average Case.
—————————— ——————————
1 INTRODUCTION
Q uicksort was invented by C. A. Horare in 1960. It is
popular because it is not difficult to implement,
works well for a variety of different kinds of input
sub‐sequences diminish until elements with sub‐
sequences 1 are sorted. The approach employed by Shell
sort is, however, modified in this paper to effectively cater
data, and consumes fewer resources than any other sort‐ for these kinds of average‐case scenarios, as has been
ing method in many situations [1]. The algorithm is also used to increase the efficiencies of Shell sort [5],[ 6].
an in‐place sorting algorithm. It is the fastest known ge‐
neric algorithm in practice [2]. Its worst‐case running time
2. SORTING ALGORITHMS
is, however, O(n2) on an input array of n numbers. In
spite of this slow worst‐case running time, Quicksort is Given a list of input elements or objects, sorting arranges
often the best practical choice for sorting because it is re‐ the elements either in ascending order or descending or‐
markably efficient on the average [3], [4]. An average‐case der and produces a sorted list as the output. The elements
scenario is one in which the elements to be sorted are to be sorted need to be stored in a data structure for ma‐
somehow partially sorted. nipulation. Among the various data structures usually
used for sorting are: arrays, linked list, heap, etc. Sorting
Though the algorithm has been improved to handle aver‐
age‐case sorting scenarios more efficiently, there are, can either be internal or external. Internal sorting is the
however, some kinds of average‐case scenarios that it, type of sorting that requires all the elements to be sorted
and its improved variant, Median‐of‐Three cannot han‐ to be in the main memory throughout the sorting process
dle efficiently. This paper presents an Improved Median‐ while an external sorting allows part of the elements to be
of‐Three Sort which uses a modified diminishing incre‐ sorted to be outside the main memory during the sorting
ment sorting to efficiently sort elements that have the cha‐ process [7]. Examples of internal sorting algorithms are:
racteristics of these kinds of average case scenarios identi‐ Insertion Sort, Selection Sort, Bubble Sort, Shellsort,
fied. Diminishing increment sorting algorithms like Shell
Quicksort, etc.
sort, divide the sequence of elements to be sorted into
sub‐sequences such that from one pass to the other, the
There is no known “best” way to sort; there are many best
———————————————— methods, depending on what is to be sorted, on what ma‐
M. O. Oyelami is with the Department of Computer and Information chine and for what purpose [8]. What needs to be done is
Sciences, Covenant University, Ota, Nigeria. to learn the characteristics of each sorting algorithm and
I. O. Akinyemi is with th Department of Computer and Information
Sciences, Covenant University, Ota, Nigeria make a good choice for a particular problem.
JOURNAL OF COMPUTING, VOLUME 3, ISSUE 4, APRIL 2011, ISSN 2151-9617
HTTPS://SITES.GOOGLE.COM/SITE/JOURNALOFCOMPUTING/
WWW.JOURNALOFCOMPUTING.ORG 194
entire list. This approach produces a lesser number of written, an accurate analysis of the time and space re‐
comparisons and swappings for lists that are partially quirements cannot be made. Experimental method deals
sorted and that exhibit the characteristics below: with actually performing experiment and measuring the
i. A list in which the elements are repeated, but in space and time used by the program. Two manageable
which they are also decreasing. For example, a approaches to estimating run time are [13]:
list of the form 9, 9, 8, 8, 7, 7, 6, 6, 5, 5, 4, 4, 3, 3, 1,
1. i. Identify one or more key operations and determine the
ii. A list in which the elements are decreasing and number of times they are performed;
also increasing but where the increasing elements ii. Determine the total number of steps executed by the
are all greater than the decreasing elements. For program.
example, a list of the form 30, 29, 28, 27, 26, 25,
24, 23, 1, 2, 3, 4, 5, 6, 7, 8. 5. PERFORMANCE ANALYSIS OF IM-
PROVED MEDIAN-OF-THREE SORT AND
4. PERFORMANCE ANALYSIS OF ALGO- THE RESULTS OBTAINED
RITHMS
Median‐of‐Three sorting which makes use of Insertion
The most important attribute of a program/algorithm is Sort for elements size ≤ 20 was implemented together
correctness. An algorithm that does not give a correct with the Improved Median‐of‐Three sort in C++ using
output is useless. Correct algorithms may also be of little Dev C++ compiler on an Intel Celeron M microcomputer
use. This often happens when the algorithm/program running Windows VistaTM Ultimate. The key operations in
takes too much time than expected by the user to run or sorting algorithms are comparison and swapping. The
when it uses too much memory space than is available on approach employed in comparing the Improved Median‐
the computer [11]. Performance of a program or an algo‐ of‐Three Sort with Median‐of‐Three Sort was to compute
rithm is the amount of time and computer memory the number of comparisons and swappings carried out by
needed to run the program/algorithm. Two methods are the two algorithms. The analysis was based on lists of
normally employed in analyzing an algorithm: elements of the form:
i. Analytical method i. 9,9,8,8 3,3,1,1
ii. Experimental method ii. 30, 29 28, 27, 12, 13, 14, 15
The results obtained are as presented in the table be‐
In analytical method, the factors the time and space re‐ low for the list that contains elements of the first form
quirements of a program depend on are identified and above:
their contributions are determined. But since some of
these factors are not known at the time the program is
Table 1: Performance Comparison of Median‐of‐Three Sort and Improved Median‐of‐Three Sort for the First Form
of Average Case Scenario
Size of Input Median‐of‐Three Sort Improved Median‐of‐Three Sort
Number of Com‐ Number of Swap‐ Number of Compar‐ Number of Swap‐
parisons pings isons pings
30 31 18 24 18
50 79 42 68 40
100 197 100 169 97
500 2021 997 1875 995
1000 4943 2440 4621 2423
For the list that contains elements of the second form, the table below shows the results.
JOURNAL OF COMPUTING, VOLUME 3, ISSUE 4, APRIL 2011, ISSN 2151-9617
HTTPS://SITES.GOOGLE.COM/SITE/JOURNALOFCOMPUTING/
WWW.JOURNALOFCOMPUTING.ORG 197
Table 2: Performance Comparison of Median‐of‐Three Sort and Improved Median‐of‐Three Sort for the Second
Form of Average Case Scenario
Size of Input Median‐of‐Three Sort Improved Median‐of‐Three Sort
Number of Com‐ Number of Swap‐ Number of Com‐ Number of Swap‐
parisons pings parisons pings
30 31 18 20 18
50 86 48 63 46
100 198 108 132 94
500 1097 601 652 442
1000 2230 1229 1312 881
[2] Mark Allen Weiss, Data Structures and Algorithm Analysis in
6 DISCUSSION C++. Pearson Education. Inc., 2006.
[3] Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest and
The first table shows that Improved Median‐of‐Three Sort Clifford Stein, Introduction to Algorithms. The Massachusetts In‐
is more efficient than the Median‐of‐Three Sort for lists stitute of Technology, 2003.
having elements having the characteristics of the first [4] Vladimir A. Dobrushkin, Methods in Algorithmic Analysis. CRC
Press, 2010.
form presented above. The results also show that the al‐
[5] Oyelami M. O., “A Modified Diminishing Increment Sort for
gorithm becomes more efficient as the size of the input Overcoming the Search for Best Sequence of Increment for
increases. The results in the second table for lists that Shellsort”, Journal of Applied Sciences Research, Vol. 4, no. 6, pp.
have elements having the characteristics of the second list 760‐766, 2008.
presented above also shows that the Improved Median‐ [6] Oyelami, M.O., Azeta, A.A. and Ayo, C.K., “Improved Shellsort
for the Worst‐Case, the Best‐Case and a Subset of the Average‐
of‐Three Sort is more efficient than the Median‐of‐Three
Case Scenarios”, Journal of Computer Science & Its Application,
Sort. The higher the size of the list, the more efficient the Vol. 14, no. 2, pp. 73 – 84, 2007.
algorithm becomes. The implication of these results is that [7] Shola P. B., Data Structures With Implementation in C and Pascal.
the Improved Median‐of‐Three Sort is useful for average Reflect Publishers, 2003.
case sorting for large values of input size. When the two [ 8] Donald E. Knuth, The Art of Computer Programming, Volume I,
Fundamental Algorithms; Third Edition. Addison‐Wesley, 1997.
tables are compared, it can be deduced that the Improved
[9] Michael Main and Walter Savitch, Data Structures & Other Ob‐
Median‐of‐Three Sort is much more efficient for lists of
jects Using C++. Pearson Education, Inc., 2005.
elements of the second form than the second form. [10] Singleton R. C., “Algorithm 347 (An Efficient Algorithm for
Sorting With Minimal Storage)”, Communications of the ACM,
6. CONCLUSION vol. 12, pp. 187‐195, 1969.
[11] David Musser (1997), “Intrspective Sorting and Selection Algo‐
rithms”, Software Practice and Experience, Vol. 27, no. 8, pp. 983‐
It has been explained that Quicksort is the fastest known 993, 1997.
algorithm in practice. It is also best suited for partially 12. Sean McRoskey and James Notwell, “Enhancing CLucene using
sorted lists. It has, however, been refined for more effi‐ Introsort”,
ciency for average case scenarios. The most efficient re‐ http://www.nd.edu/~cseprog/proj08/final_data/clucenesearch/p
aper.pdf
fined Quicksort is the Median‐of‐Three Sort for average
[13] Sartaj Sahni, Data Structures, Algorithms and Applications in Java.
case scenarios. There are, however, some average case
McGrawHill, 2000.
scenarios that the Median‐of‐Three Sort is not so efficient
for. This paper has presented an approach for improving
the Median‐of‐Three Sort to cater for these scenarios. The Oyelami Moses Olufemi holds a BSc. in Computer Science (1999),
MSc. in Computer Science (2004) and currently teaches the same in
results from the implementation, experimentation and Covenant University, Nigeria. He won a South African Institute of
comparison of results generated from the experimenta‐ Computer Scientists and Technologists (SAICSIT) African best pa-
per award in 2009. He currently has about 18 publications. His cur-
tion of the improved algorithm with Medain‐of‐Three rent research interests include mobile computing, dialogue systems
Sort show that the Improved Median‐of‐Three Sort is and algorithm design. He is a member of Computer Professional
Registration Council of Nigeria (CPN) as well as a fellow of the Nige-
more efficient.
rian Computer Society (NCS).
Akinyemi Ibidapo O. is a Ph.D. student in the Department of Com-
REFERENCES puter and Information Sciences, Covenant University, Ota, Nigeria.
He holds B. Sc. Mathematical Sciences (Computer Science option)
[1] Robert Sedgewick, Algorithms in C. Addison‐Wesley, 1998. and M. Sc. (Computer Science) . His current research interests are
on Computational Intelligence and Software Engineering. He is a
member of the Nigeria Computer Society and Computer Professional
Registration Council of Nigeria. Email: dapkin2002@yahoo.co.uk.