I’m not able to understand this method
Pls explain me or else give other method
Selection sort doubt
selection sort is one of the easiest sorting algorithm
How Selection Sort Works?
- Set the first element as
minimum
.
Select first element as minimum
- Compare
minimum
with the second element. If the second element is smaller thanminimum
, assign the second element asminimum
.
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.
Compare minimum with the remaining elements
- After each iteration,
minimum
is placed in the front of the unsorted list.
Swap the first with minimum
- 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.
The first iteration
The second iteration
The third iteration
The fourth iteration
Selection Sort 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
if you want to ask something about this feel free to ask
i hope this helps
if yes show your response with and don’t forgot to mark doubt as resolved