Skip to content

Instantly share code, notes, and snippets.

@amelieykw
Last active June 14, 2018 15:36
Show Gist options
  • Save amelieykw/93acf995b8ce4c30f41295385f36c93c to your computer and use it in GitHub Desktop.
Save amelieykw/93acf995b8ce4c30f41295385f36c93c to your computer and use it in GitHub Desktop.
[Selection Sorting]#DataStructure #algorithm #selectionSorting
var indexOfMinimum = function(array, startIndex) {
// Set initial values for minValue and minIndex,
// based on the leftmost entry in the subarray:
var minValue = array[startIndex];
var minIndex = startIndex;
// Loop over items starting with startIndex,
// updating minValue and minIndex as needed:
for (var i=minIndex+1; i<array.length;i++){
if(array[i]<minValue){
minValue = array[i];
minIndex = i;
}
}
return minIndex;
};
var array = [18, 6, 66, 44, 9, 22, 14];
var index = indexOfMinimum(array, 2);
// For the test array [18, 6, 66, 44, 9, 22, 14],
// the value 9 is the smallest of [..66, 44, 9, 22, 14]
// Since 9 is at index 4 in the original array,
// "index" has value 4
println("The index of the minimum value of the subarray starting at index 2 is " + index + "." );
Program.assertEqual(index, 4);
Program.assertEqual(indexOfMinimum(array, 0), 1);
Program.assertEqual(indexOfMinimum(array, 4), 4);
  1. Find the smallest card. Swap it with the first card.
  2. Find the second-smallest card. Swap it with the second card.
  3. Find the third-smallest card. Swap it with the third card.
  4. Repeat finding the next-smallest card, and swapping it into the correct position until the array is sorted.

Finding the index of the minimum element in a subarray

One of the steps in selection sort is to find the next-smallest card to put into its correct location. For example, if the array initially has values [13, 19, 18, 4, 10], we first need to find the index of the smallest value in the array. Since 4 is the smallest value, the index of the smallest value is 3.

Selection sort would swap the value at index 3 with the value at index 0, giving [4, 19, 18, 13, 10]. Now we need to find the index of the second-smallest value to swap into index 1.

It might be tricky to write code that found the index of the second-smallest value in an array. I'm sure you could do it, but there's a better way. Notice that since the smallest value has already been swapped into index 0, what we really want is to find the smallest value in the part of the array that starts at index 1. We call a section of an array a subarray, so that in this case, we want the index of the smallest value in the subarray that starts at index 1. For our example, if the full array is [4, 19, 18, 13, 10], then the smallest value in the subarray starting at index 1 is 10, and it has index 4 in the original array. So index 4 is the location of the second-smallest element of the full array.

var swap = function(array, firstIndex, secondIndex) {
var temp = array[firstIndex];
array[firstIndex] = array[secondIndex];
array[secondIndex] = temp;
};
var indexOfMinimum = function(array, startIndex) {
var minValue = array[startIndex];
var minIndex = startIndex;
for(var i = minIndex + 1; i < array.length; i++) {
if(array[i] < minValue) {
minIndex = i;
minValue = array[i];
}
}
return minIndex;
};
var selectionSort = function(array) {
for(var i=0;i<array.length;i++){
var minIndex = indexOfMinimum(array, i);
swap(array, i, minIndex);
}
};
var array = [22, 11, 99, 88, 9, 7, 42];
selectionSort(array);
println("Array after sorting: " + array);
Program.assertEqual(array, [7, 9, 11, 22, 42, 88, 99]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment