Insertion Sort Algorithm
Insertion Sort Algorithm
Insertion sort works similar to the sorting of playing cards in hands. It is assumed that the
first card is already sorted in the card game, and then we select an unsorted card. If the
selected unsorted card is greater than the first card, it will be placed at the right side;
otherwise, it will be placed at the left side. Similarly, all unsorted cards are taken and put in
their exact place.
The same approach is applied in insertion sort. The idea behind the insertion sort is that first
take one element, iterate it through the sorted array. Although it is simple to use, it is not
appropriate for large data sets as the time complexity of insertion sort in the average case and
worst case is O(n2), where n is the number of items. Insertion sort is less efficient than the
other sorting algorithms like heap sort, quick sort, merge sort, etc.
o Simple implementation
o Efficient for small data sets
o Adaptive, i.e., it is appropriate for data sets that are already substantially sorted.
Algorithm
The simple steps of achieving the insertion sort are listed as follows -
Step 1 - If the element is the first element, assume that it is already sorted. Return 1.
Step3 - Now, compare the key with all elements in the sorted array.
Step 4 - If the element in the sorted array is smaller than the current element, then move to
the next element. Else, shift greater elements in the array towards the right.
Now, let's see the time complexity of insertion sort in best case, average case, and in worst
case. We will also see the space complexity of insertion sort.
1. Time Complexity
2. Space Complexity
is a simple sorting algorithm. This sorting algorithm, like insertion sort, is an in-place
comparison-based algorithm in which the list is divided into two parts, the sorted part at the
left end and the unsorted part at the right end. Initially, the sorted part is empty and the
unsorted part is the entire list.
The smallest element is selected from the unsorted array and swapped with the leftmost
element, and that element becomes a part of the sorted array. This process continues moving
unsorted array boundaries by one element to the right.
This algorithm is not suitable for large data sets as its average and worst case complexities
are of O(n2), where n is the number of items.
This type of sorting is called Selection Sort as it works by repeatedly sorting elements. That
is: we first find the smallest value in the array and exchange it with the element in the first
position, then find the second smallest element and exchange it with the element in the
second position, and we continue the process in this way until the entire array is sorted.
Selection sort spends most of its time trying to find the minimum element in the unsorted part
of the array. It clearly shows the similarity between Selection sort and Bubble sort.
Bubble sort selects the maximum remaining elements at each stage, but wastes some
effort imparting some order to an unsorted part of the array.
Selection sort is quadratic in both the worst and the average case, and requires no
extra memory.
For each i from 1 to n - 1, there is one exchange and n - i comparisons, so there is a total of n
- 1 exchanges and
In the worst case, this could be quadratic, but in the average case, this quantity is O(n log n).
It implies that the running time of Selection sort is quite insensitive to the input.
Example
For the first position in the sorted list, the whole list is scanned sequentially. The first position
where 14 is stored presently, we search the whole list and find that 10 is the lowest value.
So we replace 14 with 10. After one iteration 10, which happens to be the minimum value in
the list, appears in the first position of the sorted list.
For the second position, where 33 is residing, we start scanning the rest of the list in a linear
manner.
We find that 14 is the second lowest value in the list and it should appear at the second place.
We swap these values.
After two iterations, two least values are positioned at the beginning in a sorted manner.
The same process is applied to the rest of the items in the array −
Implementation
The selection sort algorithm is implemented in four different programming languages below.
The given program selects the minimum number of the array and swaps it with the element in
the first index. The second minimum number is swapped with the element present in the
second index. The process goes on until the end of the array is reached.
Output
The array after sorting in Ascending Order by selection sort is:
[0, 9, 11, 12, 45, 88, 97, 202, 747]
We assume list is an array of n elements. We further assume that swap function swaps the
values of the given array elements.
Step 1 − Check if the first element in the input array is greater than the next element in the
array.
Step 2 − If it is greater, swap the two elements; otherwise move the pointer forward in the
array.
Step 3 − Repeat Step 2 until we reach the end of the array.
Step 4 − Check if the elements are sorted; if not, repeat the same process (Step 1 to Step 3)
from the last element of the array to the first.
Step 5 − The final output achieved is the sorted array.
Pseudocode
We observe in algorithm that Bubble Sort compares each pair of array element unless the
whole array is completely sorted in an ascending order. This may cause a few complexity
issues like what if the array needs no more swapping as all the elements are already
ascending.
To ease-out the issue, we use one flag variable swapped which will help us see if any swap
has happened or not. If no swap has occurred, i.e. the array requires no more processing to be
sorted, it will come out of the loop.
In this algorithm, the number of comparison is irrespective of the data set, i.e. whether the
provided input elements are in sorted order or in reverse order or at random.
Memory Requirement
From the algorithm stated above, it is clear that bubble sort does not require extra memory.
Example
We take an unsorted array for our example. Bubble sort takes Ο(n2) time so we're keeping it
short and precise.
Bubble sort starts with very first two elements, comparing them to check which one is
greater.
In this case, value 33 is greater than 14, so it is already in sorted locations. Next, we compare
33 with 27.
We find that 27 is smaller than 33 and these two values must be swapped.
Next we compare 33 and 35. We find that both are in already sorted positions.
To be precise, we are now showing how an array should look like after each iteration. After
the second iteration, it should look like this −
Notice that after each iteration, at least one value moves at the end.
And when there's no swap required, bubble sort learns that an array is completely sorted.
Implementation
One more issue we did not address in our original algorithm and its improvised pseudocode,
is that, after every iteration the highest values settles down at the end of the array. Hence, the
next iteration need not include already sorted elements. For this purpose, in our
implementation, we restrict the inner loop to avoid already sorted values.
Output
Sorted array is:
11 12 22 25 34 64 90
Merge Sort
A problem is split into multiple sub-problems in merge sort. Each sub-problem is addressed
separately. The final result is formed by combining sub-problems. The MergeSort method
divides the array in half periodically until we reach a point when we try to MergeSort on a
subarray of size 1. The merge function then takes over, merging the sorted arrays into larger
arrays until the entire array is merged.
1. Consider an array
2. Find the middle point to divide the array into two halves
3. Call merge sort for the first half
4. Call merge sort for the second half
5. Merge both the half
6. The result will be in sorted format
Algorithm
As of now, we have a rough understanding of how merge sort is performed. For better
understanding let's dive deep into the algorithm followed by the code:
Merge_Sort(Left)
Merge_Sort(Right)
i=j=k=0
print(Left)
print(Right)
while i < len(Left) and j < len(Right):
if Left[i] < Right[j]:
array[k] = Left[i]
i =i+ 1
else:
array[k] = Right[j]
j =j+ 1
k += 1
# Driver program
if __name__ == '__main__':
array = [7, 2, 5, 6, 3, 1, 8, 4]
print("Orignal Array is: ", array)
Merge_Sort(array)
print("Sorted array is: ")
printarray(array)
Output:
Orignal Array is: [7, 2, 5, 6, 3, 1, 8, 4]
[7]
[2]
[5]
[6]
[2, 7]
[5, 6]
[3]
[1]
[8]
[4]
[1, 3]
[4, 8]
[2, 5, 6, 7]
[1, 3, 4, 8]
Sorted array is:
12345678