Nodejs Array Selection Sort selectionSort()

Here you can find the source of selectionSort()

Method Source Code

//### 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 ww  w  . j ava  2s.co 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(', '));

Related

  1. selectSort()
    Array.prototype.selectSort = function() {
      for(var i = 0; i <= this.length - 2; i++) {
        var index = i;
        for(var z = i + 1; z <= this.length - 1; z++) {
          if(this[z] < this[index]) {
            index = z;
        if(index != i) {
    ...
    
  2. selectSort()
    Array.prototype.selectSort = function(){
      var temp,min_index,len = this.length;
      for(var i = 0;i < len;i++){
        min_index = i;
        for(var j = i + 1;j < len;j++){
          if(this[j] < this[min_index]){
            min_index = j;
        if(min_index != i){
          temp = this[i];
          this[i] = this[min_index];
          this[min_index] = temp;
      return this;
    
  3. selectionSort()
    Array.prototype.selectionSort = function () {
      var i, j, min;
      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;
    };
    
  4. selectionSort()
    Array.prototype.selectionSort = function () {
        'use strict';
        var lastIndex = this.length,
            temp,
            i,
            j,
            smaller;
        for( i = 0 ; i < lastIndex ; i++){
            smaller = i;
    ...
    
  5. selectionSort()
    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(', '));
    
  6. selectionSort()
    Array.prototype.selectionSort = function() {
      var sorted_array = [];
      var temp_array = this;
      var counter = 0;
      var arrEnd = this.length;
      while(counter < arrEnd) {
        var index = temp_array.indexOf(temp_array.min());
        var x = parseInt(temp_array.splice([index], 1).join(''))
        console.log(x);
    ...
    
  7. selectionSort()
    Array.prototype.selectionSort = function () {
        for (var i = 0; i < this.length; i++) {
            var index = i;
            for (var j = i+1; j < this.length; j++) {
                if(this[j] < this[index]){
                    index = j;
            this.swap(i, index);
    ...