You are on page 1of 10

Review Paper on Comparative Analysis of Selection Sort and

Insertion Sort
Zeeshan Ali Hassan Raza Hasiba Liaquat
Computer Sciences Computer Sciences Computer Sciences
Bahria University Bahria University Bahria University
Lahore Pakistan Lahore Pakistan Lahore Pakistan
alirajazeeshan@gmail.com Hasankhan1399@gmail.com hasiba.572@gmail.com

complexities in terms of the number of comparisons or the number


1. ABSTRACT
of iterations were carefully analyzed [1]. An algorithm is an
One of the most important problems in the field of computer unambiguous, step-by-step process for solving any given problem,
science is data ordering or data arrangement. The sorting algorithm which is guaranteed to be completed after a limited number of
is determined by the arrangement of numerical, alphabetic or steps. Sorting is usually regarded as the process of rearranging a
symbolic data in a statistical order in ascending or descending given data set in a certain order, and therefore the analysis and
order. The sorting problem is a problem that often arises in the field development of useful sorting algorithms is most important areas
of computer programming. Sorting is a process that arranges all of research in the field of CS/IT. Despite the fact that many new
elements of an array in a specific order. Sorting any data involves sorting algorithms are developed, a large number of computer
rearranging the information in descending order or in ascending science programmers depend on one of the sorting algorithms
order. Basically, data sorting can be either numeric or alphabetic.
based on comparison, such as: sorting by bubbles, sorting by
In the process of numerical sorting, we will sort the numerical
values either in ascending or descending order, as well as in inserts, sorting by choice, etc. Therefore, data sorting is an almost
alphabetical order, we will sort the data set alphabetically. To speed universal process and, therefore, is considered as the main activity.
up sorting, many different algorithms have been developed and One of the main problems in the field of computer science is the
improved. For this purpose, there are many types of sorting sorting of a list of data. It includes the arrangement of a data set of
algorithms for sorting a dataset, such as a selection sorting elements in ascending or descending order. A general definition of
algorithm, an insertion sorting algorithm, a quick sorting algorithm, any sorting problem is as follows:
a Radix sorting algorithm, a merge sorting algorithm, and a bubble Input: A sequence having any numbers n in some random order
sorting algorithm, as well as their variants. In this review paper, two [a1, a2, a3, up to an]
sorting algorithms of these algorithms are taken and investigated as Output: A permutation of the [a1, a2, a3………., an] input
Selection sorting algorithm and Insertion sorting algorithm and
sequence such that
their enhancements. We are going to discuss their variants as well
which show their enhanced form. In this review article, we are a1≤ a2≤a3≤………. an
going to compare the performance of the selection sorting For example, if the given input of numbers is 79, 51, 41, 37, 26, 58,
algorithm and the insertion sorting algorithm, which are commonly then the output sequence returned by the sorting algorithm will be
used in terms of their time and space complexity. Time complexity 26, 37, 41, 51, 58, 79. Because of the many numbers, sorting
is the most important factor for evaluating any sorting algorithm in algorithms that available. Best for a particular application depends
computer science. The complexity of Time basically shows the on various factors that have been generalized as:
execution time of the algorithms on a real machine or virtual 1. The size of the list (the number of items to sort).
machine. 2. The extent to which a given input sequence has already been
sorted.
KEYWORDS 3. Possible restrictions on the given input values.
Sorting-Algorithms, Selection-Sort, Enhanced-Selection-Sort, 4. The architecture of the system on which the sort operation will
Insertion-Sort, Enhanced-Insertion-Sort, Time-Complexity, Space- be performed.
Complexity. 5. Type of storage devices: main memory or disks is used.
Almost all the available sorting algorithms can be categorized into
2. Introduction two groups based on their main factors. The complexity of any
Sorting of any data is one of the most important and well-studied sorting algorithm and its respective effectiveness are directly
problems in the field of computer science. Many good algorithms corresponded. A standardized notation i.e. Big O(n) notation is
are known that offer various tradeoffs in efficiency, clarity, used to describe the complexity of any sorting algorithm. In this
memory usage, and other factors. However, these algorithms do not notation, the big O represents the time complexities and space
talk about the features of modern computer architectures that complexities of the algorithm and n represents the size of the input
significantly affect performance. A large number of sorting data set. The two types of sorting algorithms are O(n^2), which
algorithms have been developed and introduced. Their asymptotic
includes the bubble, insertion, selection sort and O (n log n) which is explained. The logic of the insertion sorting algorithm is
includes the merge, heap & quick sorting algorithms[2]. illustrated by an example. The algorithm code is implemented using
The data items to be sorted are usually stored in the data structure the C ++ programming language, Runtime for sorting using the
of the array. But this is not necessary, we can use any other linear insertion sorting algorithm is also provided. The insertion sorting
or non-linear data structure for storage in accordance with the algorithm is explained. The logic of the insertion sorting algorithm
nature of the data of the elements of the data set. In any work field is illustrated by an example. The code for the insertion sorting
there is large number of software applications that required sorting algorithm is written using the C ++ programming language.
the data in some order, it could be in ascending order or descending Runtime for sorting using the insertion sort algorithm is also
order.. When the data is sorted, some sorting algorithm must be provided. At the end, a comparison of sorting selection and
used for this. Therefore, research in the field of sorting algorithms insertion sorting algorithm is given in terms of time complexity.
is very important in the field of computer science. In the old days,
people thought that a business computer spent a considerable 3. Algorithms and Explantation
amount on sorting data. There are many basic algorithms, as well 3.1. Insertion Sort
as advanced algorithms that are best suited for a particular sorting
The insertion sorting algorithm is based on the technique according
or dataset task, but not for all sorting or dataset problems. We think
to which one element from the data set array is taken at each step
that they depend on the amount of data, the nature of the data, the to find its correct position in the given array. At the end, it moves
input format, or perhaps on a particular machine. There are many to the position to which it belongs in the sorted array. The insertion
reasons for choosing a sorting algorithm to solve a given data set. sort iterates through the input elements, increasing the sorted array
In this research paper, we are going to conduct a comparative at each step. The insertion sorting algorithm compares the current
analysis between the two most commonly used algorithms: item with the largest item in the sorted array. If the current element
insertion sort and selection sort. Each of the algorithms will be is larger than the other element, it leaves the element in its place
discussed and explained, and then we will compare both algorithms and moves on to the next element in this array. Then it finds its
based on their time complexity and space-complexity. correct position in the sorted array and moves the element to that
In this research paper, we tried to present a comparison-based position. This insertion sorting process is performed by shifting all
elements that are larger than the current element in the sorted array
analysis of insertion sort and Selection Sort and their
one position forwards[4].
Enhancements and explain these algorithms in a simple manner.
We also presented some previous comparative studies of sorting The insertion sorting algorithm is a simple sorting technique that
algorithms of some other people which have done in a good works the way we sort cards during a game. We believe that the
first card in our hand is already sorted. Then we select an unsorted
manner. But in the field of research and knowledge we can argue
map. If an unsorted card is larger than the first card in our hand, it
with people in a polite and civilized way the work of other people is placed on the right. If it is smaller than the first card we placed
which are great researcher, did hard work and much respectable on the left. In the same way, other unsorted cards are taken and put
people. It is also a big factor that the critical research is the base of in the right place.[5] [6].
knowledge.
Pseudo Code of Insertion Sorting:
At the end we will conclude which algorithm is best in terms of
time complexities and space complexities. Performance of INSERTION_SORT (B)
selection sorting algorithm and shell sorting algorithm is compared For k ← 2 to length [B]
in terms of running time. It is also a fact that shell sorting algorithm Do key ← B [k]
shows better performance than selection sort. Selection sorting
Insert A[k] into the sorted sequence B [1 . . . k - 1].
algorithm is more used because of its simpler structure. Variants of
Bubble sorting and Selection sorting are explained. Furthermore, m←k-1
Selection sorting, Enhanced Selection sorting, Enhanced Bubble While m > 0 and B[m] > key
sorting algorithms and Bubble sorting algorithms are compared in Do B [m + 1] ← B[m]
terms of number of comparisons, number of swaps and time it
m←m-1
takes[3].
Insertion sorting, selection sorting, merge sorting, quick sorting and B [m + 1] ← key
bubble sorting are compared in terms of time and space complexity. 3.1.1. C++ Implementation of Insertion Sorting:
As a result of this comparison, the quick sort algorithm is the Void Insertion_Sorting (int B [ ] , int tn)
fastest, and the selection sort algorithm is the slowest for large data
{
sets. When the number of datasets is larger, the performance of the
quick sort algorithm is better than the merge sort algorithm. As a For (k = 0; k< tn; k++) {
result, when the number of elements is greater, the performance of int tem = B [k];
the quick sort algorithm is better than the merge sort algorithm, and int m = k;
vice versa.
In this review, the document selection sorting algorithm and the while (m > 0 && tem < B [ m -1])
insertion sorting algorithm are compared in terms of time {
complexity and spatial complexity. The insertion sorting algorithm
B [m ] = B [ m-1]; in variables that increase at the front end and decrease at the rear
m= m - 1; end as the algorithm progresses.
At the second stage of the extended sorting algorithm, the first two
}
neighboring elements from the array are selected and compared. If
B [m] = tem; necessary, the insertion of elements is carried out in accordance
} with the order. Now the same process is performed as in the
} insertion sorting algorithm[7].
3.1.2. Working of Insertion Sort:
3.1.4. Flow Chart of Enhanced Insertion Sort:
1. Following array is given to be Sort.
99 59 19 49 39

2. We assume that the element of first index which is 99 is sorted.


Now take the element of second index 59 and store in variable key
or temp. Compare the value of key or temp variable with first
element, If the first element is greater than key or temp, then temp
or key is placed in front of first element, In this case 99 is moved
right side to make room for 59
59 99 19 49 39

3. The first two elements 19 and 99 has been sorted. Now take the
element of third index 11 and compare it with the elements on the
left side. Placed it where the element smaller than it. In this case 19
is placed at first index. 59 and 99 has been moved one step forward
to make room for 11.
19 59 99 49 39

4. Now three elements 19, 59 and 99 has been sorted. Take the
element of fourth index 49 and compare it with the elements on the
left side. Placed it where the element smaller than it. In this case 49
is placed after 19. 59 and 99 has been moved one step forward to
make room for 49.
19 49 59 99 39

5. Now four elements 19, 49, 59 and 99 has been sorted. Take the
element of fifth index 39 and compare it with the elements on the
left side. Placed it where the element smaller than it. In this case 39 Figure 3.1
is placed after 19. 49,59 and 99 has been moved one step forward
to make room for 39. The final sorted list. 3.1.5. Working of Enhanced Insertion Sort:
19 39 49 59 99
Pre-Processing Step:
First Step: In the First Step first index 77 and Last index 57 is
3.1.3. Enhanced Insertion Sort compared if last index is smaller than first index than swap
occurred.
77 35 45 17 87 27 67 57
There have been several authors who have continually made efforts
to increase the efficiency and productivity of the sorting process.
New Algorithm - Optimization and Improvements to Insertion Sort. Second Step: Now take second element 35 and compared with
Advanced algorithm works in two stages: second last element 67. Since 35 is smaller than 67, So no swap
At the first stage, which is the preliminary processing stage, the first occurred.
and last elements of the given data set are compared. If the first 57 35 45 17 87 27 67 77
element of this array is larger than the last element, then we change
these two elements. The position of the element from the front end Third Step: Now take third Element 45 and compared with fourth
of the array and the element from the rear end of the array is stored Last Element 27. Now 27 is smaller than 45, 27 is being replaced
with 45.
57 35 45 17 87 27 67 77 y = C[k] co2 n-1
m=k–1 co3 n-1
Fourth Step: Fourth Element 17 is Compared with Fourth Last while m >= 0 and C[m] > y co4 ΣΣm to n = pow (2, tm)
Element 87. Since 17 is smaller than 87, So no swap occurred. C[m+1] = C[m] co5 ΣΣm to n = pow(2, tm-1)
57 35 27 17 87 45 67 77 m=m-1 co6 ΣΣm to n = pow(2, tm-1)
Fifth Step: After Pre-Processing Step Apply Insertion Sort end while
algorithm on given array. C[m+1] = y co7 n-1
57 35 27 17 87 45 67 77 T(n) = Co1 * n + (Co2 + Co3) * (n - 1) + Co4 * (n - 1) + (Co5 +
Co6) * (n - 2) + Co7 * (n - 1)
Insertion Sort Step: T(n)= O(n)
First Step: 57 is Compared with 35. If First element is greater than When the array is already sorted, the top loop is executed n times,
its adjacent than swap the values. In this 37 is placed at first index, while the inner loop is not executed. So, there are n times
57 is moved one step forward to make room for 57. comparison. So, the complexity is linear [8].
57 35 27 17 87 45 67 77
Average Case:
Second Step: 27 is placed on its correct position. 35 and 57 is placed The average complexity of the insertion sort can be calculated by
one step forward to make room for element 27. taking the average of the best case and the complexity of the worst
35 57 27 17 87 45 67 77 case of any algorithm. Suppose that tm = (m-1) / 2 to calculate the
average case.
Cost Times
Third Step: 17 is placed on its correct position. 27, 35, 57 is placed
for k =1 to length(C) co1 n
one step forward to make room for 17.
y = C[k] co2 n-1
17 27 35 57 87 45 67 77
m=k–1 co3 n-1
while m >= 0 and C[m] > y co4 Σ Σm to n = pow(2, tm)
Fourth Step: Now 87 is compared with 57, 35, 27, 17, Hence there
C[m+1] = C[m] co5 ΣΣm to n = pow(2, tm-1)
will be no swap because 88 is larger than all values on its left.
m=m-1 co6 ΣΣm to n = pow(2, tm-1)
17 27 35 57 87 45 67 77
end while
C[m+1] = y co7 n-1
Fifth Step: 45 is placed on its correct position, element 57 is placed T(n) = Co1 * n + (Co2 + Co3) * (n - 1) + Co4/2 * (n - 1) (n) / 2 +
one step forward to make room for 45. (Co5 + Co6)/2 * ((n - 1) (n) / 2 - 1) + Co7 * (n - 1)
17 27 35 45 57 87 67 77 T(n)= O(n^2)
It occurs when the elements of an array are in neither ascending
Sixth Step: In this step or iteration, there is no swapped because 87 order nor descending order.
is larger than all values on the left side.
17 27 35 45 57 87 67 77 Worst Case:
Suppose we have an array in ascending order, and we want to sort
Seventh Step: In this Step 87 is moved one step forward to make our array in descending order. In this case, the complexity of the
room for 67. worst case arises. We can calculate the complexity of the worst case
17 27 35 45 57 67 87 77 by assuming that the element is in the last position, or we assume
that the element is not even present in the array. The worst case
Eighth Step: 87 is moved right side to make room for 77. The array occurred when tm = m
now has been sorted. Cost Times
17 27 35 45 57 67 77 87 for k =1 to length(C) co1 n
y = C[k] co2 n-1
m=k–1 co3 n-1
3.1.6. Time Complexity of Insertion Sort while m >= 0 and C[m] > y co4 ΣΣm to n = pow(2, tm)
C[m+1] = C[m] co5 ΣΣm to n = pow(2, tm-1)
m=m-1 co6 ΣΣm to n = pow(2, tm-1)
Best Case:
end while
The best time complexity of insertion sorting can be calculated if
C[m+1] = y co7 n-1
the array is already sorted or the required element is in the first
T(n) = Co1 * n + (Co2 + Co3) * (n - 1) + Co4/2 * (n - 1) (n) / 2 +
position. The calculation of the time complexity insertion gives us
(Co5 + Co6)/2 * ((n - 1) (n) / 2 - 1) + Co7 * (n - 1)
a lower bound on time. The best case occurred when tm = 1 means
T(n)= O(n^2)
that the array is already sorted in ascending order.
Each element is compared with every other element in the array, so
Cost Times
a comparison is performed for each element (n-1) times. Thus, the
for k =1 to length(C) co1 n
total number of comparisons = n (n-1) = O (n ^ 2).
3.1.7. Space Complexity 3.1.10. Pros and Cons of Insertion Sort:

Space complexity is measured as the amount of working memory Pros:


of the algorithm needed to sort a given array or data set. The 1. The sorting shows good results when working with a small list.
complexity of the space shows how much memory is required in 2. The insertion sorting algorithm is an in-place sorting algorithm,
the worst case at any point in the algorithm. Speaking about the so the space requirements are much less.
complexity of time, we are most worried about how space should 3. The insertion sorting algorithm is relatively simple and easy to
grow. The term we use means Big Oh, as the size n of the input task implement.
grows. 4. It's simple, small and easy to write.
In Insertion-Sort Everything is done in place (means that the 5. It does not use a lot of overhead
secondary data structure is not used, this algorithm only performs 6. Stable.
permutations in the given input array), therefore the complexity of 7. If the data is almost sorted, they can very quickly approach O (N)
space in Insertion Sort is O (1). If you calculate the total space, and faster than merge sort
suppose that the input size of the array and the additional storage 8. Insertion sorting algorithm can give us advantage of the
are used by the algorithm together, then this is O (n) O (n). But if processor’s cache of our hardware, and that one advantage can
we take a problem such as a search problem, adding input from an improve the performance more that you might think.
array doesn’t really add much interesting, especially when we talk 9. Only a constant amount of O (1) extra memory space is required.
about sorting algorithms that are not based on comparison. 10. A stable sorting algorithm, since it does not change the relative
order of elements with equal keys
3.1.8. Comparison of Insertion Sort and Enhanced Cons:
Insertion Sort 1. Insertion-Sorting is only useful when sorting a list of some items.
2. Insertion sort repeatedly scans the list of elements each time,
inserting elements in an unordered sequence into the correct
Criteria/Complexity Insertion Sort Enhanced
position.
Insertion Sort
3. Insertion-Sorting requires a large number of elements shifts.

3.2. Selection Sort :


The idea behind the selection sorting algorithm is to find the
Best-Case O(n) O(n)
minimum or maximum element in an unsorted array and then put it
Complexity in the correct position in the sorted array. Suppose we have an array
A [] = [77, 57, 47, 22], which must be sorted in ascending order.
The smallest element in the array, like “22,” is looked up and then
replaced with the element that is currently in first position 77. As
Average-Case O(n^2) Less than this process continues, the smallest element in the array that is in
Complexity O(n^1.59) the remaining unsorted array is searched for and placed in the
second position in this array, and so on. The selection sorting
algorithm is one of the simplest and most useful sorting algorithms
for processing a small amount of data. The selection sorting
Worst-Case O(n^2) Less than algorithm performs many comparisons, but the amount of data it
Complexity O(n^1.59) moves is very small. Thus, if you have small keys, but a large data
area, then the selection sorting algorithm can act faster to give us
results [9].
The main idea of the selection sorting algorithm is given below:
Table 3.1 Insertion Sort vs Enhanced Selection Sort 1. Find the smallest item in the data list.
2. Place this item in the first position of the list.
3. Find the next smallest item in the list
3.1.9. Insertion Sort Applications:
4. Place in the second position of the list and continue until all data
items are sorted.
Insertion sort is used when:
The selection sorting algorithm is a simple comparison algorithm.
1. An array has a small number of elements.
The selection sorting algorithm begins to get the smallest element
2. Only a few items to sort are left in the array.
from the data set in the array. Then it changes this smallest element
3. The array has a small number of elements; you can say that less
to the first element from the given array. After this first step of the
than 25 elements
selection sorting algorithm, this algorithm tries to select the
4. If the cost of comparisons exceeds the cost of swaps, then
smallest element in the unsorted part of the array at each iteration
Insertion sorting may have better performance.
of the sorting. It swaps this selected smallest element with another
element from this array. This process continues until there are no Fourth Step: Now find the minimum element and compared with
unsorted elements in the array. The selection-sorting-algorithm fourth element. 10,16 and 20 already sorted, 26 is compared with
occupies most of his time to get the smallest element in the unsorted 33 and swapped.
part of the list to sort it [10]. 10 16 20 26 37 33 45 47
Pseudo Code of Selection Sort:
Selection_Sort (C) Fifth Step: Now find the minimum element and compared with fifth
for k ← 1 to tn-1 element. 10,16,20 and 26 already sorted, 33 is compared with 37
small ← k and swapped.
for m ← k + 1 to tn 10 16 20 26 33 37 45 47
if C [ m ] < C [ small ]
small ← m Sixth Element: Now List has been sorted completely. The Final
Swap C[ k ] ↔ C[ small ] Sorted list.
10 16 20 26 33 37 45 47
3.2.1. C++ Implementation of Selection Sort

Void Selection_Sort (int C [ ], int tn) 3.2.3. Enhanced Selection Sort:


{
int min; An Enhanced selection sorting algorithm inserts an array of
for (int k = 0; k < tn-1; k++) elements and sorts these elements in the same array, finding the
{ maximum element and swapping it with the last element of this
min = k; array, and then reducing the size of the array by one for the next
for (int m = k+1; m < tn; m++) iteration. As we know, an improved selection sorting algorithm is
{ an improvement and improvement of the selection sorting
If (C [m] < C[min]) algorithm while reducing the number of swap operations, which
{ makes the algorithm data dependent and makes it more stable[11].
min = m; The Enhanced selection-sorting (ESS) algorithm procedure can be
} described as follows:
} 1. Insert all elements of the array.
swap (C [ min], C [ k ]) ; 2. Call the ESS function with the transfer of the array and its size.
} 3. Find the maximum element in the array and replace it with the
} last index.
4. Reducing the size of the array by one.
3.2.2. Working of Selection Sort: 5. Calling the "Enhanced Selection-Sort" function recursively. The
size of the array decreases by one after each call to the Enhanced
The following array is given to be Sort: Selection Sort function[12].
16 33 26 10 37 20 45 47
3.2.4. Pseudo Code of Enhancement Selection Sort:
First Step: For the first position in the sorted list, the whole list is
scanned sequentially. The first position where 16 is stored Enhanced_Selection_Sort (arr, size_of_array)
presently. Now Find Minimum element in the array and swapped 1. if size_of_array > 1 then
with first element. So, 10 and 16 are swapped. 2. var index, tem, maximum
10 33 26 16 37 20 45 47 3. index = size_of_array-1
4. maximum = arr(index)
Second Step: Now find the minimum element and compared with 5. for c= 0 to size_of_array-2 do
second element. So, 33 and 16 are swapped. 6. if arr(c) ≥ maximum then
10 16 26 33 37 20 45 47 7. maximum = arr(c)
8. index = c
Third Step: Now find the minimum element and compared with 9. end if
third element. 10 and 16 already sorted, 20 is compared with 26 and 10. end for
swapped. 11. if index! = size_of_array-1 then
10 16 20 33 37 26 45 47 12. tem = arr(size_of_array-1)
13. arr(size_of_array-1) = maximum
14. arr(index) = tem
15. end if
16. size_of_array = size_of_array-1 3.2.6. Time Complexity of Selection Sorting:
17. return Enhanced_Selection_Sort (arr, size_of_array)
18. else The time complexity of the Selection-Sort is the same in best case,
19. return arr average case and worst case. At each step, you have to find the
20. end if minimum element and put it in its right location. The same process
followed until the end of the array is not reached.
Pseudocode can be described as: Total Number of comparisons = (n-1) + (n-2) + (n-3) + (n-4) + (n-
1. Read all the elements in the array. 5) +…… +1= n(n-1)/2= n^2
2. Set k to the starting index Also, we can analyze the complexity by observing the number of
3. Traversal of the array in both directions: one pointer from the loops in the algorithm. There are two loops in the algorithm, so the
index k in incremental order, and the second pointer will point to complexity of this algorithm is n*n=n^2
the last element of the array and move in the opposite direction. Complexity = O(n^2)
4. The algorithm finds the smallest element in the array passing
simultaneously on both sides and replacing it with c [k] of the same Best Case:
array. When the array is in ascending order is given, and we want to sort
5. Increase k by 1. the array in ascending order, the best-case time complexity
6. Repeat step 3 until k becomes equal to the total number of occurred.
elements.
Cost Times
3.2.5. Working of Enhanced Selection Sort: 1. for (k=1 to tn-1) c01 n
2. int minimum=k; c02 n-1
The Enhanced selection sorting algorithm is easy to analyze 3. for (m=j+1 to tn) c03 (n²-n) / 2 + n
compared to other sorting algorithms because the loop is 4. if(c[m] < c[minimum]) c04 (n²-n) / 2
independent of the data in the array. Selecting the highest element 5. minimum=m; c05 (n²-n) / 2
from the array requires scanning all n elements, which requires n - 6. if (minimum! = k) c06 (n²-n) / 2
1 comparisons, since we assume that the first element is sorted, and 7. swap(c[k], c[minimum]); c07 n-1
then change it to the last position. The number of permutations in T(n)= (C01 *n) + (C02 *(n-1)) + (C03 * ((n²-n)/2) + n) + (C04*
the ESS algorithm can be expressed as follows: (n²-n) / 2) + (C05 * (n²-n) / 2) + (C06 * (n²-n) / 2) + (C07 * n-1)
1. In the best case, if the input array is already sorted in ascending T(n)=O(n^2)
order, there is no need to exchange, since each element is in the
right place. Worst Case:
2. In the scenario of the middle case, if the data set from the input If the array is in descending order and we want to sort the array in
array is sorted in reverse order or the data is given in descending ascending order, the worst-case time complexity occurred.
order, then the total number of exchange operations will be equal
to (n / 2). Since the exchange of maximum values in this array with Cost Times
the last elements of this array means that the maximum and 1. for (k=1 to tn-1) c01 n
minimum values of this data set are in the correct places in the 2. int minimum=k; c02 n-1
array. For example, if we have an array which is in descending 3. for (m=j+1 to tn) c03 (n²-n) / 2 + n
order 4. if(c[m] < c[minimum]) c04 (n²-n) / 2
57 47 37 27 17 5. minimum=m; c05 (n²-n) / 2
6. if (minimum! = k) c06 (n²-n) / 2
Swap the First Element with Last Element in the array. 7. swap(c[k], c[minimum]); c07 n-1
17 47 37 27 57 T(n)= (C01 *n) + (C02 *(n-1)) + (C03 * ((n²-n)/2) + n) + (C04*
(n²-n) / 2) + (C05 * (n²-n) / 2) + (C06 * (n²-n) / 2) + (C07 * n-1)
T(n)=O(n^2)
In the next comparison, the second element now is the maximum
value and it will be swapped with the fourth element(size-1).
Average Case:
17 27 37 47 57
Its occurred when the array is in neither in ascending order nor
descending order.
Now Array has been sorted.
17 27 37 47 57
Cost Times
1. for (k=1 to tn-1) c01 n
3. In the worst case, if the input array is neither in ascending nor
2. int minimum=k; c02 n-1
descending order, then n number of operations/Swaps are
3. for (m=j+1 to tn) c03 (n²-n) / 2 + n
necessary.
4. if(c[k] < c[minimum]) c04 (n²-n) /2
5. minimum=m; c05 (n²-n) / 2 1. Selection-Sort always performs O (n) temporary swaps, and the
6. if (minimum! = k) c06 (n²-n) / 2 Enhanced-Selection-Sort algorithm performs a swap operation
7. swap(c[k], c[minimum]); c07 n-1 depending on the input array (0, n / 2 and n).
T(n)= (C01 *n) + (C02 *(n-1)) + (C03 * ((n²-n)/2) + n) + (C04* 2. If the input array is already sorted, Enhanced-Selection-Sort does
(n²-n) / 2) + (C05 * (n²-n) / 2) + (C06 * (n²-n) / 2) + (C07 * n-1) not swap, but Selection-Sort performs n permutation operations n
T(n)=O(n^2) times. Writing to memory is much more expensive in terms of time
than reading.
3.2.7. Space Complexity:
3.2.9. Selection Sort Applications:
Space complexity is measured as the amount of working memory
needed by the algorithm to sort a given array or data set. The Selection sort is used when:
complexity of the space shows how much memory is required in 1. A small list should be sorted
the worst case at any point in the algorithm. Speaking about the 2. The exchange cost does not matter.
complexity of time, we are most worried about how space should 3. If checking all the elements in the array is required, it is better to
grow. The term we use means large O when the size n of the input use selection sorting.
task grows. Selection-Sort is an in-place algorithm. It does all the 4. When memory is limited, sorting is the best choice.
calculations in the original array, and no extra array is required. 5. Checking if everything is already sorted, it is best to use
Consequently, the complexity of the space works O (1) times, Selection-Sort.
because it requires only 1 additional temporary variable temp.
3.2.10. Pros and Cons of Selection Sort:
3.2.8. Comparison of Selection Sort and Enhanced
Selection Sort Pros:
1. The main advantage of sorting a selection is that it works well
Criteria/Complexity Selection Sort Enhanced with a small number of lists.
Selection Sort 2. This is an in-place sorting algorithm, so it does not require
additional temporary storage.
3. Its performance is easily influenced by the order of things before
Best-Case Big Oh(n^2) Big Oh(n^2) sorting.
=O(n^2) =O(n^2) 4. Productivity increase by 60% compared to the bubble sorting
algorithm.
5. Selection-Sorting is simple and easy to implement.
Average-Case Big Oh(n^2) Big Oh(n^2)
Cons:
=O(n^2) =O(n^2)
1. The selection sorting algorithm has low efficiency when working
with a large list of items.
2. The choice of sorting requires a degree n ^ 2 of the number of
Worst-Case Big Oh(n^2) Big Oh(n^2) steps for sorting n elements.
=O(n^2) =O(n^2) 3. Quicksort is more efficient than selection sorting.
4. Does not work faster on a partially sorted array.
5. Always perform on O (n ^ 2) even at its best.
Stable or Not Yes Yes
4. Methodology :
This study is conducted to examine the comparison and time
complexity of various sorting algorithms. Specifically, this study
Number of Swaps Always total Depends on attempts to find the working and time complexity of Insertion-Sort,
Performed number of n Data: 0, n/2 or n Selection-Sort and their improvements. A study is a descriptive
study of a research nature; this study will use secondary studies and
secondary data. A simple, descriptive statistical technique is used
to analyze the data. This study was conducted on various randomly
Table 3.2 Comparison of Time Complexity
selected data. A secondary research method is used to collect data.
The stratified random sampling technique is used to collect data
The Some Benefits of Enhanced-Selection-Sort as compared to
because this data collection technique is good because it will reduce
Selection -Sort algorithm are:
sampling error. This study collected quantitative data from various
scientific articles, review articles, journals, and various articles.
5.1.2. Comparative Runtime Analysis of Selection and
Enhanced Selection Sort, Insertion and
Enhanced Insertion Sort:

N Calculated Calculated Calculated Calculated


Time of Time of Time of Time of
Selection Enhanced Insertion Enhanced
Sort (Sec) Selection Sort (Sec) Insertion
Sort (Sec) Sort (Sec)
1000 1504495 631254 1503496 921345

1500 33381745 1415629 3380246 215243

2000 6008995 2512504 6006996 4503421

2500 93864245 3921879 9383746 7545122

3000 13513495 5643754 13510496 10123452

3500 18390745 7678129 18387246 13689762


Figure 4.1 Methodology

4000 24917995 10025004 24013996 19623143


5. Analysis :
5.1.1. Comparison of Space Complexity of Various 4500 30395245 12684379 30390746 25712521
Sorting Algorithms:
5000 37522495 15656254 37517496 29012332
Sorting-Algorithms Worst Case Space
Complexity 5500 45399745 18940629 45394246 41562631
Insertion-Sort O (1)

Table 5.2 Run Time Analysis


Selection-Sort O (1)
6. Conclusion :
Enhanced-Insertion-Sort O (1) In this study, we discussed the operation of Insertion-Sort and
Selection-Sort and their enhancements, their pseudo-codes, C ++
Enhanced-Selection-Sort O (1) implementation, runtime, and complexity of various sorting
algorithms. We discussed the work of Enhanced-Selection-Sort and
Enhanced-Insertion-Sort, Comparing these algorithms in terms of
Heap-Sort O (1)
time and space complexity with other sorting algorithms. To
compare Selection-Sort with Insertion-Sort, these algorithms have
Bubble-Sort O (1) different pros and cons, which is discussed in this article.
Therefore, it cannot be said that one of them is better than the other
Merge-Sort O (n) completely. The insertion-sorting algorithm for fully sorted arrays
works well compared to selection sorting in terms of runtime.
Quick-Sort O (n) Whereas Selection sorting for unsorted arrays works better than
Insertion sorting in terms of runtime. We discussed applications,
the pros and cons, the operation of the algorithm, runtime analysis,
Table 5.1 Comparison of Space Complexity
implementation of pseudocode and C ++, the complexity of
insertion-sort, selection-sort, enhanced-insertion-sort, and
enhanced-selection-sort.
7. References :
[1] A. Dev Mishra and D. Garg, “SELECTION O. BEST
SORTING ALGORITHM.”
[2] J. B. Hayfron-Acquah, O. Appiah, and K. Riverson,
“Improved Selection Sort Algorithm,” 2016.
[3] S. Kumar and P. Singla, “Sorting using a combination of
Bubble Sort, Selection Sort &amp; Counting Sort,”
International Journal of Mathematical Sciences and
Computing, vol. 5, no. 2, pp. 30–43, Apr. 2019, doi:
10.5815/ijmsc.2019.02.03.
[4] P. Mishra, K. Nishant, M. #2, and S. #3, “A New Approach
to Improve Selection Sort by the Modified Selection Sort
(MSSA) and Performance Comparison,” 2018.
[5] M. K. Pooja Chhatwani, “International Journal of
Computer Science and Mobile Computing Insertion Sort
with its Enhancement,” 2017.
[6] K. Ali, “A Comparative Study of Well Known Sorting
Algorithms,” International Journal of Advanced Research in
Computer Science, vol. 8, no. 1.
[7] “10.1.1.654.4716.”
[8] A. S. Mohammed, Ş. E. Amrahov, and F. v. Çelebi,
“Bidirectional Conditional Insertion Sort algorithm; An
efficient progress on the classical insertion sort,” Aug.
2016, doi: 10.1016/j.future.2017.01.034.
[9] M. Zhou and H. Wang, “An efficient selection sorting
algorithm for two-dimensional arrays,” in Proceedings -
4th International Conference on Genetic and
Evolutionary Computing, ICGEC 2010, 2010, pp. 853–
855, doi: 10.1109/ICGEC.2010.216.
[10] F. Gemci Furat, “A Comparative Study of Selection Sort
and Insertion Sort Algorithms,” International Research
Journal of Engineering and Technology, 2016.
[11] V. Bothra and S. Bhalerao, “An Enhancement of Selection
Sorting.”
[12] J. Alnihoud and R. Mansi, “An Enhancement of Major
Sorting Algorithms,” 2018.

You might also like