Iterative and Recursive Algorithm Comparison Using Radix and Merge Sort

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Interative and Recursive Algorithm Comparison

Using Radix and Merge Sort


Hizkia William Eben
Faculty of Engineering Department of
Electrical Engineering
University of Indonesia
Jakarta, Indonesia
email: williamhizkia@gmail.com

Abstract—Sort is one a basic problem and fundamental This paper will be divided in four main chapters. Chapter
operation in computer science, which needs a deep I will discuss about introduction to sorting algorithm, the
understanding about how it’s work, that said its algorithm. second Chapter will be covering details of the discussed sort
Many types of sorting can be found, and mainly it’s divided by algorithm, and the visual representation of each algorithm
two types, Recursive Algorithm and Iterative Algorithm. To respectively, and the third Chapter will explain the result of
help understanding both types, this paper used radix sort as the experiment and graphical visualization of running time
representative of the iterative algorithm and merge sort as growth with varied data given, while the last Chapter will
representative of the recursive algorithm, to compare them to cover the conclusion.
see how both works and what differences we can find. The
programming language used to test both sort algorithm is II. BASIC THEORY
Python 3.7.2
A. Radix Sort
Keywords—sorting, algorithm, iterative, recursive, radix, Radix sort, like counting sort and bucket sort, is an integer-
merge.
based algorithm (i.e. the values of the input array are assumed
I. INTRODUCTION to be integers). Hence radix sort is among the fastest sorting
algorithms around, in theory. The particular distinction for
Sorting refers to ordering data in an increasing or radix sort is that it creates a bucket for each cipher (i.e. digit);
decreasing fashion according to some linear relationship as such, similar to bucket sort, each bucket in radix sort must
among the data items [1]. Sorting itself can be based on be a growable list that may admit different keys.
alphabetically or numbers. It can be done by ascending or
descending. Sort can easily reduce time for us to search in Below is the code of radix sort written in Python [2]:
something, like for example list of student names. We can
clearly find easily with names sorted in alphabetically rather
than scrambled names randomly.
Sorting itself can be divided by two main parts, which is
recursive algorithm and iterative algorithm. These two
algorithms differ in many parts. The one we can see clearly is
the time needed to sort, or we can say the resources it took
while sorting data. This is crucial when we want to use sorting
algorithm to sort huge amount of data, such as thousands or
millions.
This experiment will show the comparation between those
two algorithms and why we need to understand the basic
concept of these algorithms.
This paper won’t discuss about all types of sorting
algorithm out there, instead will use radix sort and merge sort
as representative of both iterative and recursive algorithm.
While there are many other types such as bubble sort, insertion Time complexity of radix sort is O(n∙k) [3].
sort, and so on for iterative sort, and there are heap sort and
quick sort for recursive sort, the reason to choose radix and To understand more about how radix sort works, below is the
merge sorting is because the two-sorting algorithm tends to be visual representation of the algorithm.
one of the fastest sorting algorithms in their respective
iterative and recursive algorithm. Many other sorting
algorithms cannot meet requirements in the tests when given
to sort a huge amount of data. Some of them crashed the
application used in testing or overflowing in recursive usage.
Interpreter is used in testing both radix and merge sort. The
reason is because interpreter (in this case python) support
storing data in a huge amount, like 1 million data, while
compiler (C++ for example) cannot store data in arrays until
the said amount of data.

©2019 IEEE
B. Merge Algorithm C. Random Number Generator
Merge sort is based on divide and conquer strategy In this experiment, we use varying number amount of data
technique which is a popular problem solving technique. We to clearly see when the two algorithm differs in time process.
will give a set of data items of length n as an input to the Hence, we need to create a random number generator.
algorithm or procedure, stored in an array A or any other data
structure which is best suited according to conditions. The This process can be done in Python by using random
merge sort algorithm is work as under module that can be used by import random in the text editor.
• Split array A from middle into two parts of length n/2. Then we use loops to generate the number and store it in an
array by appending it. The process can be shown by the code
• Sorts each part calling Merge Sort algorithm recursively. below
• Merge the two sorting parts into a single sorted list. [4]
Below is the code of radix sort written in Python [5]:

The program to create the random data and stores it in an


array was done with one line code, where the data is stored in
variable called List, and insertion of the data was done by for
loop and call random integer function.

As you can see, there are four variables in range and


randint parameters. The a and b variables can be set as how
many data do we need to run test. Take an example, one
thousand data, then we can put 0 in a variable and 1000 in b
variable. The x and y are the range of the random number. If
you set x and y as 0 and 1000 as well, it means you try to
generate random number ranging between 0 and 1000.

D. Running Time Calculation


We need to create code to calculate the actual running
time as well, for each experiment. The module we need to
make the running time calculation code is from time module
provided by Python. Below is the code for running time
calculation.
Time complexity for merge sort is 𝑂 𝑛(log(𝑛))[3]
The visual representation of how merge sort process is can be
seen below

There are two variables used to count in the running time


as well. The start variable indicates when will the running
time begin counting until it reaches the line where end
variable is declared. To count the running time, is simply by
subtracting both end and start variable to get time difference
when the #insert code here code is executed.
III. EXPERIMENT
In order to compare each algorithm we state before, as
mentioned an interpreter will be used, which is python version
3.7.2, and the device to test this experiments is a laptop with
Processor Intel(R) Core(TM) i5-5200U CPU @ 2.20GHz (4
cores CPUs) with memory of 4GB RAM
The individual data will be varying between 0 and 1000.
While the amount of data will by varying between 5 times
experiments, which 100, 1000, 10000, 100000, and 1000000
for every algorithm we tested. For each experiment, we run
three times. So, there will be fifteen experiments in total.
A. Table Reluts The y axis on the graphic shows the amount of time in
The result given by conducting the experiments will be seconds while the x axis is the number of experiments. The
shown in table below. Each column will represent each of the blue color is representing the merge sort running time while
algorithm’s running time. the orange color is representing the radix sort running time.

Radix Sort Merge Sort From the graphic visualization we can see the running time
Amount of Data gradually gets bigger as the amount of data sorted is increase.
(second) (second)
The graphic shows a clear significant difference after the
1st try 0.05796 0.00101 amount of data given is greater than one hundred thousand.
nd
100 2 try 0.03797 0.001014 B. Evaluation
3rd try 0.03399 0.00099 Based on the experiment we have done, we now can
evaluate the running time complexity for each algorithm. As
1st try 0.25784 0.00701 already mentioned in the basic theory, the upper bound of
nd radix sort is 𝑂(𝑛 ∙ 𝑘) while the merge sort is 𝑂 𝑛(log(𝑛)). To
1000 2 try 0.25187 0.01099
prove the concept of time complexity, we will input the largest
3rd try 0.28781 0.00699 number of the varying variable which is 1000000
st First for the radix sort, it has two variables, n and k. The n
1 try 1.85893 0.11991
nd
will be how much the amount of data being processed, while
10000 2 try 1.82995 0.11191 k is the number of bits required to represent largest element in
3rd try 1.82793 0.10495 the array which is 100, therefore we can put that k will be 8 as
27 = 128 and the bits start at 20. To begin testing the concept,
st
1 try 17.08350 1.36022 the equation will be as follows
nd
100000 2 try 17.50699 1.36623 𝑂 (1000000 ∙ 8) = 8000000
3rd try 16.91331 1.22827 As for the merge sort the upper bound equation will be
st
1 try 150.84628 16.42467 𝑂 1000000(log 1000000) = 6000000
1000000 2nd try 145.51251 15.43516 From the results, we can see that there are 2000000-time
unit difference for the sample that we test, with 1000000
3rd try 153.87894 16.08280 amount of data, and so it is proven for the merge sort has faster
running time than radix sort due to the result of the running
time complexity.
As we can see from the data shown in the table, recursive
method, in this case the merge sort algorithm, clearly much IV. CONCLUSION
faster than the iterative method, which is the radix sort. The Results of the experiments clearly shows us that iterative
data cannot be seen much difference when the amount of data
and recursive sorting methods are different. From the analysis
sorted is less than 1000. However, when the data approaches
and experiment above, we can summarize that the two types
1000, we can see merge sort still run below than one second,
while radix sort almost takes two seconds. When we see on a of sorting (iterative and recursive) represent by radix and
bigger input amount of data, such as one hundred thousand, or merge sort will results a small amount different of running
even one million, the difference is significant. This conclude time, but it will increase gradually as the amount of data
that merge sort (recursive method) is far faster than radix sort increased as well. In real life, we may not meet condition
(iterative method), especially when the amount of data sorted where we sort data in a small environment, but in a huge
is huge. amount instead, especially when talking about big data, data
science, data analytics, and so on.
The Graph below will represent the running time growth
comparison according from the result
From this experiment we can conclude that recursive is
far more efficient than iterative. And it was proven by
analyzing the upper bound and compare it with the results
from the experiment, be it the table results or the graph by far,
recursive has much shorter running time.

Also, by counting and calculate the upper bound we can


get that the iterative upper bound will show us that it has a
much higher number time unit, while the recursive upper
bound will give a much lower number, with 2000000-time
unit differences in algorithm time complexity.

And as a result, it is recommended for us to choose a


recursive type sort algorithm rather than the iterative type
because it has lesser running time and less resource
consumption, especially when talking with big or huge
amount of data.
REFERENCES [4] Kazim Ali, “A Comparative Study of Well Known Sorting
Algorithms” 2017 ISSN No. 0976-5697
[1] http://ecomputernotes.com/data-structures/searching-and-
sorting/what-is-sorting-type-of-sorting [5] http://interactivepython.org/courselib/static/pythonds/SortSearch/The
MergeSort.html
[2] Radix Sort by Isai Damier, geekviewpoint
[3] http://bigocheatsheet.com/

You might also like