Updated March 28, 2023
Introduction to Selection Sort in Python
The selection sorting technique has been used to accomplish a suitable sort of the given elements in some specific order. However, the necessity of sorting techniques is such that the data which is being generated is very much badly ordered, then we need to set with a specific selection sorting technique. Among the available list of sorting techniques, selection sort is the most basic one. The selection sorting technique involves determining the minimum element and operating upon it to get the whole input set sorted. In this topic, we are going to learn about Selection Sort in Python.
Selection sort Logic
Here the most minimum element from the unsorted set is first determined; then, this most minimum element in the given unsorted set has been placed at the beginning of the unsorted set. Then this element will be positioned at the beginning or the first position of the unsorted set, which has been operated. Next, this operation will be carried upon each and every element in the unsorted set. So this means each and every minimum element in the unsorted set is determined and queued back to the formulated set one after the other.
The below example explains the selection sorting execution technique,
Ex array: [ 29,54,3,56,78,62]
The whole list is validated sequentially, and value 3 is determined as the lowest value from the given list. So the value 3 will get swapped with the position of 29. The result set will be looking something like the below,
Iteration1 outcome: [3,54, 29,56,78,62]
Again the entire list is validated sequentially, and value 29 will be determined as the main second smallest possible value from the mentioned list. So the value 29 will get swapped with the second position of 54. As a result, the outcome set will be as like below,
Iteration2 outcome: [3,29,54,56,78,62]
On performing the third iteration, it can be determined that the value 54 is the third smallest value, and it’s already associated with position 3 in the given list. This means there will be no changes to the current list since it is already of the given necessary format.
Iteration3 outcome: [3,29,54,56,78,62]
Again on evaluation, it will be determined that the value 56 is the fourth-smallest value, and this will get associated in the 4th position of the array or list. So since the existing list is of this format, there will not be any further changes to the list in the 4th iteration.
Iteration4 outcome: [3,29,54,56,78,62]
From the fifth iteration, it can be determined that the value 62 is the fifth smallest value in the list. So since it is currently placed as the last element in the list, it needs to be swapped with the element, which is one position before. so this means the value 62 will get swapped alongside the value 78. So 78 will be paced as the last element in the array. So now, the array becomes formulated as an ordered sequence.
Iteration6 outcome: [3,29,54,56,78,62]
Examples of Selection Sort in Python
Different examples are mentioned below:
Example – For Loops
Selection_Array = [29,54,3,56,78,62] # Print the keyed in input array print("Unsorted Array:",Selection_Array) print("\n") # Ever element in the array is selected for processing for element1 in range(len(Selection_Array)): # The most least index value form the giben list is initially associated to element1 Least_Index = element1 # From the holded item the next item is identified and looped across for finding the smallest value for element2 in range(element1+1, len(Selection_Array)): if Selection_Array[Least_Index] > Selection_Array[element2]: Least_Index = element2 # The previous element in the list and the currently holded least element is been swapped Selection_Array[element1], Selection_Array[Least_Index] = Selection_Array[Least_Index], Selection_Array[element1] # main code print ("Array after getting sorted by selection sort") print("Sorted Array:",Selection_Array)
The above-mentioned technique is the most commonly used one for selection sorting. It involves acceptance of a keyed in the list. The list is initially printed in the console. Next, each element of the key in the list is picked for processing. Finally, every picked item is compared with each and every other item in the list. If the current item is the smallest one, then it will be left as it is. but in case the current item is greater than some other element in the given list, then it will be compared and swapped upon with the element in the current position. This process will be followed across every other element in the list. The comparison and swapping process will be performed upon all the elements in the list. So, at last, the list retrieved will be very much of a sorted order. The newly generated ordered list is printed on the console.
Selection_array = [29,54,3,56,78,62] print("Unsoted Array",Selection_array) iterator = 0 while iterator < len(Selection_array): #smallest element in the sublist smallest = min(Selection_array[iterator:]) #Smallest element index index_of_smallest = Selection_array.index(smallest) #Swap the contents of the array Selection_array[iterator],Selection_array[index_of_smallest] = Selection_array[index_of_smallest],Selection_array[iterator] iterator = iterator + 1 print("Soted Array",Selection_array)
Explanation: Again here the selection sort technique is achieved here by means of a while loop. Here for the length of the given array or list, each element in the array is verified for the smallest value; whenever the smallest value is found, it has been swapped with the first item associated with the list. This swapping process is again iterated for all the elements present in the list. segregating each and every minimum element makes a list to be formulated very much in ascending order. Hence at the end of this process, a nicely ordered list is prepared. Again, this list is of ascending in order. Finally, the formulated list is printed to the console.
Sorting is considered to be among the key paradigms in programming perception. There are numerous sorting techniques involved, and the intent of these techniques is to achieve processing in either ascending or descending order, more generically in some orderly fashion. The above article nicely formulates a suitable way to achieve selection sorting of the given data entity in a very precise manner.
This is a guide to Selection Sort in Python. Here we discuss the Examples of Selection Sort in Python and the Selection sort Logic with iterations. You may also have a look at the following articles to learn more –