Skip to main content

--description--

Here we will implement selection sort. Selection sort works by selecting the minimum value in a list and swapping it with the first value in the list. It then starts at the second position, selects the smallest value in the remaining list, and swaps it with the second element. It continues iterating through the list and swapping elements until it reaches the end of the list. Now the list is sorted. Selection sort has quadratic time complexity in all cases.

Instructions: Write a function selectionSort which takes an array of integers as input and returns an array of these integers in sorted order from least to greatest.

--hints--

selectionSort should be a function.

assert(typeof selectionSort == 'function');

selectionSort should return a sorted array (least to greatest).

assert(
isSorted(
selectionSort([
1,
4,
2,
8,
345,
123,
43,
32,
5643,
63,
123,
43,
2,
55,
1,
234,
92
])
)
);

selectionSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]) should return an array that is unchanged except for order.

assert.sameMembers(
selectionSort([
1,
4,
2,
8,
345,
123,
43,
32,
5643,
63,
123,
43,
2,
55,
1,
234,
92
]),
[1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]
);

selectionSort should not use the built-in .sort() method.

assert(isBuiltInSortUsed());

--seed--

--after-user-code--

function isSorted(a){
for(let i = 0; i < a.length - 1; i++)
if(a[i] > a[i + 1])
return false;
return true;
}

function isBuiltInSortUsed(){
let sortUsed = false;
Array.prototype.sort = () => sortUsed = true;
selectionSort([0, 1]);
return !sortUsed;
}

--seed-contents--

function selectionSort(array) {
// Only change code below this line
return array;
// Only change code above this line
}

--solutions--

function selectionSort(array) {
for (let i = 0; i < array.length-1; i++) {
let minimumIndex = i;
for (let j = i+1; j < array.length; j++){
if (array[j] < array[minimumIndex]) {
minimumIndex = j;
}
}
let value = array[minimumIndex];
array[minimumIndex] = array[i];
array[i] = value;
}
return array;
}