let's implement the selection sort algorithm using python.

Selection sort is an algorithm that selects the smallest element from an unsorted list in each iteration and places that element at the beginning of the unsorted list.

Let's See How Selection Sort Works

Step 1: Set the first element as minimum

Step 2: Compare minimum with the second element. If the second element is smaller than minimum, assign the second element as minimum.

Step 3: 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.

Step 4: After each iteration, minimum is placed in the front of the unsorted list.

Step 5: 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.

Complexity : O(n2 )


$ python selectionSort.py 


[1, 2, 3, 4, 5]