Selection Sort (With Code in Python-C++-Java-C)
Selection Sort (With Code in Python-C++-Java-C)
Compare minimum with the third element. Again, if the third element is
smaller, then assign minimum to the third element otherwise do
nothing. The process goes on until the last element.
3. A%er each iteration, minimum is placed in the front of the unsorted list.
Swap the first with minimum
4. For each iteration, indexing starts from the first unsorted element. Step
1 to 3 are repeated until all the elements are placed at their correct
positions.
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
Time Complexity
Best O(n2)
Worst O(n2)
Average O(n2)
1st (n-1)
2nd (n-2)
3rd (n-3)
... ...
last 1
Number of comparisons:
(n - 1) + (n - 2) + (n - 3) + ..... + 1 = n(n - 1) / 2 nearly equals to
n2 .
Complexity = O(n2)
Time Complexities:
The time complexity of the selection sort is the same in all cases. At every
step, you have to find the minimum element and put it in the right place.
The minimum element is not known until the end of the array is not
reached.
Space Complexity:
2. Quicksort (/dsa/quick-sort)
Share on:
(h"ps://www.facebook.com/sharer/sharer.php? (h"ps://twi"er.com/intent/twe
u=h"ps://www.programiz.com/dsa/selection- text=Check%20this%20amazi
sort) sort)
(/dsa/insertion-sort) (/dsa/bubble-sort)
(/dsa/shell-sort) (/dsa/quick-sort)
: