0% found this document useful (0 votes)
139 views

Performance Analysis of Heap Sort and Insertion Sort Algorithm

In the era of new technology, we have huge amount of data to deal with arranging the huge amount of data has remained a big challenge. This research paper includes two types of sorting algorithm, Heap Sort and Insertion Sort and also their performance analysis on thebasis of running time along with their complexity. This paper includes the algorithms and theirimplementation in Java programming language. For theresults of this research study,the comparison ofthese two sorting

Uploaded by

WARSE Journals
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
139 views

Performance Analysis of Heap Sort and Insertion Sort Algorithm

In the era of new technology, we have huge amount of data to deal with arranging the huge amount of data has remained a big challenge. This research paper includes two types of sorting algorithm, Heap Sort and Insertion Sort and also their performance analysis on thebasis of running time along with their complexity. This paper includes the algorithms and theirimplementation in Java programming language. For theresults of this research study,the comparison ofthese two sorting

Uploaded by

WARSE Journals
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

ISSN 2347 - 3983

Volume
Humaira Ali et al., International Journal of Emerging 9. No.
Trends 5, May 2021
in Engineering Research, 9(5), May 2021, 580 – 586
International Journal of Emerging Trends in Engineering Research
Available Online at http://www.warse.org/IJETER/static/pdf/file/ijeter08952021.pdf
https://doi.org/10.30534/ijeter/2021/08952021

Performance Analysis of Heap Sort and Insertion


Sort Algorithm
Humaira Ali1, Haque Nawaz2, Abdullah Maitlo3, Inayatullah Soomro4
1
Department of Computer Science, Sindh Madressatul Islam University, Karachi, Sindh, Pakistan,
[email protected]
2
Department of Computer Science, Sindh Madressatul Islam University, Karachi, Sindh, Pakistan,
[email protected]
3
Department of Computer Science, Shah Abdul Latif University, Khairpur Mirs, Sindh, Pakistan,
[email protected]
4
Department of Mathematics, Shah Abdul Latif University, Khairpur Mirs, Sindh, Pakistan,
[email protected]


ABSTRACT Sorting algorithm works as if the input is given like (7, 6, 4,
9, 2, 0, and 1) so it gives us output in sequential order (0, 1,
In the era of new technology, we have huge amount of data to 2, 4, 6, 7, and 9). For the analysis of best case numbers is
deal with arranging the huge amount of data has remained a organized in sequential order and in average case number is
big challenge. This research paper includes two types of organized in unsystematic order, and in worst case number is
sorting algorithm, Heap Sort and Insertion Sort and also their organized in reducing order.
performance analysis on the basis of running time along with
their complexity. This paper includes the algorithms and their
So it is clear that sorting is important for managing data.
implementation in Java programming language. For the
Due to their major role in managing data appropriately,
results of this research study, the comparison of these two
many algorithms have been discovered for example, bubble
sorting algorithms with different type of the data at running
sort, quick sort, merge sort, selection sort etc...
time such as Large, Average, and Small. In Large, data
pass100 integers in the array. For Average data pass 50 These all algorithms work for different domains. Some of
integers in the array and for Small data pass10 integers in the these algorithms are useful for sorting small data, some for
array. It checks that, which sorting technique is efficient large and some for average data. However, depends on their
according to the input data. Then identifies the efficiency of performance and time complexity in programming
these algorithms according to this data three cases used that is languages like java[3]. An Insertion Sort performs with
Best, Average and Worst Case. The result of this analysis is order of n^2 and Heap Sort perform the order of nlogn [4].
showing with the help of graphs to show that how much time
1.1 Insertion Sort Algorithm Working
both algorithms take while given the desired output.
As shown in figure 1 insertion sort divides the input data
Key words: Array, Complexity, Heap Sort, Insertion Sort, into sorted and unsorted data and then compares the
Performance, Sorting Algorithm. unsorted part with the sorted data, if the data in the unsorted
part is less than sort that data is in its accurate position[5]
1. INTRODUCTION [6].

An algorithm can be used in both mathematical and computer


science field. The term sorting algorithm means arranging
data in a certain/sequential order [1]. Whereas, both Heap
Sort and Insertion Sort algorithms are comparison based
algorithms. Sorting algorithms are also classified as
complexity of the algorithm which includes (worst, average
and best case) according to the size of the input data, and
check the stability how much memory is used.

The efficiency of the algorithm depends upon the time and Figure 1: Insertion Sort Working Process
space taken by the given data [2].

580
Humaira Ali et al., International Journal of Emerging Trends in Engineering Research, 9(5), May 2021, 580 – 586

1.2 Insertion Sort Algorithm 1.4 Heap Sort Algorithm


Take Var l, j ,tmp;
mx-heapfy (b, j)
For l = 1 to the length of the array
l left(j)
Assign temp = a[l];
r right(j)
k = l;
if l heap-size[b] and b[l] > b[j]
While (j > 0 && a[j - 1] > tmp )
then largest j
Do Assign a[j] = a[j - 1]
if r heap-size[b] and b[r] > b[largest]
Assign j=j-1
then largest r
A[j] = tmp;
if largest j
End of while loop
then exchange b[j] b[largestl]
End of for loop[7]
max-heapfy(b, largest)[7]
1.3 Heap Sort Algorithm
Heap Sort is an improved sort algorithm of selection sort [8]. 2. LITERATURE REVIEW
This is performed on the heap data and heap is basically the
complete binary tree. There are 2 basic natures of heap that is According to V.P.Kulalvaimozhi et al, while comparing an
max and min [9]. In maxi heap the parental node is bigger algorithm after executed, it uses the computer’s Central
than its child and in mini heap the parental node is fewer than Processing Unit (CPU) to perform operations and its memory
its child [8]. Figure 2 shows the working process of heap sort. to hold the program and data. Researcher used different
sorting algorithms and then perform operations on all of them
[7][10]. The algorithm which they used for the efficiency of
the algorithms are bubble sort, insertion sort, shell sort,
binary tree sort, heap sort, quick sort, merge sort and radix
sort. In that analysis, the efficiency of the all algorithms were
determined by the number of comparison that each algorithm
do while running. Every algorithm has different number of
comparison so the efficiency of all the algorithms may vary as
it depends upon the algorithm used. Whereas, all these
algorithms were implemented in C++ language[7].

Matej Hul´ın, provide a comprehensive analysis of the


algorithms that include pseudo codes [11], discussion of
stability and space and time analysis in the worst, average and
best case. Their research is divided in different parts. In the
first Part, he described all the algorithms that use to perform
work on the algorithm’s, the description. It includes how the
algorithm works, there pseudo code, and the stability by
checking the time and how much the algorithm space takes
[2]. Researcher selected C++ to implement the sorting
algorithms. The working environment on which these
algorithms were tested. According to him the data that used
for finding the efficiency of the algorithms has different
sequences. Then all the algorithms were measured by
dividing them into different parts and included the results of
analysis in graphs[11].

Gaurav Kocher, Nikita Agrawal, provide a comparison based


sorting algorithms. Perform the analysis of these comparison
based algorithm for same number of data. Also mention that
each and every application has an algorithm and the
Figure 2 - Heap Sort Working Process efficiency of the application can depend on these algorithms.

581
Humaira Ali et al., International Journal of Emerging Trends in Engineering Research, 9(5), May 2021, 580 – 586

Also perform the analysis on both the Central Processing Unit


(CPU) and Graphics Processing Unit GPU. They worked on
insertion sort, bubble sort, quick sort, and heap sort and
merge sort[10] [12]. They concluded that input in integer
takes less time as compare to strings. According to their
findings, selection sort and quick sort was not be used for
large types of data[13].

Dr. I. Lakshmi, perform analysis on four different types of


algorithm such as Quick sort, insertion sort, heap sort, merge
sort. Researcher analyzed the time complexity [14], [15]-[18]
The analysis of the study is to define which algorithm is useful
when we have a confusing set of data. The analysis is
depending upon best, average and worst case. In the analysis
researcher used C# environment and the data was randomly
used [10].

3. METHODOLOGY

This research paper includes 3 phase. The first phase includes


both the algorithms implemented in java language, in the
second phase finding the complexity of both algorithms is
explained and the third phase compare and analyze the result
of both algorithms and also implement them in java
programming

The application both algorithm is implemented in java.


Moreover, in second phase pass different types of data and
check the complexity and showing the analysis with the help Figure 4: Flowchart shows Working Mechanism of both Sorting
Algorithms
of graph. The third phase check the running time of both
algorithms and then compare them on the basis of type of the
data. The overall process of this study work is mentioned in 4. IMPLEMENTATION AND RESULT ANALYSIS
three phases. Figure 3 represents the phases of study.
The analysis of this study is divided into three min phses.in
the first phase the implementation is done in java language, in
Phase I second phase check the complexity by using three strategies
Implementation that include best, average and worst case. In the last phase the
comparison of both heap sort and insertion sort is done.

Phase II 4.1 Phase I (Implementation):


Both the algorithms insertion sort and heap sort is
Check Complexity implemented in java programming language. And the data is
based on arrays. In both algorithms three types of data is
tested that is (Small, Average and large data) that is the
Phase II length of the array. These three types of data set are tested on
Comparison three cases best, average and worst case. Table 1 presents data
size.
Figure 3: Phases of Study
Table 1: Data Set Size
These all three process consists on different methods and
gives a result to move forward in the next step, and then find Data Array Size
according to the graph that which is the best algorithm. Small 10
Moreover, Figure 4 shows the working mechanism of both Average 50
sorting algorithms. Large 100

582
Humaira Ali et al., International Journal of Emerging Trends in Engineering Research, 9(5), May 2021, 580 – 586

4.2 Phase II (Check Complexity):

In this phase, the complexity can be check in three ways: Large Data
0.062
1. Best Case
0.061
2. Average Case
0.06
3. Worst Case
0.059
So according to these cases in best case the data in array is Time in Seconds
arranged in sequential order, in average case the data in array 0.058
Time in Seconds
is arranged in random order and in worst case the data in 0.057
array is arranged in decreasing order.
0.056
Analysis for Best Case: Insertion Sort Heap Sort

The running time is depending on these input data. The


running time of all these three cases is different in both Figure 7: Represent Large Data In Best Case.
insertion sort and heap sort. Some data can take too much
time and some data take less time. So according to the In best case the insertion sort performance is good for small
running time check the complexity of these algorithms that and average data and for large data the performance of heap
which algorithm is efficient for which type of data. Figure 5 sort is best.
presents small data in best case, Figure 6 presents average
data in best case, and Figure 7 presents large data in best case. Analysis for Average Case:

In the analysis of average case the data that is tested is in


Small Data unsystematic form. The analysis can be performing on large,
small and average set of data. Figure 8 presents small data in
0.0575 average case, Figure 9 presents average data in average case,
0.057 and Figure 10 presents large data in average case.

0.0565
Time in Seconds Small Data
0.056 Time in Seconds 0.0582
0.058
0.055 0.0578
5 Insertion Sort Heap Sort 0.0576
0.0574 Time in Seconds
0.0572 Time in Seconds
Figure 5: Represent Small Data In Best Case. 0.057
0.0568
0.0566 Insertion Sort Heap Sort

Average Data Figure 8: Represent Small Data In Average Case.


0.06
0.059
0.058 Large Data
0.057
0.0635
0.056
0.055 Time in Seconds
0.063
0.054 Time in Seconds
0.053 0.0625
Time in Seconds
0.062 Time in Seconds
Insertion Sort Heap Sort
0.0615
Insertion Sort Heap Sort
Figure 6: Represent Average Data In Best Case.
Figure 9: Represent Large Data In Average Case.

583
Humaira Ali et al., International Journal of Emerging Trends in Engineering Research, 9(5), May 2021, 580 – 586

0.8
Large Data
Average Data
0.7
0.065 0.6
0.5
0.064 0.4 Time in Seconds
0.3 Time in Seconds
0.063
0.2
Time in Seconds
0.1
0.062 Time in Seconds
0
0.0615
Insertion Sort Heap Sort
Insertion Sort Heap Sort
Figure 13: Represent Large Data In Worst Case.
Figure 10: Represent Large Data In Average Case.
4. 3 Phase III (Comparison):
In average case the insertion sort works best for all types of
data as compared to heap sort. In this phase the comparison of heap and insertion sort is
performed by using the best, average and worst case on small,
Analysis for Worst Case: large and average data set. Figure 14 presents the comparison
of insertion and heap sort on small data set. Figure 15 presents
In the analysis of worst case the data that is tested is in the comparison of insertion and heap sort on average data set, and
decreasing form. The analysis can be performing on large, Figure 16 presents the comparison of insertion and heap sort on
small and average set of data. Figure 11 presents small data in large data set.
worst case, Figure 12 presents average data in worst case, and
Figure 13 presents large data in worst case.
0.066
0.064
Small Data 0.062
0.08 0.06 Best Case
0.058 Average Case
0.06
0.056
Worst Case
0.04 0.054
Time in Seconds
0.052
0.02 Time in Seconds
0.05
0
Insertion Sort Heap Sort
Insertion Heap

Figure 11: Represent Small Data In Worst Case. Figure 14: Represent The Comprison of Insertion and Heap Sort
on Small Data Set.

0.0585
Average Data 0.058
0.063
0.0575
0.062
0.057
0.061 Best Case
0.0565
0.06 0.056 Average Case
Time in Seconds 0.0555
Worst Case
0.059 Time in Seconds
0.055
0.058

0.057
Insertion Heap
Insertion Sort Heap Sort

Figure 12: Represent Average Data In Worst Case. Figure 15: Represent The Comprison of Insertion and Heap Sort
on Average Data Set.

584
Humaira Ali et al., International Journal of Emerging Trends in Engineering Research, 9(5), May 2021, 580 – 586

n (n+1) so highest order is O ( ). The complexity of heap


sort is O (n log n) for all the cases. Because the time
complexity of building a heap is O (n) and n-1 call heapify
0.08 that takes O (logn) and the complete time complexity is O (n
0.07 log n).
0.06
0.05 Best Case
0.04
Average Case
REFERENCES
0.03
0.02 Worst Case [1] P. K. Chhatwani, “Insertion Sort with its
0.01 Enhancement,” Int. J. Comput. Sci. Mob. Comput., vol.
0 3, no. 3, pp. 801–806, 2014.
[2] E. S. Al-jaloud, H. A. Al-aqel, and G. H. Badr,
“Comparative Performance Evaluation of Heap-Sort and
Insertion Heap
Quick-Sort Algorithms,” Int. J. Comput. Acad. Res., vol.
3, no. 2, pp. 39–57, 2014.
Figure 16: Represent The Comprison of Insertion and Heap Sort [3] S. Grover, “Performance Analysis of Heap, Merge, and
on Large Data Set. Insertion Sort,” in The 20th Winona Computer Science
Undergraduate Research Symposium, 2019, pp. 18–22.
5. RESULTS DISCUSSION [4] M. A. Bender, M. Farach-Colton, and M. A.
Mosteiro,“Insertion Sort is O(n log n),” Theory Comput
Syst, vol. 39, no. 3, pp. 391–397, Jun. 2006, doi:
The analysis of this study is divided into three main phases. In
10.1007/s00224-005-1237-z.
the first phase the implementation is done in java language, in
[5] C. C. Lwin, N. Z. Moe, and P. N. Wai, “A Study of
second phase check the complexity by using three strategies
Comparison for Sorting Algorithms Based on Data
that include best, average and worst case. In the last phase the
Sequences,” Univ. J. Creat. Innov. Res., vol. 01, no. 01,
comparison of both heap sort and insertion sort is done.
p. 4, 2020.
[6] M. A. Bender, M. Farach-Colton, and M. A. Mosteiro,
Now, according to the analysis the result of the running time
“Insertion Sort is O(n log n),” Theory Comput. Syst., vol.
that insertion sort takes in small data set is less than the heap
39, no. 3, pp. 391–397, Jun. 2006.
sort, and in average data the insertion sort work best but in
[7] V. P. Kulalvaimozhi, M. Muthulakshmi, R. Mariselvi,
worst case heap the performance of heap sort is better and in
G. S. Devi, C. Rajalakshmi, and C. Durai, “Performance
large data the performance of heap sort is good rather than
analysis of sorting algorithm,” Int. J. Comput. Sci. Mob.
insertion sort.
Comput., vol. 4, no. 1, pp. 291–306, 2015.
[8] V. Sharma, S. Singh, and D. K. S. Kahlon,
According to these conditions it is clear that insertion sort
“Performance Study of Improved Heap Sort Algorithm
takes less time in best and average case for small and average
and Other Sorting Algorithms on Different Platforms,”
data. But for large data the performance of heap sort is good,
Int. J. Comput. Sci. Netw. Secur., vol. 8, no. 4, pp.
so for large data use heap sort algorithm to save our time and
101–105, 2008.
complete the tasks with in minimum time.
[9] Z.-G. Zhu, “Analysis and Research of Sorting Algorithm
in Data Structure Based on C Language,” J. Phys. Conf.
6. CONCLUSION Ser., vol. 1544, no. 2020, pp. 1–5, May 2020, doi:
10.1088/1742-6596/1544/1/012002.
In the above analysis and comparison of both insertion sort [10] D. I. Lakshmi, “Performance Analysis of Four Different
and heap sort the insertion sort has fast running time for Types of Sorting Algorithms using Different
small and average data and for large data insertion sort takes Languages,” International Journal of Trend in Scientific
too much time so according to this analysis insertion sort is Research and Development, vol. Volume-2, no. Issue-2,
use for small and average data. Heap sort takes less time Jan. 2018.
than insertion sort in average and large data for both in [11] M. Hulín, “Performance analysis of Sorting
average and worst case and it has smaller difference in Algorithms,” Thesis, Masaryk University Faculty of
running time for large data set so heap sort is best for large Informatics, Pole-Ponava, Czechia, 2017.
data in every case. The time complexity of insertion sort in [12] R. Schaffer and R. Sedgewick, “The Analysis of
best case is O (n) because compare each element if it is not Heapsort,” Journal of Algorithms, vol. 15, no. 1, pp.
sorted then put in the correct position. And for worst and 76–100, Jul. 1993, doi: 10.1006/jagm.1993.1031.
average case the complexity is O ( ) because when the [13] P. Prajapati, N. Bhatt, and N. Bhatt, “Performance
input no in ‘n’so take n comparison+ n no of movements. Comparison of Different Sorting Algorithms,” p. 3,
Then write this 2(n) that is 2(1+2+3…+n) so the sum of 2017.
natural nois equal to 2n (n+1)/2 now the remaining value is

585
Humaira Ali et al., International Journal of Emerging Trends in Engineering Research, 9(5), May 2021, 580 – 586

[14] R. Schaffer and R. Sedgewick, “The Analysis of


Heapsort,” Journal of Algorithms, vol. 15, no. 1, pp.
76–100, Jul. 1993, doi: 10.1006/jagm.1993.1031.
[15] I. Ali, H. Nawaz, I. Khan, A. Maitlo, M. Ameen, and M.
Malook, “Performance Comparison between Merge and
Quick Sort Algorithms in Data Structure,” International
Journal of Advanced Computer Science and
Applications, vol. 9, no. 11, 2018, doi:
10.14569/IJACSA.2018.091127.
[16] S. M. Aqib, H. Nawaz, and S. M. Butt, “Analysis of
Merge Sort and Bubble Sort in Python, PHP, JavaScript,
and C language,” International Journal of Advanced
Trends in Computer Science and Engineering, vol. 10,
no. 2, pp. 680–686, Apr. 2021, doi:
10.30534/ijatcse/2021/311022021.
[17] M. A. Hingoro and H. Nawaz, “A Comparative Analysis
of Search Engine Ranking Algorithms,” International
Journal of Advanced Trends in Computer Science and
Engineering, vol. 10, no. 2, pp. 1247–1252, Apr. 2021,
doi: 10.30534/ijatcse/2021/1081022021.
[18] F. A. Agha and H. Nawaz, “Comparison of Bubble and
Insertion Sort in Rust and Python Language,” IJATCSE,
vol. 10, no. 2, pp. 1020–1025, Apr. 2021, doi:
10.30534/ijatcse/2021/761022021.

586

You might also like