0% found this document useful (0 votes)
145 views5 pages

A New Optimized Version of Merge Sort

The document discusses a new optimized version of the merge sort algorithm that is proposed to sort data faster than the standard merge sort. It introduces merge sort and explains that sorting algorithms are important for organizing large amounts of data efficiently. The proposed method aims to improve upon standard merge sort which has a time complexity of O(nlogn).
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
145 views5 pages

A New Optimized Version of Merge Sort

The document discusses a new optimized version of the merge sort algorithm that is proposed to sort data faster than the standard merge sort. It introduces merge sort and explains that sorting algorithms are important for organizing large amounts of data efficiently. The proposed method aims to improve upon standard merge sort which has a time complexity of O(nlogn).
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

2023 11th International Conference on Emerging Trends in Engineering &

2023 11th International Conference on Emerging Trends in Engineering & Technology - Signal and Information Processing (ICETET - SIP) | 979-8-3503-4842-2/23/$31.00 ©2023 IEEE | DOI: 10.1109/ICETET-SIP58143.2023.10151579

Technology - Signal and Information Processing (ICETET - SIP)

A New Optimized Version of Merge Sort


Abhishek Tyagi Anil Kumar Ahlawat
Department of Computer Science & Engineering, Department of Computer Science & Engineering
KIET group of institutions KIET group of institutions
(Krishna Institute of Engineering and Technology) (Krishna Institute of Engineering and Technology)
Ghaziabad, U.P INDIA. Ghaziabad, U.P INDIA.
[email protected] [email protected]

Abstract-The ordering of items of an array/list in all around the world and is seen as a necessary task.
a certain sequence is one of the crucial topics in In daily life, the most significant sorting application
computer science. Sorting algorithms determines the is utilised. For example: Phone book faster access to
new order of elements in a data structure typically in contacts, income tax files, contents of tables,
arrays and lists, they do so based on a comparison libraries access, dictionaries, search engine [6].
operator. The effectiveness of these sorting algorithms
is determined by calculating their time and space
complexity. Financial institutions and commercial
enterprises organize much of the information by
sorting. They must deal with a large amount of data.
There might be millions of data rows. Now we have
enough space but less time that is also one the major
reasons of microservices architecture is being used
nowadays. The microservice design enables the fast,
frequent, and reliable delivery of large, sophisticated
applications. Hence, the algorithms must be lightning
fast. The purpose of this study is to improve on the
classic Merge Sort Algorithm and create a new
approach with a faster execution time. Merge sort is a
popular method for sorting large datasets since it is
typically efficient and straightforward to implement.
The proposed method surpasses the standard Merge Fig.1. A useful Big-O Complexity Graph to visualize how the
Sort algorithm, which has a time complexity of O(nlog2 number of operations and time grow as number of elements
n). The proposed approach has been examined, increases.
executed, and compared, with favourable findings and
the suggested modified mergesort algorithm is 12.12%
faster than the usual merge sort. II. PROPOSED ALGO

Keywords- Divide and Conquer; External Sorting; Merge Sort, one of the Divide and Conquer
stack; Recursive; Merge Sort sorting algorithms, has a wide range of applications.
In the case of big arrays, this sorting method has a
I. INTRODUCTION temporal complexity of O(nlog2n) and can
outperform the Insertion sort [7].When the array or
The organisation of items in a specified sequence list is a reverse order list ,it is more fast even more
has always been one of the key and fundamental than Quick sort [8].
challenges of computer science. These issues are
addressed by a variety of sorting algorithms. Some A. Merge Sort Working Process:
of these algorithms are simple and spontaneous, like Consider it a recursive procedure that splits the
Insertion Sort while others are extremely complex, array into two half repeatedly until it can no longer
like Quick Sort yet provide quick results [1], [2]. be divided further. This means that splitting will stop
Almost all sorting algorithms are problem specific if the array becomes empty or has just one element
[3]. While some algorithms do well with tiny data, left, which is the base conditions and the reason for
some perform well with vast data. While some can the recursion to finish as a single element is already
handle lists with repeated values, some are better sorted. If the array has multiple elements, divide it
suited to floating point values. We usually sort the in half and recursively apply the merge sort on each
lists in statistical order or in lexicographical order, half. After sorting both portions, the merging
in ascending or descending order [4], [5]. In their process is used. The merge technique combines two
own fields of programming, many software smaller sorted arrays into a bigger sorted array. The
engineers rely on various sorting algorithms, such as Divide and Conquer concept serves as the
Merge, Fast sort, etc. Sorting is a common practise foundation for the proposed sorting method

979-8-3503-4842-2/23/$31.00 ©2023 IEEE


Authorized licensed use limited to: PONTIFICIA UNIVERSIDADE CATOLICA DO RIO DE JANEIRO. Downloaded on April 03,2024 at 23:27:20 UTC from IEEE Xplore. Restrictions apply.
discussed in this work. It is a modified form of the faster. So instead of having only one base condition,
standard Merge Sort. Although it runs more quickly we have three base conditions in the modified
than the conventional algo in both big and small version of the conventional Merge Sort. one for only
lists, it still has a runtime complexity of O(nlog2 n). one element in an array that is already sorted and one
The idea is to reduce recursive calls. because for two elements in the array, and the third for when
recursion involves lots of overheads. The "call three elements in the array.
stack" is something used by recursive functions. A
function is called by a programme, and that function III. PSEUDO CODE FOR THE PROPOSED
rises to the top of the call stack. This is like a pile of ALGORITHM.
books. You add things one at a time. Then, when you
are ready to take something off, you always take off Algorithm:MERGE_SORT(int[] array)
the top item. This recursion involves lots of Where, array:array of elements
overheads and thus results in an increment in
execution time. larger the number of elements,larger 1.find n=array.length
will be the recursive stack and larger will be the
execution time. In conventional merge sort recursive 2.Base condition
call stops when the array becomes empty or has only 2.(A) when n==3 array have only three element
one element after that merging process starts. i. CALL SortThree(array)
ii. return;
B. Conventional MergeSort algo 2.(B) when n==2 array have only two element
i. CALL SortTwo(array)
Algorithm:MERGE_SORT(int[] array) ii. return;
Where, array:array of elements 2.(C) when n==1 array have only one element
i.return;
1.find n=array.length
3.Find mid =n/2;
2.Base condition
2.(A) when n==1 array have only one element 4.Make a left array int[] left=new int[mid]
i.return; 4.(A) copy element from array(0 to mid)

3.Find mid =n/2; 5.Make a right array int[] right=new int[n-mid]


5.(A) copy element from array(mid to n)
4. Make a left array int[] left=new int[mid]
4.(A) copy element from array(0 to mid) 6.CALL MERGE_SORT(left)

5.Make a right array int[] right=new int[n-mid] 7.CALL MERGE_SORT(right)


5.(A) copy element from array(mid to n)
8.CALL MERGE(left,right,array)
6.CALL MERGE_SORT(left)
9.STOP
7.CALL MERGE_SORT(right)
Sub-Algorithm: SortTwo(int[] numsary)
8.CALL MERGE(left,right,array) This algo will order the array of size 2
Where, numsary:array of telements size 2
9.STOP
1.Init temp=0
So to improve execution time we can modify the
base case of this conventional algorithm. One thing 2. if(numsary [0]> numsary [1])
worth noticing is that in merge sort during the divide (2.A) temp= numsary [0];
phase when we have just one element in the array we (2.B) numsary [0]= numsary [1];
return because a list with a single element is already (2.C) numsary [1]=temp;
sorted. when we have just two elements in an array
then we can arrange them in the correct order by just Sub-Algorithm: SortThree(int[] numsary)
comparing them in O(1) time and can prevent two This algo will order the array of size
recursive calls. Similarly during the divide phase Where, numsary:array of elements size 3
when we have an array of size three, that is three 1.Init temp=0;
elements we can arrange them in the correct order by
at most three comparisons which are also an O(1) 2.if (numsary [1] < numsary [0])
time operation and can prevent three recursive calls. (2.A) temp = numsary [0];
hence overall execution of the algorithm will be (2.B) numsary [0] = numsary [1];

Authorized licensed use limited to: PONTIFICIA UNIVERSIDADE CATOLICA DO RIO DE JANEIRO. Downloaded on April 03,2024 at 23:27:20 UTC from IEEE Xplore. Restrictions apply.
(2.C) numsary [1] = temp; • The number of items is denoted by n.

3. if (numsary [2] < numsary [1]) • Because we continually split the sub-arrays
(3.A) temp = numsary [1]; into two equally sized pieces, doubling the
(3.B) numsary [1] = numsary [2]; amount of items n requires just one more
(3.C) numsary [2] = temp; step of divisions d. The fig.3 below shows
(3.D) if (numsary [1] < numsary [0]) that four items require two division steps,
(i)temp = numsary [0]; while eight elements require only one more
(ii) numsary [0] = numsary [1]; step. Thus the number of division stages is
(iii) numsary [1] = temp; log2 n.

Sub-Algorithm: MERGE(int[] left_ary,int[]


right_ary,int[] array)

1.Init nleft=left.length , nright=right.length, k=0,


i=0, j=0

2.Repat while(i<nleft && j<nright)


(2.A) if(left_ary [i]<= right_ary [j])
(i) array[k++]=left[i++]

(2.B) else
(i)array[k++]= right_ary [j++] Fig.3. Divisions in merge sort

• We must combine a total of n components


3.Repeat while(i<nleft) at each merge step. Because there are no
(3.A) array[k++]= left_ary [i++] nested loops in the merging operation, it is
done with linear complexity. When the
4.Repeat while(j<nright) array size is twice, the merging time also
(4.A) array[k++]= right_ary [j++] doubles. As a result, the overall effort is the
same at all merging levels. As a result, we
have n items multiplied by log2n division
IV. PICTORIAL REPRESENTATION and merging phases.

• The functions SortTwo(int[] nums) do not


have any loop this function simply do
comparison at most two times. So this
function is a constant time function. So
have a Time Complexity of O(1).

• The functions SortThree(int[] nums) do not


have any loop this function simply do
comparison at most three times. So this
function is constant time function. So have
a Time Complexity of O(1).

• Since the same steps occur whether the


array is already sorted, the best and worst-
case times are the same. Thus, the time
complexity of Proposed algo is O(nlog2n)
in all three cases Best, Worst and Average
Fig.2. Pictorial Representation of Proposed Algo case.
V. TIME COMPLEXITY

The following factors are taken into account


while assessing the time complexity of the newly
suggested improved merge sort:-

Authorized licensed use limited to: PONTIFICIA UNIVERSIDADE CATOLICA DO RIO DE JANEIRO. Downloaded on April 03,2024 at 23:27:20 UTC from IEEE Xplore. Restrictions apply.
TABLE I. Time taken to sort elements between MergeSort and
Proposed Algo
NUMBER OF TIME(ms) BY TIME(ms) BY
ELEMENTS MERGE PROPOSED
SORT ALGO

1K 0 0
10K 3 2
100K 46 24
1000K 234 167
10000K 1768 1592
50000K 9533 8695
70000K 15024 13040
Fig.4. Recursive tree for the proposed algorithm. 90000K 19307 17065
100000K 21347 18524

VI. SPACE COMPLEXITY The above table have been plotted in graphs as
shown in Fig. 5 and Fig.6.
The function MERGE_SORT(int[] nums) of the
proposed algorithm Divide the array into two parts
the left part and the right part. we store left part and
right part in temporary(auxiliary) arrays and then
280
use original array to store the completely merged
array. Thus, In proposed algorithm all elements are 245
copied into an auxiliary array so O(N) auxiliary
Execution Time( ms)

210
space is required for proposed algorithm. Where N
is the total number of elements. 175
140
105
VII. COMPARATIVE EXECUTION 70
ANALYSIS AND ALGORTIHM
BENCHMARKING 35
0
The time that passes between the beginning and 1K 10K 100K 1000K
finish of an event is generally referred to as the
elapsed time. These are two methods for finding Number of Elements(n)
elapsed time in Java. The current time is given via TIME BY MERGE SORT
the currentTimeMillis() method in milliseconds. The TIME BY PROPOSED ALGO
current time in nanoseconds is returned by the
Fig.5. Comparative execution times of MergeSort and Proposed
nanoTime() method [9].We may calculate the
algorithms.
amount of time that has passed since the execution
of a method by comparing the time values before and
after it. In our paper we used currentTimeMillis() 24000
Execution Time( ms)

21600
method to find the execution in milliseconds. The 19200
suggested algorithm's execution times have been 16800
14400
determined for various values of n. (number of array 12000
9600
elements).These execution times are compared to 7200
the execution times of standard Mergesort for 4800
2400
various values of n (number of array elements). 0
10000K 50000K 70000K 90000K 100000K
The Table 1 shows the comparison between the Number of Elements(n)
time taken in sorting randomly generated elements
between the proposed algorithm and the
TIME BY MERGE SORT
conventional merge sort algorithm.
TIME BY PROPOSED ALGO
Fig.6. Comparative execution times of MergeSort and Proposed
algorithms

Authorized licensed use limited to: PONTIFICIA UNIVERSIDADE CATOLICA DO RIO DE JANEIRO. Downloaded on April 03,2024 at 23:27:20 UTC from IEEE Xplore. Restrictions apply.
TABLE II. Time taken to sort elements between Standard MergeSort and Proposed Algo and Percentage Improvements

NUMBER OF TIME(ms) BY TIME(ms) BY DIFFERENCE IN PERCENTAGE


ELEMENTS MERGE SORT PROPOSED ALGO TIME(ms) IMPROVEMENT(%)

1K 0 0 0 0
10K 3 2 1 33.33333333
100K 46 24 22 47.82608696
1000K 234 167 67 28.63247863
10000K 1768 1592 176 9.954751131
50000K 9533 8695 838 8.790517151
70000K 15024 13040 1984 13.20553781
90000K 19307 17065 2242 11.61236857
100000K 21347 18524 2823 13.22434066
Total 67262 59109 8153 12.12125717

According to the graph above in Fig.5 and shortcoming of the traditional Merge Sort approach.
Fig.6 , when n=1000, both Conventional Merge Sort It still needs auxiliary space of O(n). In the future,
and the suggested modified mergesort require we will endeavour to solve such a situation.
almost the same amount of time to run. As n grows,
the execution time of Merge Sort exceeds the
execution time of the suggested Algorithm. Which x. REFERENCES
indicates that proposed algorithm is fast than the
conventional merge sort. The above Table II. shows
the detailed comparison and the percentage [1] I. T. Adamson, "Sorting," in Data structures and
performance improvement. We can see from the algorithms A First Course, Springer, 2012, pp. 79-85.
Table II. that There has been an average [2] A. A. S. S. A. S. C. Pira, "“Doubly Inserted Sort: A
improvement of about 12.12 per cent is achieved. Partially Insertion Based Dual Scanned Sorting
Algorithm”," in Emerging Research in Computing,
Thus, the suggested modified merge sort algorithm
Information, Communication and Applications
is about 12.12% quicker than the usual merge sort. (ERCICA), 2015.
[3] D. M. D. a. P. S. Y. J. L. Wolf, "A Parallel Sort Merge
VIII. CONCLUSION Join Algorithm for Managing Data Skew," IEEE
Transactions on Parallel and Distributed Systems, vol. 4,
The time complexity of both the standard and pp. 70-86, 1993.
suggested techniques is O(nlog2n).The proposed [4] D. Knuth, "Sorting and Searching," in The Art of
algorithm runs faster than the conventional merge Computer programming Sorting and Searching,
sort. The proposed algorithm prevents recursive Addison- Wesley,, 1998.
calls on arrays of size 3 and 2 by putting elements of [5] R. E. Neapolitan, Foundations of Algorithms (Fifth
arrays of size 3 and 2 in the correct order in constant Edition), Jones & Bartlett Learning, 2015.
time. This modification in the base case of [6] M. P. C. a. M. J. S. Somani, "Comparative Analysis &
conventional merge sort results a new modified Performance of Different Sorting Algorithm in Data
version of the mergesort which is about 12.12% Structure," International Journal of Advanced Research
in Computer Science and Software Engineering, vol. 3,
faster than the conventional one.
no. 11, pp. 500-507, 2013.
Due to the prevalence of Merge Sort in file
[7] "www.ijarcsse.com/docs/papers/Volume_3/3_March201
storage and databases, a modified version of the 3/V3I3-0319.pdf," ijarcsse, March 2013. [Online].
method requires less work from the programmer in
[8] K. W. Robert Sedgewick, "Algorithms, Part I,"
order to provide quick access to an item. Cousera,Princeton University, 2022.
[9] Oracle JAVA Docs, "JAVA 10 lang Systems," Oracle,
IX. FUTURE SCOPE 2022. [Online]. Available:
https://docs.oracle.com/javase/10/docs/api/java/lang/Sys
Although the suggested technique sorts a large tem.html. [Accessed 1 2023].
list of elements in less amount of time as compared
to standard Divide and Conquer MergeSort
algorithm, it cannot eliminate the primary

Authorized licensed use limited to: PONTIFICIA UNIVERSIDADE CATOLICA DO RIO DE JANEIRO. Downloaded on April 03,2024 at 23:27:20 UTC from IEEE Xplore. Restrictions apply.

You might also like