DEV Community

Doug Jones
Doug Jones

Posted on

SORTING IT OUT πŸ‘¨πŸΏβ€πŸ’»

Selection Sort

I recently came across this algorithm problem in my studies.
It took me a while to try to full grasp it. So hopefully I can save someone else some time.

The Concept

In Selection Sort you are taking an array and sorting them by value. Until the entire array is in numerical order.
Example:

Before Selection Sort:[4,3,1,5,2]
After Selection Sort: [1,2,3,4,5]
Enter fullscreen mode Exit fullscreen mode

How it works

The idea behind selection sort.

  1. Take the number at the first index in the array.
  2. Compare in to the rest of the numbers in the array.
  3. If another number in the array is smaller than the one we are comparing it to. The lower number is compared to the rest of the array.
  4. After going through the array the lowest number is place in the spot of the number it was compared to. The process repeats itself until all number are in order and the entire array has been iterated through.

Final Thoughts

I have seen this done a number of ways.But I hope this helps break it down for you. Up to this point my favorite solution is to create an empty array and push the results into the new array after being iterated through.

I hope this helps break it down and makes it a little bit easier to understand. If you have a process that you like please feel free to share it in the comments.

Happy Coding πŸ‘¨πŸΏβ€πŸ’»πŸ‘¨πŸ»β€πŸ’»πŸ§‘πŸΎβ€πŸ’»πŸ‘©β€πŸ’»

Top comments (0)