Python Searching and Sorting|Selection sort

Selection Sort Example-

Consider the following elements are to be sorted in ascending order-

6, 2, 11, 7, 5

The above selection sort algorithm works as illustrated below-

Step-01: For i = 0

Step-02: For i = 1

Step-03: For i = 2

Step-04: For i = 3

Step-05: For i = 4
Loop gets terminated as ‘i’ becomes 4.

The state of the array after the loops are finished is as shown-

With each loop cycle,
• The minimum element in unsorted sub-array is selected.
• It is then placed at the correct location in the sorted sub-array until array A is completely sorted.

Important Notes-

• Selection sort is an in-place algorithm.
• It performs all computation in the original array and no other array is used.
• Hence, the space complexity works out to be O(1).

• Selection sort is not a very efficient algorithm when data sets are large.
• This is indicated by the average and worst-case complexities.
• Selection sort uses the minimum number of swap operations O(n) among all the sorting algorithms.

Visitor Counter