![]() |
VOOZH | about |
The selection sort is a simple comparison-based sorting algorithm that sorts a collection by repeatedly finding the minimum (or maximum) element and placing it in its correct position in the list. It is very simple to implement and is preferred when you have to manually implement the sorting algorithm for a small amount of dataset.
In this article, we will learn about the selection sort, its working and its implement in C language.
Selection sort work by virtually dividing the array into two parts: sorted and unsorted. Initially, whole list is considered unsorted. Starting from the first element in the unsorted part, a minimum element is identified from the unsorted part of the array and placed at the current position. The same is done for the rest of the elements in the unsorted part one by one gradually growing the sorted part till the whole array is sorted.
Let's take the example of the array {64, 25, 12, 22, 11} which we want to sort in ascending order and see what happens in each pass:
The resultant array is the sorted array.
Unsorted array: 64 25 12 22 11 Sorted array: 11 12 22 25 64
Time Complexity: O(N2), as there are two nested loops.
Auxiliary Space: O(1), as the only extra memory used is for temporary variable while swapping two values in Array.