In a nutshell, sorting is nothing but arranging data in a particular fashion. The `selection sort` is a popular sorting algorithm. In this blog post, you will learn `Selection Sort` In Python.

In Selection sort, First and foremost the list is divided into two parts left part being the sorted which is initially empty, and the right part unsorted which at the very beginning is the entire list then at each step elements are recursively compared and swapped depending on the condition till the list is entirely sorted.

While sorting a list in `ascending order` first, we need to go through the entire list and then find the minimum element and swap it with the element in the leftmost. Next, we need to find the second smallest element and swap it with the 2nd element of the list, and this keeps on going until the entire list is sorted.

Similarly, for sorting a list or array in descending order, we need to find the maximum element and swap it with the first element, and so on. Since at each step we are selecting the next element for the sorted list hence named selection sort. Below

#### Algorithm For Selection Sort In Python

• Step 1Â â€“Â Select the minimum element of the list and swap it with the first element (for Ascending order).
• Step 2:Â In every comparison, if any element is found smaller than the selected element, then both are swapped.
• Step 3:Â Repeat the same procedure with the element in the next position in the list until the entire list is sorted.

Output:

#### Explanation:

In the above program, theÂ `start` the position is initially 0Â  at each iteration it keeps increasing till the limitÂ `len(l)`, at every iteration when the nested loop finds a smaller element than the element at the positionÂ `start`Â the values get swapped this continues until the entire list is sorted.

#### Analysis Of Selection Sort

For an unsorted sequence of lengthÂ `n`Â the algorithm requiresÂ `n`Â step for the first scan then at each iteration it reduces by 1. Mathematically this can be expressed as,

The above expression concludes that this algorithm is proportional toÂ n2. Therefore for a given list of sizeÂ n, the time complexity of selection sort can be expressed as,

Worst Case Time Complexity [ Big-O ]:Â O(n2)
Best Case Time Complexity [Big-omega]:Â O(n2)
Average Time Complexity [Big-theta]:Â O(n2)
Space Complexity:Â O(1)

###### Selection Sort In Python

The article was published on October 25, 2020 @ 3:00 PM