Javascript Array selectionSort() method

Description

Javascript Array selectionSort() method


Array.prototype.selectionSort = function () {
  var i, j, min;//www . j a  v a2  s . co m
  var temp;
  for (i = 0; i < this.length - 1; i++) {
    min = i;
    for (j = i + 1; j < this.length; j++) {
      if (this[min] > this[j]) {
        min = j;
      }
      temp = this[min];
      this[min] = this[i];
      this[i] = temp;
    }
  }
};

Javascript Array selectionSort()

Array.prototype.selectionSort = function () {
    for (var i = 0; i < this.length; i++) {
        var minIndex = i;
        for (var j = i + 1; j < this.length; j++) {
            if (this[minIndex] > this[j]) minIndex = j;
        }/*from   w w  w  . j  av  a2 s.com*/
        if (minIndex !== i) {
            var temp = this[i];
            this[i] = this[minIndex];
            this[minIndex] = temp;
        }
    }
    return this;
};

Javascript Array selectionSort()

Array.prototype.selectionSort = function () {
    'use strict';

    var lastIndex = this.length,
        temp,/*from  w  w  w .  j  av  a  2 s  .  co  m*/
        i,
        j,
        smaller;

    for( i = 0 ; i < lastIndex ; i++){
        smaller = i;
        for ( j = i + 1 ; j < lastIndex ; j++ ) {

            if(this[j] < this[smaller]){
                smaller = j;
            } 

        }
        
        if(i != smaller){
            temp = this[i];
            this[i] = this[smaller];
            this[smaller] = temp;
        }
        
    }

    return this;
};

var vector = [50,6, 2, 5, 23, 9, 17, 29, 3];

console.log(vector);

console.log(vector.selectionSort());

Javascript Array selectionSort()

/* Sorting an array means to arrange its elements in increasing order.
   Write a script to sort an array./*from ww  w .j a v  a  2  s  .com*/
   Use the selection sort algorithm: Find the smallest element, move it at the first position,
   find the smallest from the rest, move it at the second position, etc. */

Array.prototype.selectionSort = function() {
    var i, j, temp, len = this.length;
    for (i = 0; i < len; i += 1) {
        for (j = i + 1; j < len; j += 1) {
            if(this[i] > this[j]) {
                temp = this[i];
                this[i] = this[j];
                this[j] = temp;
            }
        }
    }

    return this;
};

var arr = [5, 6, 4, 3, 2, 1];
arr.selectionSort();
console.log(arr);

Javascript Array selectionSort()

Array.prototype.selectionSort = function () {
    // Loop through each number
    for (var i = 0; i < this.length; i++) {
        // Assume the current index holds the smallest number
        var minIndex = i;

        // Loop through all the numbers after i to search for the smallest number
        for (var j = i + 1; j < this.length; j++) {
            // Update min_index if a smaller number is found
            if (this[minIndex] > this[j]) minIndex = j;
        }/*from   w ww .  jav  a  2  s .co m*/

        // If a smaller number was found, swap them
        if (minIndex !== i) {
            var temp = this[i];
            this[i] = this[minIndex];
            this[minIndex] = temp;
        }
    }

    return this;
};

Javascript Array selectionSort()

/*//w  w  w . ja  va 2  s  .c o  m
 *Adds a method into array object that sorts the array via
 *selection sort in increasing order
 *Algorithm Complexity: O(n^2)
 *
 *@return sorted array
 *@author ketanSaxena
*/
Array.prototype.selectionSort = function() {
  var index, innerIndex, LENGTH = this.length;
  for (index = 0; index < LENGTH - 1; index++) {
    var minIndex = index;
    for (innerIndex = index + 1; innerIndex < LENGTH - index; innerIndex++) {
      if(this[innerIndex] < this[minIndex]) {
        minIndex = innerIndex;
      }
    }
    if (minIndex != index) {
      this._swap(minIndex, index);
    }
  }

  return this;
}

Array.prototype._swap = function (x ,y) {
  var b = this[x];
  this[x] = this[y];
  this[y] = b;
  return this;
}

Javascript Array selectionSort()

//### Problem 5. Selection sort
//* Sorting an array means to arrange its elements in increasing order.
//* Write a script to sort an array.
//* Use the [selection sort](http://en.wikipedia.org/wiki/Selection_sort) algorithm: Find the smallest element, move it
// at the first position, find the smallest from the rest, move it at the second position, etc.
//_Hint: Use a second array_

Array.prototype.selectionSort = function(){
    var sorted = [];

    while(this.length) {
        var minInd = 0;
        for (var ind = 0; ind < this.length; ind++) {
            if(this[ind] < this[minInd]) minInd = ind;
        }/*from www . ja va  2s  .  c  o  m*/
        sorted.push(this[minInd]);
        this.splice(minInd, 1);
    }
    this.push(sorted);
}

var numbers =  [8, 12, 3, 4,1276,123,1245,-123, 5, 2, 11, 13, 7, 4, 15, 14, 12, 8, 1];
numbers.selectionSort();

console.log(numbers.join(', '));

Javascript Array selectionSort()

// Patched on the Array Prototype
Array.prototype.selectionSort = function () {
  console.time('SelectionSort')
  var arr = this/*w w w . j ava  2 s.c o  m*/
  // loop through each element in our array
  for (let i = 0; i < arr.length; ++i) {
    let minValue = arr[i]
    let minIndex = i
    // find the minimum value in the remainder of the array
    for (let j = i + 1; j < arr.length; ++j) {
      if (arr[j] < minValue) {
        minValue = arr[j]
        minIndex = j
      }
    }
    // swap values if not the same
    if (i !== minIndex) {
      let temp = arr[i]
      arr[i] = arr[minIndex]
      arr[minIndex] = temp
    }
  }
  console.timeEnd('SelectionSort')
}

let arr = []
let max = 1000000
for (let i = 0; i < max; ++i) {
  arr.push(Math.round(Math.random() * 10))
}
console.log('sorting arr on length: ', max)
arr.selectionSort()
// console.log('sorted: ', arr)

Javascript Array selectionSort()


/*Sorting an array means to arrange its elements in increasing order.
 Write a script to sort an array.//from w  ww.  j ava 2 s . co  m
 Use the selection sort algorithm: Find the smallest element, move it at the first position, find the smallest from the rest, move it at the second position, etc.*/

Array.prototype.selectionSort=function(){
 var sorted=[];
 while(this.length){
  var minIndex=0;
  for(var i=0; i<=this.length;i++) {
   if (this[i] < this[minIndex]) {
    minIndex = i;
   }
  }
  sorted.push(this[minIndex]);
  this.splice(minIndex,1);
 }
 this.push(sorted);
};

var numbers=[4, 1, 1, 4, 2, 3, 4, 4, 1, 2, 4, 9, 3 ];
numbers.selectionSort();
console.log('numbers=[4, 1, 1, 4, 2, 3, 4, 4, 1, 2, 4, 9, 3 ];');
console.log(numbers.join(', '));



PreviousNext

Related