Search This Blog

2024/04/06

Javascript Algorithm:Selection Sort(FreeCodeCamp)

Implement Selection Sort:
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.

Test Case:
1) selectionSort should be a function.
2) selectionSort should return a sorted array (least to greatest).
3) 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.
4) selectionSort should not use the built-in .sort() method.

Solution:
function selectionSort(array) {
for (let i = 0; i < array.length -1; i++) {
//find lowest element in array
let lowestIndex = i;
let lowest = array[i];
for (let j = i + 1; j < array.length; j++) {
if (lowest > array[j]) {
lowestIndex = j;
lowest = array[j];
}
}

//swap
[array[i], array[lowestIndex]] = [array[lowestIndex], array[i]];
}
return array;
}

let result = selectionSort([
1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92,
]);
console.log(result);

No comments:

Post a Comment