Selection Sort in Ascending Order in Python

1. Introduction

In this blog post, we will learn how to write a Python program to sort an array of integers in an Ascending Order using the Selection Sort algorithm.

Selection Sort is a comparison-based sorting algorithm. The core idea is to divide the list into two parts: a sorted section and an unsorted section. In each pass, the smallest (or largest, for descending order) element from the unsorted section is selected and swapped with the first unsorted element, thus extending the sorted section. Let's dive into this algorithm using Python.

2. Program Overview

1. selection_sort(): The primary function for implementing the selection sort algorithm.

2. print_list(): A helper function to display the list.

3. Sample list to demonstrate the sorting process.

3. Code Program

def selection_sort(arr):
    n = len(arr)
    
    # Traverse through all list elements
    for i in range(n):
        # Find the minimum element in the remaining unsorted list
        min_idx = i
        for j in range(i+1, n):
            if arr[j] < arr[min_idx]:
                min_idx = j
                
        # Swap the found minimum element with the first element
        arr[i], arr[min_idx] = arr[min_idx], arr[i]

def print_list(arr):
    for i in arr:
        print(i, end=" ")
    print()

# Driver code to test the functions
if __name__ == "__main__":
    arr = [64, 34, 25, 12, 22, 11, 90]
    print("Original list is:")
    print_list(arr)
    
    selection_sort(arr)
    
    print("\nSorted list in ascending order is:")
    print_list(arr)

Output:

Original list is:
64 34 25 12 22 11 90 

Sorted list in ascending order is:
11 12 22 25 34 64 90 

4. Step By Step Explanation

1. selection_sort(): This function sorts the list using the Selection Sort technique. The outer loop iterates over each element, while the inner loop searches for the smallest element in the unsorted section. Once found, the smallest element is swapped with the first unsorted element.

2. print_list(): This is a utility function to print list elements.

3. Driver code: Demonstrates the function with a sample list.

The Selection Sort algorithm is both simple and intuitive. However, due to its O(n^2) time complexity, it may not be ideal for large lists. The core principle revolves around selecting the smallest (or largest for descending order) element from the unsorted portion and moving it to the sorted portion.

Related Python Programs on Sorting Algorithms

  1. Bubble Sort in Ascending Order in Python
  2. Bubble Sort in Descending Order in Python
  3. Selection Sort in Ascending Order in Python
  4. Selection Sort in Descending Order in Python
  5. Insertion Sort in Descending Order in Python
  6. Insertion Sort in Ascending Order in Python
  7. Merge Sort in Ascending Order in Python
  8. Merge Sort in Descending Order in Python
  9. Quick Sort in Ascending Order in Python
  10. Quick Sort in Descending Order in Python
  11. Heap Sort in Ascending Order in Python
  12. Heap Sort in Descending Order in Python


Comments