showing results for - "selection sort javascript"
Carlos
01 Nov 2017
1const selectionSort = array => {
2  const arr = Array.from(array); // avoid side effects
3  for (let i = 0; i < arr.length - 1; i++) {
4    let minPos = i;
5    for (let j = i + 1; j < arr.length; j++) {
6      if (arr[j] < arr[minPos]) {
7        minPos = j;
8      }
9    }
10    if (i !== minPos) {
11      [arr[i], arr[minPos]] = [arr[minPos], arr[i]];
12    }
13  }
14  return arr;
15};
16
17console.log(selectionSort([4, 9, 2, 1, 5]));
18
Amelie
29 Jun 2016
1let selectionSort = (arr) => {
2    let len = arr.length;
3    for (let i = 0; i < len; i++) {
4        let min = i;
5        for (let j = i + 1; j < len; j++) {
6            if (arr[min] > arr[j]) {
7                min = j;
8            }
9        }
10        if (min !== i) {
11            let tmp = arr[i];
12            arr[i] = arr[min];
13            arr[min] = tmp;
14        }
15    }
16    return arr;
17}
queries leading to this page
selection sorting in jsjavascript selection sortselection sort js codehow to sort select option in javascriptselection sort arrayjavascript sort select optionsselection sort in jsselection sorting in javascriptselection sort program in javascriptsort select options with javascriptjs sort select optionsslection sort javascriptselection sort algorithm javascriptselection sot explain in jsjavascript select sort optionsselection sort in algorithm in jsjavascript select sort optionselection sort jsselection sort code in javascripthow to selection sort use in jacascript selectino sort in javascriptslection sort in jssort on select javascriptsort select option jshow to sort order of select options javascriptselection sort in place jshow to write a select sort in javascriptselection sort running time in javascripthow to sort select options javascriptselection sort example code jsimplement selection sort in jsselection sorting i jshow to create a selection sort algorithme javascriptselection sort by taking inputs from html code in javascriptseletion sorting in jswhat is selection sort algorithm javascriptselection sort java scriptselection sort using arrayjavascript selection sort exampleselection sort explanation javascripthow to write a selection sort in javascriptselect sorting javascriptselectionsortt jswhat is selection sort in array javascriptselection sort javascript codevanilla javascript selection sortcreate selection javascriptselectionsort javascriptselection sort javascript exampleimplement the 22selection sort 22 in javascript js selection sort algorithmjavascript code for selection sortsort select options javascriptselection sort in java scripthow to sort select option javascriptsequential sort javascriptselection sort javascriptselection sort in javascriptiselection sort javascriptseelction sort algorithm in javascriptselection sort javascript arrayjs selectionjavascript selection sortjs selection sortselection sort algorithm jsslection sort js algotithmselection sort javascript