In-place Selection Sort

Ashok Be
2 min readNov 6, 2017

--

In-place selection sort recursive code in Javascript

Just like when you play cards

Following Part 1, Part 2 explores the in-place recursive Selection Sort technique which is not as straightforward, but still fun to wrap your head around using the JavaScript splice function.

The in-place select sort algorithm calls for a swap of the minimum value (basis for selection) with a specific indexed location, starting from 0 to the one-minus length of array.

Line 21 secures the minimum value in the variableminVal which is spliced in place in the index location idx in Line 23. This is after the element in that indexed location has already been spliced into place at the location where the minimum value originally had been discovered.

And finally, the loop continues to proceed to handle each of the other elements in the list. But, wait a minute! There is an important tweak you have to make to the helper function indexOfMinValue, which is as follows:

Basically, the only addition is Line 10 which filters out those elements that are not integers. More importantly, the i < iMin check is required to ensure that the comparison check begins with the first element of the unsorted sub-list, which is located at thestart index.

Happy Sorting!

--

--

Ashok Be

Entrepreneur, struggle-to-code evangelist, paradox collector, green tea and black coffee enthusiast. And yes, squash!