merge
merge
net/publication/312963714
CITATIONS READS
6 7,455
2 authors, including:
Tribikram Pradhan
Indian Institute of Technology BHU
40 PUBLICATIONS 405 CITATIONS
SEE PROFILE
All content following this page was uploaded by Tribikram Pradhan on 01 February 2020.
Abstract—This paper aims at introducing a new sorting uses the concept of divide and conquer to sort the
al- gorithm which sorts the elements of an array In Place. array recursively using bottom up approach. Instead of
This algorithm has O(n) best case Time Complexity using an external array to merge the two sorted sub
and O(n log n) average and worst case Time Complexity. arrays, we use multiple pivots to keep track of the
We achieve our goal using Recursive Partitioning
minimum element of both the sub arrays and sort it In
combined with In Place merging to sort a given array. A
comparison is made between this particular idea and Place.
other popular implementations. We finally draw out a
conclusion and observe the cases where this outperforms Rest of the paper is organized as follows. Section II
other sorting algorithms. We also look at its shortcomings discusses the various references used in making this
and list the scope for future improvements that could be paper. Section III describes the basic working idea
made. behind this algo- rithm. Section IV contains the
pseudo code required for the implementation of this
Keywords—Time Complexity, In Place, Recursive algorithm. In Section V, we do a Case Study on the
Partitioning
merging process over an array. In Section VI, we derive
the time and space complexities of our code. In Section
I. INTRODUCTION
VII, we do an experimental analysis of this algorithm
In mathematics and computer science, the process of
on arrays of varying sizes. In Section VIII, we draw out
ar- ranging similar elements in a definite order is
asymptotic conclusion based on Section VI and VII.
known as sorting. Sorting is not a new term in
We finally list out the scope for future improvements
computing. It finds its significance in various day to
and conclude the paper in Section X.
day applications and forms the backbone of
computational problem solving. From complex search
II. LITERATURE SURVEY
engine algorithms to stock markets, sorting has an
You Ying, Ping You and Yan Gan[2], in the year 2011
impeccable presence in this modern day era of
made a comparison between the 5 major types of
information technology. Efficient sorting also leads in
sorting algorithms. They came to a conclusion that
optimization of many other complex problems.
Insertion or Selection sort performs well for small
Algorithms related to sorting have always attracted a
range of elements. It was also noted that Bubble or
great deal of Computer Scientists and Mathematicians.
Insertion sort should be preferred for ordered set of
Due to the simplicity of the problem and the need for
elements. Finally, for large random input parameters,
solving it more systematically, more and more sorting
Quick or Merge sort outperforms other sorting
algorithms are being devised to suit the purpose.
algorithms.
There are many factors on which the performance of
a sorting algorithm depends, varying from code
Jyrki Katajainen, Tomi Pasanen and Jukka
complexity to effective memory usage. No single
Teuhola[4], in the year 1996 explained the uses and
algorithm covers all aspects of efficiency at once.
performance analysis of an In Place Merge Sort
Hence, we use different algorithms under different
algorithm. Initially, a straightforward variant was
constraints.
applied with O(n log 2n)+ O(n) comparisons and 3(n log
When we look on developing a new algorithm, it is
2n) + O(n) moves. Later, a more advanced variant was
impor- tant for us to understand how long might the
introduced which required at most (n log 2n) + O(n)
algorithm take to run. It is known that the time for
comparisons and (n log 2n) moves, for any fixed array
any algorithm to execute depends on the size of the
of size ’n’.
input data. In order to analyze the efficiency of an
algorithm, we try to find a relationship on it’s time
Antonio S Symbonis[6], in 1994 showed the stable
dependence with the amount of data given.
merging of two arrays of sizes ‘ m’ and ‘ n’, where m
Another factor to take into consideration is the
< n, with O(m + n) assignments, O(m log (n/m + 1))
space used up by the code with respect to the input.
comparisons and a constant amount of additional
Algorithms that need constant minimum extra space
space. He also mentioned the possibility of an In
are called In Place. They are generally preferred over
Place merging without the use of an internal buffer.
algorithms that take extra memory space for their
execution.
Wang Xiang[7], in the year 2011 presented a brief
In this paper, we introduce a new algorithm which
analysis of the performance measure of Quick Sort 3.1 DIVIDE AND CONQUER
algorithm. This paper discusses about the Time
Complexity of Quick Sort algorithm and makes a We use the Divide and Conquer strategy to split the
comparison between the improved Bubble Sort and given array into individual elements. Starting from
Quick Sort through analysing the first order derivative individual elements, we sort the array using Bottom Up
of the function that is found to co-relate Quick Sort Approach, keeping track of the minimum and
with other sorting algorithms. maximum value of the sub arrays at all times. The
technique used for splitting the array is similar to
Shrinu Kushagra, Alejandro Lopez-Ortiz and J. Ian that of a standard Merge Sort, where we recursively
Munro [8], in 2013, presented a new approach which partition the array from start to mid, and from mid to
consisted of multiple pivots in order to sort elements. last after which, we call the sort function to sort that
They performed an experimental study and also particular sub array.
provided analysis on cache behavior of these
algorithms. Here, they proposed a 3 pivot mechanism
for sorting and improved the performance by 7-8%. 3.2 PIVOT BASED MERGING
699
2016 International Conference on Advanced Communication Control and Computing Technologies (ICACCCT)
yet been accessed (again, for most of the time, 30: end if
31: else if a = x and b = y and ar[b] < ar[a] then
barring a few passes). As mentioned earlier, ‘x’ is the 32: swap ( ar[x] , ar[b] )
point before which our final array is sorted. So at 33: a ← b, b ← b + 1 , x ← x + 1
any point, the array from ‘i’ to ‘x-1’ is sorted. 34: if ctr = b − 1 then
35: ctr ← ctr + 1
Finally, we have another variable called ‘ctr’, which 36: end if
is initialized and always kept equal to ’b’ till the 37: else if a = x and b = y and ar[b] >= ar[a] then
38: x ← x + 1 and a ← a + 1
second sub array (from ’b’ to ’j’) is sorted. If not, 39: else if b = a + 1 and ar[b] < ar[a] then
we keep on incrementing ‘ctr’ and swap it with its 40: swap ( ar[b] , ar[x] )
41: swap ( ar[a] , ar[b] )
next value until the element at ‘ctr’ gets placed in 42: b←b+1, x←x+1, a←a+ 1
its correct position and the second sub array becomes 43: if ctr = b − 1 then
sorted once again. We then make ‘ctr’ equal to ’b’. 44: ctr ← ctr + 1
45: end if
46: else if b = a + 1 and ar[b] >= ar[a] then
Our logic revolves around comparing the current 47: swap ( ar[x] , ar[a] )
48: a←y, x←x+1
minimum values in the two sorted sub arrays (values at 49: else if a = y and x < y and ctr != b + 1 and ar[b] < ar[a] then
’a’ and ’b’), and swapping the smaller number with the 50: swap ( ar[x] , ar[b] )
51: b←b+1, x←x+1
value at ’x’. We then increment ’x’ and reposition (’a’ 52: if ctr = b − 1 then
or ’b’) accordingly. 53: ctr ← ctr + 1
54: end if
55: else if b > a + 1 and ar[b] >= ar[a] then
IV. PSEUDO CODE 56: swap ( ar[x] , ar[a] )
57: x ← x + 1 , a ← a+ 1
58: end if
Given below is the working pseudo code for the idea 59: end while
proposed. We have two main functions to achieve our
purpose, one to split the array and the other to sort that
particular sub array In Place. V. CASE STUDY
In this Case Study, we take a look into the merging
Algorithm 1 SPLITTING ALGORITHM process of the two sorted sub arrays. Let us consider an
array of size 18 elements for the sake of this example.
1: Procedure split (int * ar, int i, int j):
2: if j = i + 1 or j = i then
The two sorted sub arrays are from ’i’ to ’b-1’ and from
3: if ar[ i ] > ar[ j ] then ’b’ to ’j’.
4: swap ( ar[ j ] , ar[ i ] )
5: return
6: end if INITIAL ARRAY:
7: else
8: mid = (i + j)/2
9: split (ar, i, mid)
10: split (ar, mid+1, j)
11: if ar[mid + 1] < ar[mid] then
PASS 1:
12: sort (ar, i, j)
13: end if This is the first pass inside the ’while’ loop of our
14: end if
’sort’ procedure. As mentioned, we compare the current
minimum values of the two sub arrays (value at ’a’ (-4)
and ’b’ (-3)). The value at ’a’ is less than that at ’b’.
Since ’a’ is equal to ’x’, we don’t need to swap the
1: Procedure sort (int * ar, int i, int j) :
values at ’a’ and ’x’. Instead, we increment ’a’ and ’x’.
2: x ← i, a ← x, b ← (i + j)/2 + 1, y and ctr ← b The first element (-4) is now in its correct position.
3: while x < b do
4: if ctr < j and ar[ctr] > ar[ctr + 1] then
’a’ holds the minimum value of first sub array that has
5: swap ( ar[ctr] , ar[ctr + 1] ) not yet been accessed (-1) and the array before ’x’ is
6: ctr ← ctr + 1 sorted.
7: end if if a = x and b = y and ar[b] >= ar[a] then
8: if ctr ≥ j or ar[ctr] <= ar[ctr + 1] then
x ← x + 1 and a ← a + 1
9: ctr ← b
end if
10: end if
11: if b > j and a > x and b = a + 1 and a > y and ctr = b then
12: b ← a, ctr ← b, a ← y
13: else if b > j and a > x and ctr = b then
14: b ← y, ctr ← b, a ← x PASS 2:
15: else if b > j and ctr = b then
16: break In this pass, the value at ’b’ is less than that at ’a’. So
17: end if we swap the value at ’b’ with the value at ’x’. ’x’ is
18: if a = x and x = y and ctr = b then
19: y←b incremented accordingly. The second element (-3) is
20: else if x = y then now in its correct sorted position. We reassign ’a’ as ’b’
21: y←a
22: end if
and increment ’b’. ’b’ now contains the current
23: if a > y and b > a + 1 and ar[b] < ar[a] and ctr = b then minimum value of the second sub array (-2) and ’a’
24: swap ( ar[a] , ar[b] ) keeps track of its previously pointed value (-1).
25: swap ( ar[a] , ar[x] ) if a = x and b = y and ar[b] < ar[a] then
26: x ← x + 1 , a ← a+ 1 swap ( ar[x] , ar[b] )
27: if ar[ctr] > ar[ctr + 1] then a ← b, b ← b + 1 , x ← x + 1
28: swap ( ar[ctr] , ar[ctr + 1] ) if ctr = b − 1 then
29: ctr ← ctr + 1
700
2016 International Conference on Advanced Communication Control and Computing Technologies (ICACCCT)
ctr ← ctr + 1 if a > y and b > a + 1 and ar[b] < ar[a] and ctr = b then
end if swap ( ar[a] , ar[b] )
end if swap ( ar[a] , ar[x] )
x ← x + 1 , a ← a+ 1
if ar[ctr] > ar[ctr + 1] then swap ( ar[ctr] , ar[ctr + 1] ) ctr ← ctr + 1
end if
end if
PASS 3:
Our motto behind each pass is to assign ’a’ and ’b’
such that they contain the current minimum values of
the two sub arrays (This condition is true for all but few PASS 8:
if ctr ≥ j or ar[ctr] <= ar[ctr + 1] then
passes (discussed in Pass 7)). ctr ← b
if b = a + 1 and ar[b] < ar[a] then end if
swap ( ar[b] , ar[x] )
swap ( ar[a] , ar[b] )
b←b+1, x←x+1, a←a+ 1
if ctr = b − 1 then
ctr ← ctr + 1 PASS 9:
end if if b = a + 1 and ar[b] < ar[a] then
end if swap ( ar[b] , ar[x] )
swap ( ar[a] , ar[b] )
b←b+1, x←x+1, a←a+ 1
if ctr = b − 1 then
ctr ← ctr + 1
PASS 4: end if
if b = a + 1 and ar[b] < ar[a] then end if
swap ( ar[b] , ar[x] )
swap ( ar[a] , ar[b] )
b←b+1, x←x+1, a←a+ 1
if ctr = b − 1 then
ctr ← ctr + 1 PASS 10:
end if if b = a + 1 and ar[b] < ar[a] then
end if swap ( ar[b] , ar[x] )
swap ( ar[a] , ar[b] )
b←b+1, x←x+1, a←a+ 1
if ctr = b − 1 then
ctr ← ctr + 1
PASS 5: end if
if b = a + 1 and ar[b] >= ar[a] then end if
swap ( ar[x] , ar[a] )
a←y, x←x+1 PASS 11:
end if
if x = y then
y←a
end if
PASS 6:
if b > a + 1 and ar[b] >= ar[a] then
swap ( ar[x] , ar[a] ) PASS 12:
x ← x + 1 , a ← a+ 1
end if if b = a + 1 and ar[b] >= ar[a] then
swap ( ar[x] , ar[a] )
a←y, x←x+1
end if
FEW NOTES:
1. It is noticeable up till now that our aim has been to
keep elements from ‘a’ to ‘b − 1’ and elements from
‘y’ to ‘a − 1’ sorted (for a >= y). PASS 13:
if b = a + 1 and ar[b] >= ar[a] then
2. Another thing worth observing is that elements swap ( ar[x] , ar[a] )
from ‘a’ to ‘b-1’ are less than the elements from ‘y’ to a←y, x←x+1
end if
‘a-1’, (provided a >= y). This means that the first sub
array can be accessed in sorted order from ‘a’ to ‘b-1’
and then from ‘y’ to ‘a-1’.
PASS 7:
Till now, we had assumed that the value of the PASS 14:
variable ‘ctr’ to be equal to ‘b’. This was only because if b = a + 1 and ar[b] < ar[a] then
ar[ctr] was less than or equal to ar[ctr+1] i.e. the array swap ( ar[b] , ar[x] )
swap ( ar[a] , ar[b] )
starting from ‘b’ was sorted. However, to preserve the b←b+1, x←x+1, a←a+ 1
two conditions stated in Pass 6, we make a swap that
if ctr = b − 1 then
costs us the order of the two sub arrays. We solve this ctr ← ctr + 1
dilemma by swapping ar[ctr] with ar[ctr+1] and end if
increment ‘ctr’. We keep on doing this until ar[ctr] end if
701
2016 International Conference on Advanced Communication Control and Computing Technologies (ICACCCT)
if ctr = b − 1 then
PASS 22:
ctr ← ctr + 1
end if In the previous pass, ’b’ and ’ctr’ have again gone
end if
out of bounds. This condition is similar to that of
Pass 19 but with different side condition.
if b > j and a > x and ctr = b and a = y then
PASS 16: b ← y, ctr ← b, a ← x
end if
if x = y then
y←a
end if
702
2016 International Conference on Advanced Communication Control and Computing Technologies (ICACCCT)
T(n) = 2T(n/2) + C2
T (n) = 2[2T (n/4) + C2] + C2 T (n) = 4T (n/4) + 3C2
703
2016 International Conference on Advanced Communication Control and Computing Technologies (ICACCCT)
7.1 AVERAGE CASE noticed that the code slows down for very large
values of ‘ n ’ The instability of this algorithm is also
We consider the average case to be an array with a cause of concern.
partial order in it.
Future improvements can be made to enhance the
performance over larger number of input array. Since
we have the minimum and maximum value of the sub
array at any time, instead of starting from the
beginning, we can combine the current logic with an
end first search to reduce the number of iterations.
Regarding its stability, as mentioned earlier, this
algorithm can be made stable by increasing the
number of pivots but this would lead to other
complications. Any improvement though, however
trivial, would be highly appreciated.
R EFERENCES
704