How to Implement Selection Sort in Python

In this tutorial, we will learn "How to implement Selection Sort in Python". The main objective is to understand and implement Selection Sort effectively. This guide will walk you step by step through the process, making it easy to follow and apply. By the end of this tutorial, you will have a solid understanding of how Selection Sort works in Python, helping you strengthen your problem-solving abilities and improve your overall coding skills in data structure implementation.

This topic is straightforward and easy to understand. By following the instructions provided, you will be able to complete it with ease. The program will guide you step by step through the process of using Selection Sort. So, let’s dive into the coding process and start implementing the solution to gain a deeper understanding of sorting algorithms in Python.

Getting Started:

First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/.

Creating Main Function

This is the main function of the application. The following code will display a simple GUI in terminal console that will display program. To do this, simply copy and paste these blocks of code into the IDLE text editor.
  1. def selection_sort(arr):
  2.     n = len(arr)
  3.     for i in range(n-1):
  4.         min_index = i
  5.         for j in range(i+1, n):
  6.             if arr[j] < arr[min_index]:
  7.                 min_index = j
  8.         arr[i], arr[min_index] = arr[min_index], arr[i]
  9.  
  10.  
  11. # MAIN LOOP
  12. while True:
  13.     print("\n========= Implement Selection Sort =========\n")
  14.  
  15.     # Input list from user
  16.     arr_input = input("Enter numbers to sort (space-separated): ").strip()
  17.     if not arr_input:
  18.         print("No input provided. Try again.")
  19.         continue
  20.  
  21.     arr = [int(x) for x in arr_input.split()]
  22.     print(f"Original list: {arr}")
  23.  
  24.     # Sort the list
  25.     selection_sort(arr)
  26.     print(f"Sorted list:   {arr}")
  27.  
  28.     # Try Again Option
  29.     opt = input("\nDo you want to try again? (yes/no): ").strip().lower()
  30.     if opt == "no":
  31.         print("Exiting program...")
  32.         break
  33.     elif opt != "yes":
  34.         print("Invalid choice. Exiting program...")
  35.         break

This Python program implements Selection Sort and allows users to sort a list of numbers interactively. It defines a `selection_sort` function that repeatedly finds the minimum element from the unsorted portion of the list and swaps it with the current position. In the main loop, the user inputs a space-separated list of numbers, the program displays the original list, sorts it using Selection Sort, and then shows the sorted list. After each run, the user can choose to sort another list or exit the program.

Output:

There you have it we successfully created How to Implement Selection Sort in Python. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

More Tutorials for Python Language

Python Tutorials