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]
How it works
The idea behind selection sort.
- Take the number at the first index in the array.
- Compare in to the rest of the numbers in the array.
- 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.
- 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)