Selection Sort: By-Abhishek Bhardwaj
Selection Sort: By-Abhishek Bhardwaj
SORT
BY- ABHISHEK BHARDWAJ
INTRODUCTION TO SELECTION SORT
2 8 3 9 6
EXAMPLE (CONTINUED)
• Second Pass:
• For the second position, where 8 is present, again traverse the rest of the array in a sequential manner.
2 8 3 9 6
• After traversing, we found that 3 is the second lowest value in the array and it should appear at the second
place in the array, thus swap these values
2 3 8 9 6
• Third Pass:
• Now, for third place, where 8 is present again traverse the rest of the array and find the third least value
present in the array.
2 3 8 9 6
• While traversing, 6 came out to be the third least value and it should appear at the third place in the array,
thus swap 6 with element present at third position.
2 3 6 9 8
EXAMPLE (CONTINUED)
• Fourth pass:
• Similarly, for fourth position traverse the rest of the array and find the fourth least element in the array
• As 8 is the 4th lowest value hence, it will place at the fourth position.
2 3 6 8 9
• Fifth Pass:
• At last the largest value present in the array automatically get placed at the last position in the array
• The resulted array is the sorted array.
2 3 6 8 9
SELECTION SORT: ANALYSIS
• COMPLEXITY
The time complexity of Selection Sort is O(N2) as there are two nested loops:
One loop to select an element of Array one by one = O(N)
Another loop to compare that element with every other Array element = O(N)
Therefore overall complexity = O(N)*O(N) = O(N*N) = O(N2)
• Is Selection Sort Algorithm stable?
Selection sort is an unstable algorithm.
ADVANTAGES & DISADVANTAGES OF
SELECTION SORT
Advantages
• The main advantage of selection sort is that it performs well on small lists.
• No additional temporary storage is required.
Disadvantages
• The primary disadvantage of selection sort is its poor efficiency while dealing
with long lists.
• The selection sort requires n-squared number of steps for sorting n number of
elements.