Write a Python program to sort a list of elements using the selection sort algorithm

Python Program for Selection Sort

The selection sort algorithm sorts an array by repeatedly finding the minimum element (considering ascending order) from unsorted part and putting it at the beginning. The algorithm maintains two subarrays in a given array.

1) The subarray which is already sorted.
2) Remaining subarray which is unsorted.

In every iteration of selection sort, the minimum element (considering ascending order) from the unsorted subarray is picked and moved to the sorted subarray.




# Python program for implementation of Selection
# Sort
import sys
A = [64, 25, 12, 22, 11]
# Traverse through all array elements
for i in range(len(A)):
# Find the minimum element in remaining
# unsorted array
min_idx = i
for j in range(i+1, len(A)):
if A[min_idx] > A[j]:
min_idx = j
# Swap the found minimum element with
# the first element
A[i], A[min_idx] = A[min_idx], A[i]
# Driver code to test above
print ("Sorted array")
for i in range(len(A)):
print("%d" %A[i]),


Please refer complete article on Selection Sort for more details!
Article Tags :
Python Programs
python sorting-exercises

How to implement selection sort in Python

Selection Sort algorithm is an in-place comparison-based algorithm. The algorithm divides the array into two segments:

  • The sorted part at the left end.
  • The remaining unsorted part at the right end.

The algorithm involves finding the minimum or maximum element in the unsorted portion of the array and then placing it in the correct position of the array.

Python Data Structures and Algorithms: Selection sort

Last update on January 04 2021 14:03:04 (UTC/GMT +8 hours)

Python Program for Selection Sort

Selection sort is a sorting algorithm that picks the smallest element from an unsorted list and sets it at the top of the unsorted list in each iteration. In this tutorial, we will perform a selection sort algorithm to sort an array.

Selection Sort - Basic Introduction

The concept behind the selection sort algorithm is to identify the smallest element in an array and sort it accordingly. The selection sort algorithm is an in-place comparison-based method that divides the input array into two sections: a sorted array on the left and an unsorted array on the right. Let us have a rough sketch of selection sorting:

  1. Assign the minimum value to array index 0
  2. Search Smallest element input in an array
  3. Swap with value at the location of minimum value
  4. Increment minimum value to point next element
  5. Repeat until the array is sorted

Now for a better understanding look at the diagram below:

Write a Python program to sort a list of elements using the selection sort algorithm

Algorithm

As of now, we have a rough understanding of the selection sort. Let us now have a look at the algorithm followed by the code for a better understanding:

  1. Create a function Selection_Sort that takes an array as an argument
  2. Create a loop with a loop variable i that counts from 0 to the length of the array – 1
  3. Declare smallest with the initial value i
  4. Create an inner loop with a loop variable j that counts from i + 1 up to the length of the array – 1.
  5. if the elements at index j are smaller than the element at index smallest, then set smallest equal to j
  6. swap the elements at indexes i and smallest
  7. Print the sorted list

Python Program

As discussed above in the algorithm, let us now dive into the programming part of the Selection Sort operation influenced by the algorithm. In this program user can input the list by giving whitespace in the console part:

def Selection_Sort(array): for i in range(0, len(array) - 1): smallest = i for j in range(i + 1, len(array)): if array[j] < array[smallest]: smallest = j array[i], array[smallest] = array[smallest], array[i] array = input('Enter the list of numbers: ').split() array = [int(x) for x in array] Selection_Sort(array) print('List after sorting is : ', end='') print(array)


Enter the list of numbers: 2 4 1 3 5
List after sorting is : [1, 2, 3, 4, 5]

Conclusion

In this tutorial, we have performed a Selection Sort operation in python to sort an array. The selection can be used to sort the small list. The time complexity of the selection sort is O(n2) and the space complexity is O(1).

What is Selection Sort?

SELECTION SORT is a comparison sorting algorithm that is used to sort a random list of items in ascending order. The comparison does not require a lot of extra space. It only requires one extra memory space for the temporal variable.

This is known as in-place sorting. The selection sort has a time complexity of O(n2) where n is the total number of items in the list. The time complexity measures the number of iterations required to sort the list. The list is divided into two partitions: The first list contains sorted items, while the second list contains unsorted items.

By default, the sorted list is empty, and the unsorted list contains all the elements. The unsorted list is then scanned for the minimum value, which is then placed in the sorted list. This process is repeated until all the values have been compared and sorted.

In this Algorithm tutorial, you will learn:

  • What is Selection Sort?
  • How does selection sort work?
  • Problem Definition
  • Solution (Algorithm)
  • Visual Representation
  • Selection Sort Program using Python 3
  • Code Explanation
  • Time Complexity Of Selection Sort
  • When to use selection sort?
  • Advantages of Selection Sort
  • Disadvantages of Selection Sort

Python Selection Sort Example: Sorting a List in Python

This tutorial helps you learn about creating an uncomplicated yet powerful method to amplify selection sort in Python using For loop.

We will create two simple functions to sort a data list in ascending and descending order.

Selection Sort

The selection sort algorithm is well known in the programming world. It is a top-notch manifestation of comparison sort.

The selection sort algorithm has O(n²) time complexity, due to which it becomes less effective on large lists, ususally performs worse than the similar insertion sort.

However, we will solve the Selection sort in python because of its uncomplicated behavior. It has the edge over other difficult algorithms for specific cases, especially where auxiliary memory is limited.

Selection Sort Algorithm

In this tutorial, you will learn about the selection sort algorithm and its implementation in Python, Java, C, and C++.

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