Selection Sort – Selection sort is a simple and efficient sorting algorithm that
works by repeatedly selecting the smallest (or largest) element from the
unsorted portion of the list and moving it to the sorted portion of the list.
The algorithm repeatedly selects the smallest (or largest) element from the
unsorted portion of the list and swaps it with the first element of the unsorted
part. This process is repeated for the remaining unsorted portion until the entire
list is sorted.
How does Selection Sort Algorithm work?
Lets consider the following array as an example: arr[] = {64, 25, 12, 22, 11}
First pass:
For the first position in the sorted array, the whole array is traversed
from index 0 to 4 sequentially. The first position where 64 is stored
presently, after traversing whole array it is clear that 11 is the lowest
value.
Thus, replace 64 with 11. After one iteration 11, which happens to be
the least value in the array, tends to appear in the first position of the
sorted list.
Selection Sort Algorithm | Swapping 1st element with the minimum in array
Second Pass:
For the second position, where 25 is present, again traverse the rest of
the array in a sequential manner.
After traversing, we found that 12 is the second lowest value in the
array and it should appear at the second place in the array, thus swap
these values.
Selection Sort Algorithm | swapping i=1 with the next minimum element
Third Pass:
Now, for third place, where 25 is present again traverse the rest of the
array and find the third least value present in the array.
While traversing, 22 came out to be the third least value and it should
appear at the third place in the array, thus swap 22 with element
present at third position.
Selection Sort Algorithm | swapping i=2 with the next minimum element
Fourth pass:
Similarly, for fourth position traverse the rest of the array and find the
fourth least element in the array
As 25 is the 4th lowest value hence, it will place at the fourth position.
Selection Sort Algorithm | swapping i=3 with the next minimum element
Fifth Pass:
At last the largest value present in the array automatically get placed
at the last position in the array
The resulted array is the sorted array.
Selection Sort Algorithm | Required sorted array
Below is the implementation of the above approach:
// C program for implementation of selection sort
#include <stdio.h>
void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
void selectionSort(int arr[], int n)
{
int i, j, min_idx;
// One by one move boundary of unsorted subarray
for (i = 0; i < n-1; i++)
{
// Find the minimum element in unsorted array
min_idx = i;
for (j = i+1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;
// Swap the found minimum element with the first element
if(min_idx != i)
swap(&arr[min_idx], &arr[i]);
}
}
/* Function to print an array */
void printArray(int arr[], int size)
{
int i;
for (i=0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}
// Driver program to test above functions
int main()
{
int arr[] = {64, 25, 12, 22, 11};
int n = sizeof(arr)/sizeof(arr[0]);
selectionSort(arr, n);
printf("Sorted array: \n");
printArray(arr, n);
return 0;
}
Output
Sorted array:
11 12 22 25 64
Complexity Analysis of Selection Sort
Time Complexity: The time complexity of Selection Sort is O(N2) as there are
two nested loops:
One loop to select an element of Array one by one = O(N)
Another loop to compare that element with every other Array element
= O(N)
2
Therefore overall complexity = O(N) * O(N) = O(N*N) = O(N )
Auxiliary Space: O(1) as the only extra memory used is for temporary
variables while swapping two values in Array. The selection sort never makes
more than O(N) swaps and can be useful when memory writing is costly.
Advantages of Selection Sort Algorithm
Simple and easy to understand.
Works well with small datasets.
Disadvantages of the Selection Sort Algorithm
Selection sort has a time complexity of O(n^2) in the worst and
average case.
Does not work well on large datasets.
Does not preserve the relative order of items with equal keys which
means it is not stable.
Frequently Asked Questions on Selection Sort
Q1. Is Selection Sort Algorithm stable?
The default implementation of the Selection Sort Algorithm is not stable.
However, it can be made stable. Please see the stable Selection Sort for details.
Q2. Is Selection Sort Algorithm in-place?
Yes, Selection Sort Algorithm is an in-place algorithm, as it does not require
extra space
Bubble Sort is the simplest sorting algorithm that works by repeatedly
swapping the adjacent elements if they are in the wrong order. This algorithm is
not suitable for large data sets as its average and worst-case time complexity is
quite high.
Bubble Sort Algorithm
In this algorithm,
traverse from left and compare adjacent elements and the higher one is
placed at right side.
In this way, the largest element is moved to the rightmost end at first.
This process is then continued to find the second largest and place it
and so on until the data is sorted.
How does Bubble Sort Work?
Let us understand the working of bubble sort with the help of the following
illustration:
Input: arr[] = {6, 3, 0, 5}
First Pass:
The largest element is placed in its correct position, i.e., the end of the array.
Bubble Sort Algorithm : Placing the largest element at correct position
Second Pass:
Place the second largest element at correct position
Bubble Sort Algorithm : Placing the second largest element at correct position
Third Pass:
Place the remaining two elements at their correct positions.
Bubble Sort Algorithm : Placing the remaining elements at their correct
positions
Implementation of Bubble Sort
Below is the implementation of the bubble sort. It can be optimized by stopping
the algorithm if the inner loop didn’t cause any swap.
C
// Optimized implementation of Bubble sort
#include <stdbool.h>
#include <stdio.h>
void swap(int* xp, int* yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
// An optimized version of Bubble Sort
void bubbleSort(int arr[], int n)
{
int i, j;
bool swapped;
for (i = 0; i < n - 1; i++) {
swapped = false;
for (j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
swap(&arr[j], &arr[j + 1]);
swapped = true;
}
}
// If no two elements were swapped by inner loop,
// then break
if (swapped == false)
break;
}
}
// Function to print an array
void printArray(int arr[], int size)
{
int i;
for (i = 0; i < size; i++)
printf("%d ", arr[i]);
}
// Driver program to test above functions
int main()
{
int arr[] = { 64, 34, 25, 12, 22, 11, 90 };
int n = sizeof(arr) / sizeof(arr[0]);
bubbleSort(arr, n);
printf("Sorted array: \n");
printArray(arr, n);
return 0;
}
Sorted array:
11 12 22 25 34 64 90
Complexity Analysis of Bubble Sort :
TimeComplexity: O(N2)
Auxiliary Space: O(1)
Advantages of Bubble Sort:
Bubble sort is easy to understand and implement.
It does not require any additional memory space.
It is a stable sorting algorithm, meaning that elements with the same
key value maintain their relative order in the sorted output.
Disadvantages of Bubble Sort:
2
Bubble sort has a time complexity of O(N ) which makes it very slow
for large data sets.
Bubble sort is a comparison-based sorting algorithm, which means
that it requires a comparison operator to determine the relative order
of elements in the input data set. It can limit the efficiency of the
algorithm in certain cases.
Some FAQs related to Bubble Sort:
What is the Boundary Case for Bubble sort?
Bubble sort takes minimum time (Order of n) when elements are already
sorted. Hence it is best to check if the array is already sorted or not
beforehand, to avoid O(N 2) time complexity.
Does sorting happen in place in Bubble sort?
Yes, Bubble sort performs the swapping of adjacent pairs without the use of
any major data structure. Hence Bubble sort algorithm is an in-place
algorithm.
Is the Bubble sort algorithm stable?
Yes, the bubble sort algorithm is stable.
Where is the Bubble sort algorithm used?
Due to its simplicity, bubble sort is often used to introduce the concept of a
sorting algorithm. In computer graphics, it is popular for its capability to
detect a tiny error (like a swap of just two elements) in almost-sorted arrays
and fix it with just linear complexity (2n).
Example: It is used in a polygon filling algorithm, where bounding lines are
sorted by their x coordinate at a specific scan line (a line parallel to the x-axis),
and with incrementing y their order changes (two elements are swapped) only
at intersections of two
Insertion sort is a simple sorting algorithm that works similar to the way you
sort playing cards in your hands. The array is virtually split into a sorted and
an unsorted part. Values from the unsorted part are picked and placed at the
correct position in the sorted part.
Insertion Sort Algorithm
To sort an array of size N in ascending order iterate over the array and
compare the current element (key) to its predecessor, if the key element is
smaller than its predecessor, compare it to the elements before. Move the
greater elements one position up to make space for the swapped element.
Working of Insertion Sort algorithm
Consider an example:
arr[]: {12, 11, 13, 5, 6}
12 11 13 5 6
First Pass:
Initially, the first two elements of the array are compared in insertion
sort.
12 11 13 5 6
Here, 12 is greater than 11 hence they are not in the ascending order
and 12 is not at its correct position. Thus, swap 11 and 12.
So, for now 11 is stored in a sorted sub-array.
11 12 13 5 6
Second Pass:
Now, move to the next two elements and compare them
11 12 13 5 6
Here, 13 is greater than 12, thus both elements seems to be in
ascending order, hence, no swapping will occur. 12 also stored in a
sorted sub-array along with 11
Third Pass:
Now, two elements are present in the sorted sub-array which
are 11 and 12
Moving forward to the next two elements which are 13 and 5
11 12 13 5 6
Both 5 and 13 are not present at their correct place so swap them
11 12 5 13 6
After swapping, elements 12 and 5 are not sorted, thus swap again
11 5 12 13 6
Here, again 11 and 5 are not sorted, hence swap again
5 11 12 13 6
Here, 5 is at its correct position
Fourth Pass:
Now, the elements which are present in the sorted sub-array are 5,
11 and 12
Moving to the next two elements 13 and 6
5 11 12 13 6
Clearly, they are not sorted, thus perform swap between both
5 11 12 6 13
Now, 6 is smaller than 12, hence, swap again
5 11 6 12 13
Here, also swapping makes 11 and 6 unsorted hence, swap again
5 6 11 13
12
Finally, the array is completely sorted.
Illustrations:
// C program for insertion sort
#include <math.h>
#include <stdio.h>
/* Function to sort an array using insertion sort*/
void insertionSort(int arr[], int n)
{
int i, key, j;
for (i = 1; i < n; i++) {
key = arr[i];
j = i - 1;
/* Move elements of arr[0..i-1], that are
greater than key, to one position ahead
of their current position */
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
// A utility function to print an array of size n
void printArray(int arr[], int n)
{
int i;
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}
/* Driver program to test insertion sort */
int main()
{
int arr[] = { 12, 11, 13, 5, 6 };
int n = sizeof(arr) / sizeof(arr[0]);
insertionSort(arr, n);
printArray(arr, n);
return 0;
}
Output
5 6 11 12 13
TimeComplexity: O(N^2)
Auxiliary Space: O(1)
Complexity Analysis of Insertion Sort:
Time Complexity of Insertion Sort
The worst-case time complexity of the Insertion sort is O(N^2)
The average case time complexity of the Insertion sort is O(N^2)
The time complexity of the best case is O(N).
Space Complexity of Insertion Sort
The auxiliary space complexity of Insertion Sort is O(1)
Characteristics of Insertion Sort
This algorithm is one of the simplest algorithms with a simple
implementation
Basically, Insertion sort is efficient for small data values
Insertion sort is adaptive in nature, i.e. it is appropriate for data sets
that are already partially sorted.
Frequently Asked Questions on Insertion Sort
Q1. What are the Boundary Cases of the Insertion Sort algorithm?
Insertion sort takes the maximum time to sort if elements are sorted in reverse
order. And it takes minimum time (Order of n) when elements are already
sorted.
Q2. What is the Algorithmic Paradigm of the Insertion Sort algorithm?
The Insertion Sort algorithm follows an incremental approach.
Q3. Is Insertion Sort an in-place sorting algorithm?
Yes, insertion sort is an in-place sorting algorithm.
Q4. Is Insertion Sort a stable algorithm?
Yes, insertion sort is a stable sorting algorithm.
Q5. When is the Insertion Sort algorithm used?
Insertion sort is used when number of elements is small. It can also be useful
when the input array is almost sorted, and only a few elements are misplaced
in a complete big array.