function newsort(arr, left, right){
for(var i= left; i < right; ++i){
var min = i;
for (var j = i; j < right; ++j){
if (arr[min] > arr[
Eloquent solution:
const selectionSort = (items) => {
items.forEach((val, i, arr) => {
const smallest = Math.min(...arr.slice(i))
const smallestIdx = arr.indexOf(smallest)
if (arr[i] > arr[smallestIdx]) {
const temp = arr[i]
arr[i] = arr[smallestIdx]
arr[smallestIdx] = temp
}
})
return items
}
Standard solution:
const selectionSort = (arr) => {
for (let i=0; i <= arr.length-1; i++) {
// find the idnex of the smallest element
let smallestIdx = i
for (let j=i; j <= arr.length-1; j++) {
if (arr[j] < arr[smallestIdx]) {
smallestIdx = j
}
}
// if current iteration element isn't smallest swap it
if (arr[i] > arr[smallestIdx]) {
let temp = arr[i]
arr[i] = arr[smallestIdx]
arr[smallestIdx] = temp
}
}
return arr
}
Testing:
console.log( // [14, 29, 56, 72, 92, 98]
selectionSort([29, 72, 98, 14, 92, 56])
)
function selectionSort(arr) {
var temp = 0;
for (var i = 0; i < arr.length; ++i) {
for (var j = i + 1; j < arr.length; ++j) {
if (arr[i] > arr[j]) { // compare element with the reset of other element
temp = arr[i]; // swap the valuse from smallest to gretest
arr[i] = arr[j];
arr[j] = temp;
}
}
}
return (arr);
}
selectionSort([4,6,5,3,7,9]);
The selection sort algorithm says: repeatedly find the minimum of the unsorted array.
Recursive approach
Note: the second argument of selectionSort (i) is needed in order to sort elements of the unsorted array
function selectionSort(arr, i) {
if (i === 0) {
return arr;
}
const min = Math.min(...arr.filter((x, j) => j < i));
const index = arr.findIndex(x => x === min);
arr.splice(index, 1);
arr.push(min);
return selectionSort(arr, --i);
}
const unsortedArr = [5, 34, 5, 1, 6, 7, 9, 2, 100];
console.log('result', selectionSort(unsortedArr , unsortedArr.length))
The problem with your code is that the left and right parameters are passed in the wrong way round. Here is the working code:alert(newsort(arr, 0 ,arr.length));
You are basically doing the selection start is reverse order which will not work.
Your left should begin with 0 and right should end with arr.length.
newsort(arr, 0 ,arr.length);
this should work.
Check out this link if you want to implement selection sort in javascript.
https://learnersbucket.com/examples/algorithms/selection-sort-in-javascript/
var selectionSort = function(array){
for(var i = 0; i < array.length; i++){
//set min to the current iteration of i
var min = i;
for(var j = i+1; j < array.length; j++){
if(array[j] < array[min]){
min = j;
}
}
var temp = array[i];
array[i] = array[min];
array[min] = temp;
}
return array;
};
var array = [3,2,10,1]
console.log('selectionSort should return [1,2,3,10]-->',selectionSort(array));
It might be easier to reason with if you use a helper swap function:
//HELPER FUNCTION
var swap = function(array, firstIndex, secondIndex){
var temp = array[firstIndex];
array[firstIndex] = array[secondIndex];
array[secondIndex] = temp;
};
var array = [2,1];
swap(array, 0, 1)
console.log('swap should return [1,2] -->', array);
var selectionSort = function(array){
for(var i = 0; i < array.length; i++){
//set min to the current iteration of i
var min = i;
for(var j = i+1; j < array.length; j++){
if(array[j] < array[min]){
min = j;
}
}
swap(array, i, min);
}
return array;
};
var array = [3,2,10,1]
console.log('selectionSort should return [1,2,3,10]-->',selectionSort(array));
Visual of selection sort:
[3,1,2]
|-----> iterate over list. find that min = 1 so we swap current i (3) with min(1)
[1,3,2]
|---> iterate over list. find that min = 2 so we swap current i (3) with min(2)
[1,2,3]
|---> iterate over list. find that min = 3 so we swap current i (3) with min(3)