Selection sort
Selection sort is basically a selection of an element position from the start with the other rest of the elements. Elements are compared and exchanged depending on the condition and then selection position is shifted to the next position till it reaches to the end.
Here are the selection positions of these elements. Comparion takes place with selected position to the rest of the next positions.
Selection sort Big(o)
Selection sort of N elements can take (N - 1) steps and (N - 1) iterations in each steps. Thus resultant is (N-1)*(N-1). This sorting algorithm is not however the best in performance when count of the elements are large. Time complexities of Selection sort is Big(o) = N^2. This sorting is well suited for small number of elements and it is easy the implement in C or any other programming languages.
Selection sort example code
Here is a C source code of Selection sorting with 5 integer elements. This code sorts the elements in ascending order.
Steps and Iterations (diagrams)
Output
Unsorted elements [4, 9, 5, 1, 0] Selection sort started- ==== Selection 1 ==== [4, 9, 5, 1, 0] #1:2 no swap 4 <= 9 [4, 9, 5, 1, 0] #1:3 no swap 4 <= 5 [4, 9, 5, 1, 0] #1:4 swap 4 > 1 [1, 9, 5, 4, 0] #1:5 swap 1 > 0 [0, 9, 5, 4, 1] ==== Selection 2 ==== [0, 9, 5, 4, 1] #2:3 swap 9 > 5 [0, 5, 9, 4, 1] #2:4 swap 5 > 4 [0, 4, 9, 5, 1] #2:5 swap 4 > 1 [0, 1, 9, 5, 4] ==== Selection 3 ==== [0, 1, 9, 5, 4] #3:4 swap 9 > 5 [0, 1, 5, 9, 4] #3:5 swap 5 > 4 [0, 1, 4, 9, 5] ==== Selection 4 ==== [0, 1, 4, 9, 5] #4:5 swap 9 > 5 [0, 1, 4, 5, 9] Selection sort ended Selection sorted elements [0, 1, 4, 5, 9]
Further readings
- www.geeksforgeeks.org - selection-sort
- www.tutorialspoint.com - selection-sortt
- www.hackerearth.com - selection-sort
- wikipedia.org - selection-sort
- www.interviewbit.com - selection-sort
About our authors: Team EQA
You have viewed 1 page out of 248. Your C learning is 0.00% complete. Login to check your learning progress.
Learn on Youtube


Questions index C Questions C++ Questions Win32 MFC COM/DCOM DLL Questions