Python Selection Sort

In this article, we will discuss another type of sorting called “selection sort”. A straightforward sorting method called selection sort alternates the smallest member from the list’s unsorted section with the list’s first unsorted element. In this article, we will talk about selection sorting in Python. We will also provide some examples of programs. The main purpose of this type of sorting is to arrange all the elements in ascending or descending order based on the requirements. If you are new to this concept, this article contains all the required information on selection sort.

Selection Sort in Python

and places that unsorted element at the beginning of the list to sort the list. Selection sort is a simple algorithm that works repeatedly to locate the smallest or largest elements in an unsorted array. It can also be beneficial in situations where the size of the array is already partially sorted.

Suppose we have the following array:

Twenty (20) is the first element. Finding the least integer from the remaining array is the next step. Two (2) is the smallest number between 4, 3, and 2. Thus, we change 20 to 2.

The new array is [2,4,3,20]. Again, this step is repeated.

Finally, we get the sorted array as [2,3,4,20].

What Is the Process of Selection Sort?

In this section, we will discuss the working of selection sort in Python. We put the first element of the array as a minimum value. Then, we compare the minimum value of the list with the second element, and so on. If the second element is greater than the minimum, we shift the minimum value to start and assign the second value to the minimum label. In this way, we easily sort the array. We also use an index for comparison in this case.

Example 1:
Let’s discuss the process of selection sorting with the help of examples. We use a simple example to demonstrate the process of selection sorting. The reference code for this example is mentioned in the following:

def selection_sort(arrN):
    for iteration in range(len(arrN)):
        min_ind = iteration
        for inner in range(iteration+1, len(arrN)):
            if arrN[inner] < arrN[min_ind]:
                min_ind = inner
        arrN[iteration], arrN[min_ind] = arrN[min_ind], arrN[iteration]
    return arrN
print(selection_sort([34, 25, 65, 21, 1,49]))

In this example, we define a function named “selection_sort” in which we pass an array to which we want to apply the selection sort. After that, we apply the “for” loop in which we take the “iteration” variable that runs until its value is equal to the length of the “arrN” array. Now, we take a variable in which we store the minimum index value named “min_ind”. In “min_ind”, we assign the iteration value. If the value of iteration is “1”, then “1” is stored in “min_ind”.

After that, we execute the “for” again loop to run the value of the inner array in which we take the loop start value more than the previous “for” loop. If the value of iteration is “1”, the value of the “inner” variable is “iteration+1”. We apply the “if” statement inside this “for” loop to check if our array value like “arrN[inner]” is less than the array index value of “arrN[min_ind]”. Then, we assign the “inner” variable to the “min-ind” variable.

Now, we swap the arrays by shifting the iteration value to the “min_ind” value and the “min_ind” value to the iteration value in the array. This process is repeated again and again until the condition is satisfied. Then, we return this value of arrays towards the function. Lastly, we run the sort array function in the print statement to display on the console. Now, let’s discuss the output of the selection sort.

The output of the previous code that is shown on the console is as follows:

[1, 21, 25, 34, 49, 65]

Here, you can see that the list is successfully sorted.

Example 2:
Here, we look at one more example of selection sorting and explain it in detail. The reference code for this example is given in the following:

def selectionSort(array1):
    for iteration in range(len(array1)):
        minimum_index = iteration
        for i in range(iteration+1, len(array1)):
            if array1[i] < array1[minimum_index]:
                minimum_index = i
        array1[iteration], array1[minimum_index] = array1[minimum_index], array1[iteration]
    return array1
Array1=[30, 20, 50, 24, 10,40, 30]

We explain the code for this example here. First, we declare and initialize the function. Lastly, we execute this function to easily solve the selection sort. We need an array to pass on the selection sort function on which the sorting is applied.

We require two arrays in sorting: one array is for index checking and the other array or loop is used to check the elements of the array. Then, we shuffle the array elements in ascending or descending order. In the selection sort function, we make our selection sort logic as we initialize the first “for” loop whose range is equal to the length of an array. Suppose the length of an array is “7”, the loop runs seven times.

In this “for” loop, we initialize the “minimum_index” variable which sorts the value of iteration. At the first loop iteration, the “minimum_index” value is “1”, and this outer loop logic is done. Now, we run another inner loop that checks the elements of arrays. Then, we execute the “for” loop in which the start value of the loop which is 1 increment than the iteration and its length is equal to the array.

In this array, we compare two values one by one. If the first element is smaller than the index value, we swap these values. Now, the number is sorted in this way with the help of this code or logic. Then, lastly, return the function value that generates the sorted array. Now, we want to run the print statement to display the array on a console. So, we pass this array to the print statement. Let's look at the output of this function.

The output of this code is attached in the following:

[10, 20,24, 30, 30, 40, 50]


Let us now conclude this article. Selection sort is the type of sorting that we already considered in the previous discussion. These sorting algorithms are really helpful to sort an unsorted array or list in Python. Python also provides many sorting functions to manage the sorting array algorithms in the code. Here, we discussed the whole process with the help of examples. You can also use these examples in your Python environment to practice this type of sorting and gain a better understanding.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content