Selection sort in Python

Introduction

Selection sort is a simple in-place (i.e. no auxiliary memory) comparison based sorting algorithm. It is an inefficient algorithm with O(n2) running time as it uses two nested loops.

Here is a summary of steps how the algorithm works:

  • Given an unsorted list
  • Start with the first element on the left side
  • Scan all other elements on the right side and find the smallest
  • Swap the smallest element with the first element if they are not equal
  • Now the first element is the smallest
  • Go to the second element, scan all elements to the right, find the smallest
  • Swap the smallest with the second element
  • Repeat these steps until all elements are sorted

Here is a cool Gypsy folk dance YouTube video demonstrating selection sort visually:

Selection sort python code

Here is an example Python source code:

Thanks for reading. For questions or feedback, please the comments section below.

Tags:

Add a Comment

Your email address will not be published. Required fields are marked *