Article Directory
In this tutorial, you will learn how selection sorting works. In addition, you will find examples of using C for selection sort.
Selective sorting is an algorithm that selects the smallest element from the unsorted list in each iteration and places the element at the beginning of the unsorted list.
How does selection sorting work?
- Set the first element to minimum.
- Compare the minimum with the second element. If the second element is less than the minimum, the second element is designated as the minimum.
Compare the minimum and the third element. Similarly, if the third element is smaller, specify the third element as the minimum, otherwise do nothing. This process continues until the last element.
- After each iteration, minimum is placed at the front of the unsorted list.
- For each iteration, the index starts from the first unsorted element. Repeat steps 1 to 3 until all elements are placed in the correct positions.
Pseudo code for selecting sorting algorithm
selectionSort(array, size)
repeat (size - 1) times
set the first unsorted element as the minimum
for each of the unsorted elements
if element < currentMinimum
set element as new minimum
swap minimum with first unsorted position
end selectionSort
C example
// Selection sort in C
#include <stdio.h>
// function to swap the the position of two elements
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
void selectionSort(int array[], int size) {
for (int step = 0; step < size - 1; step++) {
int min_idx = step;
for (int i = step + 1; i < size; i++) {
// To sort in descending order, change > to < in this line.
// Select the minimum element in each loop.
if (array[i] < array[min_idx])
min_idx = i;
}
// put min at the correct position
swap(&array[min_idx], &array[step]);
}
}
// function to print an array
void printArray(int array[], int size) {
for (int i = 0; i < size; ++i) {
printf("%d ", array[i]);
}
printf("\n");
}
// driver code
int main() {
int data[] = {
20, 12, 10, 15, 2};
int size = sizeof(data) / sizeof(data[0]);
selectionSort(data, size);
printf("Sorted array in Acsending Order:\n");
printArray(data, size);
}
the complexity
Cycles | Number of comparisons |
---|---|
the first time | (n-1) |
the second time | (n-2) |
the third time | (n-3) |
… | … |
the last time | 1 |
Number of comparisons: (n-1) + (n-2) + (n-3) +… + 1 = n(n-1) / 2 approximately equal to n 2 n^2n2 .
Complexity = O(n 2 n^2n2 )
In addition, we can analyze the complexity by simply observing the number of cycles. There are 2 loops, so the complexity is n*n=n 2 n^2n2 .
time complexity:
- Worst case complexity: O( n 2 n^2n2 )
If we want to sort in ascending order and the array is sorted in descending order, the worst case will occur. - Best case complexity: O( n 2 n^2n2 )
Appears when the array is sorted - Average case complexity: O( n 2 n^2n2 )
This happens when the elements of the array are out of order (neither ascending nor descending).
Reference documents
[1]Parewa Labs Pvt. Ltd.Selection Sort Algorithm[EB/OL].https://www.programiz.com/dsa/selection-sort,2020-01-01.