Implement a selection sort algorithm that swaps values at a given position with the minimum value index. Use the swap and indexOfMinimum functions provided below. Despite following the logic, there seems to be an issue preventing the code from successfully running the assertion statement.
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) {
var j;
var smallest;
for(j = 0; j < array.length; j++)
{
smallest = indexOfMinimum(array, 0);
swap(array , j , smallest);
}
};
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]);