You are on page 1of 5

JOURNAL OF COMPUTING, VOLUME 3, ISSUE 4, APRIL 2011, ISSN 2151-9617

HTTPS://SITES.GOOGLE.COM/SITE/JOURNALOFCOMPUTING/
WWW.JOURNALOFCOMPUTING.ORG 193

Improving the Performance of Quicksort for


Average Case Through a Modified
Diminishing Increment Sorting
Oyelami M. Olufemi and Akinyemi I. Olawole

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

2.1 QUICKSORT partition (A[ ], start, end) 


1. {set the pivot to the first element} 
Quicksort sorts by employing a divide and conquer strat‐ pivot = A[start] 
egy to divide a list of elements to be sorted into two sub‐ 2. do while start < end 
lists.  It divides the elements to be sorted into two groups,  Begin 
sorts the two groups by two recursive calls, and combines  {Search for a number smaller than the pivot from the end} 
the two sorted groups into a single array of sorted values  3. while start < end and A[end] >= pivot end = end ‐ 1 
[9]. The steps followed are:  4.  if start < end 
1. Pick an element, called a pivot, from the list.     begin 
  A[start] = A[end]{a smaller number found} 
2. Reorder  the  list  so  that  all  elements  which  are  less 
  {find  a  number  larger  than  the  pivot  from  the 
than  the  pivot  come  before  the  pivot  and  so  that  all 
start} 
elements  greater  than  the  pivot  come  after  it  (equal 
5.  while  start  <  end  and  A[start]  <=  pivot  start  ++
values can go either way). After this partitioning, the 
   
pivot is in its final position. This is called the partition 
  {a larger number found} 
operation.  
6.  A[end] = A[start] 
3. Recursively  sort  the  sub‐list  of  lesser  elements  and    end 
the sub‐list of greater elements.  End 
  7. A[start] = pivot 
To sort an array with the lowest index low and the high‐ 8. return start
est  index  high,  traditional  Quicksort  selects  the  first  ele‐ The figure below illustrates Quicksort: 
ment  with  index  low.  With  p  as  the  pivot,  it  now  scans   
through the array and move all the elements smaller than 
p  to  the  lower  half  and  all  the  elements  larger  than  p  to 
the upper half. The two halves are then sorted recursively 
using Quicksort. The variable mid stands for the position 
(index)  in  the  array  where  the  pivot  element  is  put.  The 
recursion will stop when low    high is true.  The Quick‐
sort algorithm is given below: 
 
Qsort(A[ ], low, high) 
1. if low < high then   
Start  looking  for  the  element  less  than  8  from  the  end, 
2.  mid = partition(A,low,high) 
end‐1, …. It is found at position 4, therefore, put the ele‐
3.  Qsort(A, low, mid‐1) 
ment in position 0. 
4.  Qsort(A, mid + 1 , high) 
 
To sort the entire array A, the first call should be Qsort(A, 
1,  sizeof(A)).  “partition”  procedure  is  the  key  to  the 
Quicksort  algorithm  because  it  arranges  the  subarray   
A[low..high] in place. The partition procedure divides the 
Start  looking  for  the  element  greater  than  8  from  1.  It  is 
array  elements  into  two  halves  and  returns  the  position 
where the pivot element is placed. The pivot is first set to  found at position 2, therefore, we have:  
A[low]  and  then  a  number  smaller  than  the  pivot  is 
looked  for,  from  the  position  high,  high  ‐1,  etc.  If  the 
number is found at position p for instance, the number is 
moved  to  position  low.  Then  a  number  larger  than  the    
pivot is looked for, from  low + 1, low + 2, etc. If the num‐
Copy the element[2] = 9 to the position 4 
ber is found at position q for instance, it is moved to posi‐
tion p. The process is then repeated, but this time by look‐
ing for a number smaller than the pivot from p‐1, p‐2, etc.  
The “partition” procedure is given below: 
   
  Now,  start  looking  for  an  element  smaller  than  8  from 
  position 3. The element is found at position 3. Therefore, 
JOURNAL OF COMPUTING, VOLUME 3, ISSUE 4, APRIL 2011, ISSN 2151-9617
HTTPS://SITES.GOOGLE.COM/SITE/JOURNALOFCOMPUTING/
WWW.JOURNALOFCOMPUTING.ORG 195

it is moved to position 2:  2.2.3 Introspective Sorting (Introsort)


Introsort  is  a  modified  Quicksort  that  can  be  said  to  be 
self‐aware.  It  starts  like  Quicksort  by  selecting  a  pivot, 
partitioning  the  array  around  the  point,  and  recursively 
calling Introsort. Introsort, however, maintains an internal 
variable that measures the number recursive calls made to 
 
Introsort. When a recursive call is made that matches the 
start looking for an element greater than  8 from position  number of recursive calls that that would be made in the 
3, after incrementing start by 1. But since start is equal to  average case, heapsort is called. In the average case, Intro‐
end, we stop and put the pivot in position 3 i.e   sort  functions  like  Quicksort,  but  in  the  worst  case,  it 
functions like heapsort [11, 12]. 
 
3. IMPROVED MEDIAN-OF-THREE
SORT FOR THE AVERAGE CASE
   
Position 3 is returned as the value of mid and the follow‐ The  proposed  approach  for  improving  Median‐of‐Three 
ing calls are made to partition method again:  Sort for average case scenarios first of all divides the ele‐
Qsort(A,0,2)  ments to be sorted into sub‐sequences just like Shell Sort 
does,  but  by  first  of  all  comparing  the  first  element  with 
Qsort(A,4,4)  
the last. If the last is less than the first, the two swap posi‐
  tions,  otherwise,  they  maintain  their  positions.  Later,  the 
Figure 1.   Illustration of Quicksort. second element is compared with the second to the last, if 
the second to the last element is smaller than the second, 
they  are  swapped.  Otherwise,  they  maintain  their  posi‐
tions.  This  process  continues  until  the  last  two  consecu‐
2.2 IMPROVEMENTS ON QUICKSORT
tive  middle  elements  are  compared,  or  until  it  remains 
There  have  been  several  refinements  on  Quicksort  to  only one element in the middle [5, 6]. After this, Median‐
handle  more  efficiently  both  elements  that  are  partially  of‐Three sort is applied to sort the entire array.  
sorted  and  those  in  reverse  order.  These  are  highlighted   
below:  Consider  the average‐case  scenario  of  sorting  the  follow‐
  ing elements in ascending order: 
2.2.1 Median-of-Three Rule 19  18  17  16  1  2  3  4 
 
Quicksort selects the first element in the list as the pivot. 
The algorithm works like this: 
This is acceptable for random input. If the list is partially 
sorted or in reverse order, it gives a poor partitioning and   
the algorithm takes quadratic time. The Median‐of‐Three 
rule  selects  the  median  of  the  first,  middle  and  the  last 
elements in each sub‐list as the pivot. This method selects 
a  pivot  closer  to  the  middle  of  the  sub‐list  than  the  first‐
element rule when the list to be sorted is already or par‐
tially sorted [1, 10]. This approach produces a better per‐
formance than the first element approach. 
 
2.2.2 Small Sub-lists

For  small  files  (n≤20),  Quicksort  is  worse  than  Insertion 


Sort, and small files occur often because of recursion. This 
approach  uses  an  efficient  sort  (e.g  Insertion  Sort)  for 
small  files  [1]. A  better  idea  is  simply  to  ignore  all  small 
sub‐files, and when execution of the Quicksort algorithm 
terminates,  the  file  will  not  be  sorted.  It  will  only  be 
slightly  unsorted,  however,  in  that  it  will  contain  small 
unordered groups of elements. But all of the elements in 
each  such  group  will  be  smaller  than  those  in  the  next   
 
group. The list will simply be sorted using Insertion Sort 
which  works  well,  since  it  is  efficient  for  nearly  sorted  Figure 2.  Illustration  of  Improved  Median‐of‐
files [1].  Three Sort 
   
After  this,  Median‐of‐Three  Sort  is  now  applied  on  the 
JOURNAL OF COMPUTING, VOLUME 3, ISSUE 4, APRIL 2011, ISSN 2151-9617
HTTPS://SITES.GOOGLE.COM/SITE/JOURNALOFCOMPUTING/
WWW.JOURNALOFCOMPUTING.ORG 196

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.
 

You might also like