selection_sort
Selection Sort Algorithm
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 and swaps it with the first element of the unsorted portion. This process is repeated for the remaining unsorted portion of the list until the entire list is sorted.
The algorithm maintains two subarrays in a given array.
- The already sorted subarray.
- The remaining unsorted subarray.
In every iteration of the selection sort, the minimum (or maximum) element from the unsorted subarray is picked and swaped with the begining element of the unsorted subarray.
After each iteration the sorted subarray size increases by one and the unsorted subarray size decrease by one. After N (size of array) iteration the array gets sorted.
Flowchart of the Selection Sort:
How selection sort works?
Implementation of the Selection Sort (C++)
[My github repository] (https://github.com/ciaociaocu/Play_with_Algorithm/tree/main/00_Sort_Selection)
Reference
- [1] geeksforgeeks
- [2] liuyubobobo