# Selection Sort Python – A Quick Tutorial and Implementation Guide

So friends, in Selection Sort Python tutorial, we will learn how to sort data using selection sort method. We will also see selection sort algorithm, complexity and its implementation in python. So let’s start this tutorial.

Before diving into selection sort, we have to understand some basics of sorting. So first of all i am figuring out the most popular topic of data structure i.e., Sorting.

## What Is Sorting – A Quick Overview

• Sorting means ordering the data either into ascending or descending order within the array.
• It is one of the most common data structure processing application.
• Sorting can be done on names, numbers and records

### Why Sorting Is Effective ?

For understanding the effectiveness of sorting, we will take an example.

• Consider you are searching a phone number from your phone dictionary.
• You will easily get number because the names in the phone book have been sorted into alphabetical order.
• But if in your phone, numbers are not sorted then you can’t find phone numbers easily. And it takes long time.

So the conclusion is that if your numbers are in sorted order then it takes less time for searching. That means sorting greatly improves the efficiency of searching.

### Sorting Methods

Sorting can be performed using several methods, they are following:

• Selection sort
• Merge sort
• Insertion sort
• Heap sort
• Bubble sort
• Quick sort

So guys, we have just learned some basics of sorting, now we will discuss selection sort and see how to implement it in python.

## Selection Sort Python Tutorial – Learn With  Examples

Selection sort is very simple method. It is in-place comparison based method. In this method, we need to compare the numbers and need to place them in the correct position.  So let’s start selection sort in python.

### Selection Sort Mechanism ?

Now we will understand the mechanism of selection sort.

• In selection sort, we pick a minimum element from a list and place it in the sorted part of list.
• Initially we consider first element as minimum element and compare it with other elements of list.
• And during comparison, if we find a minimum element then its refers as new minimum element.
• When we complete one full round of comparison then the minimum element is swapped with first element.
• And this process will continued until we sort all the elements.

### Selection Sort Algorithm

And now the question is that how we perform selection sort. So this is the answer :

1. Let’s consider the first element as sorted and the rest as unsorted
2. Assume the first element is the smallest element.
3. Checking,  if the first element is less than each of the other elements in list, then do:
1. If yes, do nothing
2. If no, choose the other smaller element as minimum and repeat step 3
4. When one iteration is completed, swap the minimum element with the first element of the list.
5. Now take the sublist(ignore sorted part) and and repeat all the above steps until all the elements in the list are sorted.

### Selection Sort Working Examples

Now let us take a example for understanding selection sort mechanism.

So consider a list which contains some elements.

And now we have to sort this list in ascending order. So let’s start the sorting process.

#### Round 1

In round 1, we select first element as minimum.

#### Round 2

In round, our minimum element is 7 and list is mylist = [0,7,9,6,2].

#### Round 3

In round 3, our minimum element is 9 and list is mylist = [0,2,9,6,7].

#### Round 4

In round 4, minimum element is 9 and list is mylist = [0,2,6,9,7].

Finally our list has been sorted successfully and the sorted list is  mylist = [0,2,6,7,9].

### Selection Sort – Big Oh Analysis

• Selection sort is not a fast sorting algorithm because it uses nested loop to sort.
• It is best suited for only small data sets.
• It runs in O(n2).

### Selection Sort Python Implementation

Let’s check the output :

Now we will see, how it looks like in each iteration. For doing this print list during swapping. So write following code –

And now you can see how sorting has been performed in each iteration.

You have noticed that even though after round 2, we can see the list is sorted, but there is no way for the algorithm to know this. Hence only after completely traversing the entire list, the algorithm stops.

### Time Complexity Of Selection Sort

 Best Case O(n2) Average Case O(n2) Worst Case O(n2)

## Related Articles :

So that’s it for Selection Sort Python tutorial. If you found this post helpful then please SHARE it with your friends. And for any question you can leave your comments below. Thank You 🙂