Report On Research Paper:
Improving of Quicksort Algorithm Performance by Sequential Thread or
Parallel Algorithms
Abdulrahman Hamed Almutairi Abdulrahman Helal Alruwaili
King Saud University
2012
1
Contents
Figures 2
Tables 2
CERTIFICATE OF ORIGINALITY 3
Acknowledgement 4
Abstract 5
Introduction 5
What is Quicksort 6
Analysis of Parallel Quicksort 7
Comparision between Sequential and Parallel Quicksort 7
Speed Up Ratio 8
Conclusion 10
2
Figures
1 Comparision of sequential and parallel quicksort, T=1000 . . . . . . . . . . 9
2 Graph showing speed up ratio by using parallel quicksort . . . . . . . . . . . 10
Tables
1 Average run times for different threshold . . . . . . . . . . . . . . . . . . . . 9
3
CERTIFICATE OF ORIGINALITY
This is to certify, that the research paper submitted by me is an outcome of my
independent and original work. I have duly acknowledged all the sources from which the
ideas and extracts have been taken. The project is free from any plagiarism and has not
been submitted elsewhere for publication.
Name of Author (s):Vivek Kumar
Affiliated Institution: Indian Institue of Information Technology,Vadodara.
Title of the paper: Improving of Quicksort Algorithm Performance by Sequential
Thread or Parallel Algorithms
E-mail: [email protected]
4
Acknowledgement
The success and final outcome of this project required a lot of guidance and assistance
from many people and I am extremely privileged to have got this all along the completion of
my project. All that I have done is only due to such supervision and assistance and I would
not forget to thank them.
I would like to express my deepest appreciation to all those who provided me the
possibility to complete this report. A special gratitude I give to our Course Instructor ,Mr
VIKAS JAIN, whose contribution in stimulating suggestions and encouragement, helped
me to coordinate my project especially in writing this report on topic ”Improving of Quicksort
Algorithm Performance by Sequential Thread or Parallel Algorithms”.
Lastly, I thank almighty, my parents, and friends for their constant encouragement
without which this assignment would not be possible.
5
Abstract
Quicksort is a sorting algorithm and also known as a Partion-Exchange Sort in which
partition is carried out dynamically. Quicksort is a comparision sort and is popular because
it is not difficult to implement, works well for a variety of different kinds of input data, and
is substantially faster than any other sorting method in typical applications.The method
compares very favourably with other known methods in speed, in economy of storage, and
in ease of programming.
Introduction
One of the basic areas of the computer science is Data Structure. Sorting is an
important issue in Data Structure which creates the sequence of the list of items. Although
numbers of sorting algorithms are available, it is all the more necessary to select the best
sorting algorithm. Therefore, sorting problem has attracted a great deal of research as sorting
technique is very often used in a large variety of important applications so as to arrange the
data in ascending or descending order. This paper presents that sorting is an important area
of study in computer science. Like searching, the efficiency of a sorting algorithm is related
to the number of items being processed.
With the appearance of parallel computing, new possibilities have appeared to remove
this bottleneck and improve the performance of known sorting algorithms [1] by modifying
them for parallel execution. At first, this was achieved using distributed computing, however
with the hardware available today, it is possible to do this even on home computers thanks
to their multicore processors. In this report, we discuss a parallel version of the well know
quicksort algorithm and compare its performance to the performance of its simpler, sequential
6
quicksort algorithm. Comparing their performance, we look for the threshold T, the size
of the array, at which the parallel algorithm becomes actually slower than the sequential
algorithm.
What is Quicksort
The quicksort algorithm was developed in 1960 by Sir Charles A. R. Hoare commonly
known asTony Hoare while in the Soviet Union.Quicksort is a divide and conquer algorithm.
Quicksort first divides a large array into two smaller sub-arrays: the low elements and the
high elements. Quicksort can then recursively sort the sub-arrays. The steps are:
1. Pick an element, called a pivot, from the array.
2. Partitioning: reorder the array so that all elements with values less than the pivot
come before the pivot, while all elements with values greater than the pivot come after it
(equal values can go either way). After this partitioning, the pivot is in its final position.
This is called the partition operation.
3. Recursively apply the above steps to the sub-array of elements with smaller values
and separately to the sub-array of elements with greater values.
The base case of the recursion is arrays of size zero or one, which are in order by
definition, so they never need to be sorted.
The pivot selection and partitioning steps can be done in several different ways; the
choice of specific implementation schemes greatly affects the algorithm’s performance.
Mathematical analysis of quicksort shows that, on average, the algorithm takes O(n
log n) comparisons to sort n items. In the worst case, it makes O(n2) comparisons, though
this behavior is rare.
7
Analysis of Parallel Quicksort
Quicksort’s divide-and-conquer formulation makes it amenable to parallelization using
task parallelism[2]. The partitioning step is accomplished through the use of a parallel prefix
sum algorithm to compute an index for each array element in its section of the partitioned
array. Given an array of size n, the partitioning step performs O(n) work in O(log n) time
and requires O(n) additional scratch space. After the array has been partitioned, the two
partitions can be sorted recursively in parallel. Assuming an ideal choice of pivots, parallel
quicksort sorts an array of size n in O(n log n) work in O(log 2 n) time using O(n) additional
space.
Quicksort has some disadvantages when compared to alternative sorting algorithms,
like merge sort, which complicate its efficient parallelization[3]. The depth of quicksort’s
divide-and-conquer tree directly impacts the algorithm’s scalability, and this depth is highly
dependent on the algorithm’s choice of pivot. Additionally, it is difficult to parallelize the
partitioning step efficiently in-place.The limitation to speed increase of a parallel algorithm
as compared to a sequential algorithm are the overhead caused by the need to create new,
parallel processes and their management.
Comparision between Sequential and Parallel Quicksort
The parallel implementation is only slightly different. First, the size of the array to
be sorted is checked against a threshold. If the size is smaller than the threshold, sequential
quicksort is used as the overhead of creating new tasks would slow the sorting process too
much. If the size of the array is bigger than the threshold, instead of calling the quicksort for
8
each part directly, new Task is created for each of the call and let’s them handle the sorting
of each part of the array.
Pseudocode for sequential quicksort :
With the threshold set at 50000, the parallel algorithm is actually slower, as the
computational cost of creating new tasks increases the total run time, but the parallelism is
not utilized enough to offset this. The use of scratch space simplifies the partitioning step,
but increases the algorithm’s memory footprint and constant overheads.
Speed Up Ratio
The performance of a parallel algorithm is determined by calculating its speedup.
Speedup is defined as the ratio of the worst-case execution time of the fastest known se-
9
Figure 1. Comparision of sequential and parallel quicksort, T=1000
T=1000 T=5000 T=50000
10 0.01 0 0.010001
100 0.001 0.020001 0.050004
1000 0.250016 0.270011 0.260018
10000 2.010118 2.880166 3.060169
25000 5.380318 6.120344 9.15052
50000 11.36065 11.320644 19.61112
75000 18.14103 18.251045 28.60164
100000 24.91142 22.591294 34.19196
Table 1. Average run times for different threshold
quential algorithm for a particular problem to the worst-case execution time of the parallel
algorithm.
Speed up formula:
As can be see here, at low number of elements no speed up is achieved. As the number
of elements increases, the speed of sorting actually decreases. This is caused as stated before
by the overhead needed for creating the parallel tasks and as there is not enough elements
for the parallelism being able to compensate for this. After the number of elements increases
enough, the overall speed and speed gain increases as well by about 20%.
10
Figure 2. Graph showing speed up ratio by using parallel quicksort
Conclusion
We successfully implemented a parallel version of quicksort algorithm. After choosing
a appropriate threshold value to switch from parallel to sequential sorting, we observed the
performance of the algorithm. The results are obviously in favor of the parallel quicksort
algorithm. Using a reasonable threshold to return to sequential quicksort, we are able to
circumvent the increased computational cost of creating new tasks for small datasets, while
with the bigger datasets we take advantage of the parallelism possible by today’s hardware.
And thanks to simplicity of the parallel implementation of quicksort algorithm it is easy to
achieve major, 20% increase of performance when sorting a larger dataset.
References
[1] Steven S Skiena. Sorting and searching. In The Algorithm Design Manual, pages 103–144.
Springer, 2012.
[2] Philippas Tsigas and Yi Zhang. A simple, fast parallel implementation of quicksort and
its performance evaluation on sun enterprise 10000. In Eleventh Euromicro Con-
ference on Parallel, Distributed and Network-Based Processing, 2003. Proceedings.,
pages 372–381. IEEE, 2003.
[3] ERNEST SCHEIBER. A parallelization scheme of some algorithms.