Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript selection sort

function selectionSort(arr){
    for(let i = 0; i<arr.length; i++){
      //find min number in subarray 
      //and place it at ith position
        let minptr = i;
        for(let j = i+1; j<arr.length; j++){
            if(arr[minptr] > arr[j]){
                minptr = j;
            }
        }
      //swap
        let temp = arr[i];
        arr[i] = arr[minptr];
        arr[minptr] = temp;
    }
    return arr;
}

let a = [99,44,6,2,1,5,63,87,283,4,0];
console.log(selectionSort(a));
Comment

How to perform selection sort, in JavaScript?

/*
This is an implementation of the selection sort
algorithm.
Idea is to sort a list by successively putting
each value in its final sorted position.

Let n be the size of the list to sort

Time complexity: O(n^2),
Space complexity: O(1)
*/
function selectionSort(arr) {
  let min;
  // idx is position to fill up with next smallest value
  for (let idx = 0; idx < arr.length - 1; idx++) {
    min = idx;
    // Look for next smallest value in rest of array
    for (let scan = idx + 1; scan < arr.length; scan++) {
      if (arr[scan] < arr[min]) {
        min = scan;
      }
    }
    // Exchange current value with the next smallest value
    swap(arr, idx, min);
  }
}
function swap(arr, i, j) {
  const temp = arr[i];
  arr[i] = arr[j];
  arr[j] = temp;
}
const arr = [1, 10, 3, 2];
selectionSort(arr);
console.log(arr); // [ 1, 2, 3, 10 ]
Comment

selection sort javascript

//selection sort javascript

const selectionSort = array => {
  const arr = Array.from(array); // avoid side effects
  for (let i = 0; i < arr.length - 1; i++) {
    let minPos = i;
    for (let j = i + 1; j < arr.length; j++) {
      if (arr[j] < arr[minPos]) {
        minPos = j;
      }
    }
    if (i !== minPos) {
      [arr[i], arr[minPos]] = [arr[minPos], arr[i]];
    }
  }
  return arr;
};

console.log(selectionSort([4, 9, 2, 1, 5]));
Comment

selection sort javascript

//selection sort javascript

function selectionSort(arr){
    for(let i = 0; i<arr.length; i++){
        let minptr = i;
        for(let j = i+1; j<arr.length; j++){
            if(arr[minptr] > arr[j]){
                minptr = j;
            }
        }
      //swap
        let temp = arr[i];
        arr[i] = arr[minptr];
        arr[minptr] = temp;
    }
    return arr;
}

let a = [99,44,6,2,1,5,63,87,283,4,0];
console.log(selectionSort(a));

Comment

PREVIOUS NEXT
Code Example
Javascript :: como percorrer um objeto js 
Javascript :: multiple class to same click jquery 
Javascript :: print in javascript 
Javascript :: nuxt auth user info 
Javascript :: autocannon npm 
Javascript :: check if a string matches a regex javascript 
Javascript :: modal slide from right 
Javascript :: from json timestamp to date python 
Javascript :: angular input date pattern validation 
Javascript :: 1 line password strength checker jquery 
Javascript :: text input underline react native 
Javascript :: how to convert react component to image 
Javascript :: return statement javascript 
Javascript :: remove object if key is duplicate javascript 
Javascript :: Updating javascript object property 
Javascript :: take string until react 
Javascript :: javascript check if it has passed midnight 
Javascript :: floor javascript 
Javascript :: mongoose find in array 
Javascript :: key js 
Javascript :: how to click on alret dialog with pupeteer 
Javascript :: properly print json in colab 
Javascript :: parentnode javascript 
Javascript :: mongooseautoincrement 
Javascript :: sortingDataAccessor 
Javascript :: use axios cancel token in react.js useEffect 
Javascript :: How to submit form with enter press in html textarea 
Javascript :: get coords of html element js 
Javascript :: useWidthSize 
Javascript :: javascript push 
ADD CONTENT
Topic
Content
Source link
Name
1+9 =