Introduction to Selection Sort

Sorting algorithm 01

Gunavaran Brihadiswaran
Mar 2 · 3 min read
Image by author
  • In each iteration, find the maximum/minimum element in the unsorted subarray and swap it with the first element of the unsorted subarray. After swapping, the first element of the unsorted subarray will be appended to the sorted subarray. Swapping the minimum value will sort the elements in ascending order while swapping the maximum value will sort the elements in descending order.
  • Continue the iteration until the last element
Image by author

Implementation in C

Analysis of Selection Sort

  1. Selection sort is an in-place algorithm which means it does not require additional memory space to perform sorting.
  2. The time complexity is O(n²). Suppose the number of elements to be sorted is n, initially, the minimum value should be found among n elements which requires (n-1) comparisons. In the next iteration, it requires (n-2) comparisons. The following figure shows the number of comparisons required in each iteration.
Image by author

Nerd For Tech

From Confusion to Clarification

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store