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 :: prettier ignore line 
Javascript :: check for internet explorer browser in javascript 
Javascript :: arraylist of characters 
Javascript :: remove and add active class with jquery 
Javascript :: box shadow react native 
Javascript :: regex to allow only numbers letters and hyphen 
Javascript :: angular npm angular material 
Javascript :: npx create-react-app 
Javascript :: allow cross origin node 
Javascript :: how to refresh slick after tab function 
Javascript :: javascript regex french phone number 
Javascript :: react native clear cach 
Javascript :: Could not resolve dependency: peer react@"^18.2.0" from react-dom@18.2.0 
Javascript :: discordjs 13 TypeError Valid intents must be provided for the Client. 
Javascript :: javascript get previous element sibling 
Javascript :: data structures for coding interviews in javascript 
Javascript :: include partials ejs 
Javascript :: disable anchor tag jquery after click 
Javascript :: fetch get authorization header 
Javascript :: uppercase string in js 
Javascript :: js set date to midnight 
Javascript :: find a number that is closest to a specific number in javascript 
Javascript :: ajax header 
Javascript :: enzyme adapter react 17 
Javascript :: round function in jquery 
Javascript :: change background colour jquery 
Javascript :: react native disable warnings 
Javascript :: trim first character in javascript 
Javascript :: js format price 
Javascript :: js extract domain from email 
ADD CONTENT
Topic
Content
Source link
Name
2+9 =