SORTING ALGORITHM
16 February 2024 17:21
TOPIC :
1. INSERTION SORT
2. BUBBLE SORT
3. SELECTION SORT
CREATED BY - SHIVAM BAREKAR
SORTING ALGORITHM
26 February 2024 21:10
What is Sorting ?
Sorting means an arrangement of elements in an ordered sequence either in
increasing(ascending) order or decreasing(descending) order. Sorting is very important and
many software and programs use this. The major difference is the amount of space and
time they consume while being performed in the program.
Different languages have their own in-built functions for sorting, which are basically
hybrid sorts that are a combination of different basic sorting algorithms. For example, C++
uses introsort, which runs quicksort and switches to heapsort when the recursion gets too
deep. This way, you get the fast performance of quicksort in practice while guaranteeing a
worst-case time complexity of O(N*logN), where N is the number of elements.
We will be focusing mainly upon the following sorting algorithms :
● Selection Sort
● Bubble Sort
● Insertion Sort
● Merge Sort
● Quick Sort
● Counting Sort
The sorting algorithms are further classified on the following basis :
● In-place sorting and out-place sorting
○ In-place sorting : In-place sorting does not require any extra space except the input
space excluding the constant space which is used for variables or iterators. It also doesn’t
include the space used for stack in the recursive algorithms. For Example, Bubble Sort,
Selection Sort, Insertion Sort, Quicksort, etc.
○ Out-place sorting : Out-place sorting requires extra space to sort the elements. For
Example, Merge Sort, Counting sort, etc.
● Stable and Unstable sorting
○ Stable sorting: A sorting algorithm when two objects with equal keys appear in the same
order in the sorted output as they appear in the unsorted input. For Example, Insertion
Sort, Merge Sort, Bubble Sort, Counting sort, etc.
○ Unstable sorting: A sorting algorithm is called unstable sorting if there are two or more
objects with equal keys which don’t appear in the same order before and after sorting. For
Example, Quick Sort, Heap Sort, Selection Sort, etc.
Above is an example of stable sorting here number 26 is appearing two times at positions 6
and 8 respectively in an unsorted array and their order of occurrence is preserved in the
sorted array as well and sorted array i.e. element 26 (blue) at position 6 in unsorted array
appears first in sorted array followed by another 26 (red) at position 8.
● Adaptive and Non-adaptive sorting
○ Adaptive sorting: When the order of occurrence of elements in an array affects the
time complexity of a sorting algorithm, then such an algorithm is known as an adaptive
sorting algorithm. For Example, Bubble sort, Insertion Sort, Quick Sort, etc.
○ Non-adaptive sorting: When the order of occurrence of elements in an array does not
affect the time complexity of a sorting algorithm, then such an algorithm is known as a
non-adaptive sorting algorithm. For Example, Selection Sort, Heap Sort, Merge Sort, etc.
INSERTION SORT
26 February 2024 21:12
Algorithm :
Insertion Sort works similar to how we sort a hand of playing cards.
Imagine that you are playing a card game. You're holding the cards in your hand, and these
cards are sorted. The dealer hands you exactly one new card. You have to put it into the
correct place so that the cards you're holding are still sorted. In selection sort, each
element that you add to the sorted subarray is no smaller than the elements already in the
sorted subarray.
But in our card example, the new card could be smaller than some of the cards you're
already holding, and so you go down the line, comparing the new card against each card in
your hand until you find the place to put it. You insert the new card in the right place, and
once again, your hand holds fully sorted cards. Then the dealer gives you another card, and
you repeat the same procedure. Then another card, and another card, and so on, until the
dealer stops giving you cards. This is the idea behind the insertion sort. Loop over
positions in the array, starting with index 1. Each new position is like the new card handed
to you by the dealer, and you need to insert it into the correct place in the sorted
subarray to the left of that position.
Insertion sort is a sorting algorithm that places an unsorted element at its suitable place
in each iteration.
Consider the following depicted array as an example. You want to sort this array in
increasing order.
Arr[] = [9,4,5,1,3]
Following is the pictorial diagram for a better explanation of how it works :
Pseudocode :
Code :
Time complexity :
O(N2), in the worst case. As for finding the correct position for the ith element, we
need i iterations from 0 to i-1th index, so the total number of comparisons = 1+2+3+ … + N-
1 = (N*(N-1))/2 and the total number of exchanges in the worst case: N-1.
So, the overall complexity becomes O(N2).
Space complexity :
O(1), as no extra space is required.
BUBBLE SORT
26 February 2024 21:13
Algorithm :
In selection sort, the elements from the start get placed at the correct position first and
then the further elements, but in the bubble sort, the elements start to place correctly
from the end.
In this technique, we just compare the two adjacent elements of the array and then sort
them manually by swapping if not sorted. Similarly, we will compare the next two elements
(one from the previous position and the corresponding next) of the array and sort them
manually. This way the elements from the last get placed in their correct position. This is
the difference between selection sort and bubble sort.
Consider the following depicted array as an example. You want to sort this array in
increasing order.
Arr[] = [50,25,5,20,10]
Following is the pictorial diagram for a better explanation of how it works :
Pseudocode :
Code :
Time complexity :
O(N2), in the worst case. For every element, we iterate over the entire array each time
giving an overall time complexity of O(N2).
Space complexity :
O(1), as no extra space is required.
SELECTION SORT
26 February 2024 21:13
Algorithm :
Steps : (sorting in increasing order)
● First-of-all, we will find the smallest element of the array and swap it with index
0.
● Similarly, we will find the second smallest and swap that with the element at index
1 and so on…
● Ultimately, we will be getting a sorted array in increasing order only.
Let us look at the example for better understanding :
Consider the following depicted array as an example. You want to sort this array in
increasing order.
Following is the pictorial diagram for a better explanation of how it works :
This is how we obtain the sorted array at last.
Pseudocode :
Code :
Time complexity :
O(N2), in the worst case. As to find the minimum element from the array of ‘N’ elements,
we require ‘N-1’ comparisons, then after putting the minimum element in the correct
position, we repeat the same for the unsorted array of the remaining ‘N-1’ elements,
performing ‘N-2’ comparisons and so on. So, total number of comparisons : N-1 + N-2 + N-3
+ … + 1 = (N*(N-1))/2 and total number of exchanges(swapping): N-1 So, time complexity
becomes O(N2).
Space complexity :
O(1), as no extra space is required.
PRACTICE PROBLEMS
26 February 2024 21:15
1.https://www.geeksforgeeks.org/problems/insertion-sort/1
2.https://www.geeksforgeeks.org/problems/bubble-sort/1
3.https://www.geeksforgeeks.org/problems/selection-sort/1