You are on page 1of 8

International Journal of Research and Analytical and Reviews

Vol.

TRIPLE PIVOT QUICK SORT


Ekansh Rathod
Department of Information Technology, University Institute of Technology RGPV,
Bhopal, Madhya Pradesh 462036, India
ekanshrathod786@gmail.com
Piyush Khodre
Department of Information Technology, University Institute of Technology RGPV,
Bhopal, Madhya Pradesh 462036, India
piyushkhodre1996@gmail.com
Roshni Thomas
Department of Information Technology, University Institute of Technology RGPV,
Bhopal, Madhya Pradesh 462036, India
roshnit.1j@gmail.com
Sahil Rai
Department of Information Technology, University Institute of Technology RGPV,
Bhopal, Madhya Pradesh 462036, India
raisahil2@gmail.com
Prof. Dr. Mahesh Pawar
Department of Information Technology, University Institute of Technology RGPV,
Bhopal, Madhya Pradesh 462036, India
mkpawar24@gmail.com

Abstract
Researchers are always interested in finding new techniques which work better in comparison to the previous
developed algorithms and produces more precise results in an easy and efficient manner. So, our study on the
sorting techniques is also based on similar interests. Study on sorting algorithms is encouraged and appreciated
very much in the research field as it improves the working capability of the other algorithms also which give
better results when applied onto some sorted content. Sorting also improves the readability. Therefore an
efficient sorting technique is important which helps in achieving outputs quickly. Our paper suggests “Triple
pivot” quick sort algorithm which is much more efficient than the traditional quick sort technique (Single-Pivot
quick sort).Researchers are always interested in finding new techniques which work better in comparison to the
previous developed algorithms and produces more precise results in an easy and efficient manner. So, our study
on the sorting techniques is also based on similar interests. Study on sorting algorithms is encouraged and
appreciated very much in the research field as it improves the working capability of the other algorithms also
which give better results when applied onto some sorted content. Sorting also improves the readability.
Therefore an efficient sorting technique is important which helps in achieving outputs quickly. Our paper
suggests “Triple pivot” quick sort algorithm which is much more efficient than the traditional quick sort
technique (Single-Pivot quick sort).

Keywords​: ​Sorting, Triple Pivot, Algorithm, Complexities – Time and Space.

I. Introduction
1. The data we work upon is generally sorted, which means it may either be in ascending or descending order.
This process of sorting which when applied on the data ensures easy calculations and efficient algorithm
applications. This must be a reason as to why many algorithmic researchers may have invested their time and
resources to suggest a better sorting algorithm.
2. Sorting may be categorized into Internal sorting and External sorting[4]. In case, if data to be sorted is small
to be placed in main memory at a time, then sorting process can be performed in main memory. This is Internal
sorting. If the data is huge, then the data which has to be sorted first is placed main memory. The remaining data
rests in secondary storage. This is External sorting. We will discuss Internal sorting.
International Journal of Research and Analytical and Reviews
Vol.

3. Sorting involves traversing given data (whole or partially, as per algorithm). This is termed as a pass. Each
pass is responsible to sort the data part by part. Finally after the last pass, we get a sorted list.
4. Our aim is to develop a sorting algorithm which sorts the data in an efficient manner. As per our study based
on the previous developed algorithm, we have found the Quick sort Algorithm to be both economical and
efficient, both in terms of time complexity as well as space complexity. Therefore, extending the concept of
Quick sort given by C.A.R. Hoare which is based on divide and conquer, we have developed a Triple-Pivot
Quick sort. This algorithm has triple pivot system to enhance the efficiency while Quick sort has a single pivot
system.
5. As we know, performance of a sorting algorithm mainly depends on -
Time (i.e. execution time).
Space requirements (i.e. space complexity)
Number of recursion calls in sorting code.
Type of data to be sorted.

As of now, space is not an issue because memory is cheaper. Thus, faster execution is desirable. Triple pivot
Quick sort tries to achieve this inadequacy.
II. Overview of other techniques
(A) Selection sort
Given a list of numbers to be sorted, then in selection sort as the name suggests, it “selects” smallest number and
places it in the first position. This process is followed until next numbers also get placed in the most appropriate
position. This is the procedure when we need to arrange the list in ascending order[3].

First n-1 elements get sorted as per the above process and the largest element gets placed automatically
due to previous passes.

Analysis​:- O(n​2​)complexity and inefficient for larger arrays.

(B) Bubble sort

The principle that this sorting algorithm works upon is quiet simple. It proceeds by comparing the elements in
the first 2 positions of the list. If found the two numbers unsorted then swaps them, else moves ahead. Then the
next comparison is between second & third element. The concept is applied throughout the list elements until
the list gets sorted. So there are many swaps in a single pass. It is not an efficient technique although it can be
implemented easily[3].
Analysis​:- Best case = O(n)
Worst & Average Case = O(n​2​)

(C) Insertion sort


This technique adds each element at its proper position in a final sorted list, that is, this method considers
the list of elements divided into 2 parts – sorted and unsorted. As it accepts and works as per one input at a
time, therefore at initial stage our sorted part has only the first element of the list and unsorted part will have
the other elements. For n elements, n-1 passes are required to sort the list[3].
Analysis​:- Worst case = O(n​2​)
Best Case = O(n)

(D) Merge sort


The concept in this technique is such that we need to merge two sorted lists into single sorted list. It follows
“Divide & Conquer”. The list is divided into 2 halves and then those newly created parts are individually sorted.
Finally the two sorted halves are merged to form a complete sorted list.
Analysis​:- Worst case = O(n logn)
Average Case = O(n logn)
Best Case = O(n logn)
(E) Quick sort
Quick sort is also known as Partition Exchange Sort. This is a sorting algorithm based on “divide & conquer”
concept. The problem list to be sorted is broken into sub problem and those are further divided into smaller
problems and so on. It is a very fast technique and requires no additional space for sorting. As the exchange
happens between largely separated elements , so less no. of exchanges takes place in positioning the elements in
their exact position.
Analysis​:- Worst case = O(n​2​)
International Journal of Research and Analytical and Reviews
Vol.

Best Case = O(n logn)


Avg. Case = O(n logn)

III. Proposed methodology

(A) Concept
The concept of Multi-pivot quick sort is an extension to the canonical sorting algorithm- quick sort, to improve
& enhance the efficiency and performance of quick sort.
In quick sort, a pivot is chosen from the list to be sorted and then arrangement of the rest elements is done such
that the elements less than the pivot value are moved to one side of pivot and the rest occupy the other side of
the pivot.
Quick sort is known to be an efficient sorting algorithm, but it lacks in utilizing the improvements brought in the
caching behaviour due to advancements in the computer systems and architectures. If the technical
improvements prove to be assisting the sorting algorithm, then there can be seen some noticeable improvements
in the execution time required by the sorting algorithm. The reason for the popularity of the multi-pivot quick
sort is that it allows adapting to the advancements in the computer architecture, thus improves the performance
of the algorithm.
In 2009, Valdimir Yaroslavsky gave the concept of dual-pivot quick sort which showed better performance in
comparison to the single-pivot quick sort, primarily by decreasing the number of swaps by 20%. Yaroslavsky
implemented in Java Virtual Machine. It is claimed that the caching behaviour of the multi-pivot quick sort
algorithms is the key to better performance. The algorithm performance also depends on system architecture.

(B)Algorithm
Three pivots p1, p2, p3, are chosen from dataset i.e. list, which are then arrange in ascending order. Then the list
is traversed and divided into 4 sub-lists such that for any element e in the list:
1. If e < p1, then e is swapped to left of p1
2. If p1 ​<​ e < p2, then e is swapped to the position between p1 and p2
3. If p2 ​<​ e < p3, then e is swapped to the position between p2 and p3
4. If p3 ​<​ e, then e is swapped to the right of p3
Three pivots must be used to divide an unsorted region and the four sub-lists. The pivots start from the ends of
the list and they cross at the centre of the list where the pivots are placed at the end of partition process. At every
step, the present element is checked with the pivot p2 (central pivot) and then also compared to either p1 or p3
(pivots) as per whether the element is less than p2 or greater than p2. This is similar to the dual-pivot quick sort
idea proposed in 2009.
Analysis of performance is done based on 4 parameters:
1. Number of comparison occurring
2. Number of swaps performed
3. Number of recursion calls
(C) Code
def TriplePivot(list):
n = len(list)
if n <= 1:
return list
elif n ==2:
return sorted(list)

p1,p2,p3 = sorted([list[0],list[1],list[2]])

list1 = [ ]
list2 = [ ]
list3 = [ ]
list4 = [ ]

for element in list:


if element < p1:
list1.append(element)
elif p1 <= element < p2:
list2.append(element)
elif p2 <= element < p3:
list3.append(element)
International Journal of Research and Analytical and Reviews
Vol.

else:
list4.append(element)

return TriplePivot(list1)+TriplePivot(list2)+TriplePivot(list3)
+TriplePivot(list4)

(IV) Analysis
There is no unsureness that higher pivot Quick sort, sorts large list or datasets more swiftly as compared to
traditional Single Pivot Quick Sort. No doubt time and space complexities are main parameters to check
performance of sorting algorithm, but other factors such as system specification also contribute. Triple Pivot
Quick Sort is tested on linux environment, 64 bit architecture and having intel core ​i3- 2​ 100 @3.10Ghz.
Datasets (i.e. lists) are generated randomly using random.randint(start,stop,increment) function using random
package in python. The same random list generated is used by single pivot, double pivot, triple pivot sorting
python codes and result are produced. The result part contains the Execution time of each sorting algorithm and
this data is saved in excel sheet, using python package xlsxwriter (or similar excel python packages can be used
such as xlwt etc).

There are three types of list generated, named, small, medium and large. The small list consists of N =
100-1,0000 elements with increment of 100 in each list. The medium list consists of N=1,000-10,000 with
increment of 1,000 in each list and the large list consists of N =10,000-1,00,000 with increment of 10,000 in
each list.

The following tables shows the execution time of the three sorting algorithms in seconds where N is length of
list.

Table 1. Small data set


International Journal of Research and Analytical and Reviews
Vol.

Table 2. Medium data set

Table 3. Large data set

Table shows large data set gives around 40% efficiency while using triple pivot as compared
to single pivot. Whereas small and medium datasets are not showing much difference in terms
of efficiency as shown in the graphs.

The execution time data is generated in three excel sheets, naming, small data set, medium
data set and large data set. The data generated is tested in MATLAB R2016a, plotting length
of list (on horizontal-axis) vs execution time(on vertical-axis).
International Journal of Research and Analytical and Reviews
Vol.

Fig.1 Graph of small dataset

Fig.2 Graph of medium dataset


International Journal of Research and Analytical and Reviews
Vol.

Fig.3 Graph of large dataset

V. Comparisons
In this research paper, we have used 3 pivots in the quick sort technique and so we name it “Triple Pivot”. In the
traditional quick sort method there is only a single pivot. Along with the increase in the number of pivots , the
success of multi-pivot is also because of it’s adaptability to the advancements in computer architecture. Hence,
we are able to enhance the efficiency of the sorting technique (Quick Sort) in terms of reduced execution time.

The other techniques that have been described in various research papers that we have gone through are also
working on some key feature which distinguishes it from the rest algorithms and their acceptance depends on
the amount of efficiency that they provide in sorting. Some examples are –

In case of the traditional quick sort, it works in 2 steps : first is deciding a right pivot position and second is the
division process in which the elements are placed in their exact positions with respect to chosen pivot. In one of
the algorithms we saw the use of Scan, Move, Sort which is an example of enhanced quick sort version. We
also have studied certain research papers based on the dual-pivot concepts which eventually helped us in
developing an idea of Triple Pivot. The idea of dual pivot itself reduced the number of swaps in the sorting
International Journal of Research and Analytical and Reviews
Vol.

algorithm by 20%, so the triple pivot has enhanced the traditional and also the dual pivot, by reducing the
execution time.

VI. Conclusion
In this paper we have proposed an enhanced version of quicksort algorithm. The traditional quicksort algorithm
uses a single pivot. The idea of Dual pivot was implemented in sort methods of java (Java 7). Using triple pivot
the number of swaps in sorting are reduced to a great extent hence utilizing lesser number of resources and
increasing the efficiency. In a nutshell the methodology proposed, pivots are chosen in the order of input i.e. the
first three numbers. Then pivots are arranged in increasing order. After that the rest of inputs are arranged in
above mentioned algorithm. The numbers smaller than the pivot on the left and similarly the numbers greater on
the right hand side of the pivot for all the three pivots.

VII. Acknowledgment

We are grateful to our mentor Dr. Prof. Mahesh Pawar for guiding us throughout the research project
completion successfully. We are also thankful to our department of Information Technology for giving us an
opportunity to work and develop sorting algorithm.

VIII. References
[1] Discussion of MultiPivot QuickSort:Theory and Experiments,Paper by Kushagra, Lopez-Oritz, Munro and
Qiao Discussion by Nisha Masharani

[2] Enhanced QuickSort algorithm by Rami Mansi Department of Computer Science, Al al - Bayt University,
Jordan

[3] List Sort: A New Approach for Sorting List to Reduce Execution TimeBy Adarsh Kumar Verma and
Prashant Kumar

[4] Sorting Algorithms By Neelam Yadav and Sangeeta Kumari

You might also like