Skip to content

Instantly share code, notes, and snippets.

@kevjose
Last active March 7, 2017 10:42
Show Gist options
  • Select an option

  • Save kevjose/279e291ea13c9f6d707930d70aa825fe to your computer and use it in GitHub Desktop.

Select an option

Save kevjose/279e291ea13c9f6d707930d70aa825fe to your computer and use it in GitHub Desktop.

Revisions

  1. kevjose revised this gist Mar 7, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Sort.js
    Original file line number Diff line number Diff line change
    @@ -22,7 +22,7 @@ Array.prototype.selectSort = function(){
    for(var i = 0; i < this.length; i++){
    min = i;
    for(var j= i+1; j< this.length; j++){
    if(this[j] > this[min]){
    if(this[j] < this[min]){
    min = j;
    }
    }
  2. kevjose revised this gist Mar 7, 2017. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions Sort.js
    Original file line number Diff line number Diff line change
    @@ -31,3 +31,8 @@ Array.prototype.selectSort = function(){
    }
    }
    }


    var array = [9, 2, 5, 6, 4, 3, 7, 10, 1, 8];
    array.bubbleSort();
    console.log(array);
  3. kevjose created this gist Mar 7, 2017.
    33 changes: 33 additions & 0 deletions Sort.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    Array.prototype.swap = function(i, j){
    var temp = this[i];
    this[i] = this[j];
    this[j] = temp;
    }

    Array.prototype.bubbleSort = function(){
    var swapped;
    do{
    swapped = false;
    for(var i = 0; i < this.length; i++){
    if(this[i] > this[i+1]){
    this.swap(i, i+1);
    swapped = true;
    }
    }
    }while(swapped);
    }

    Array.prototype.selectSort = function(){
    var min;
    for(var i = 0; i < this.length; i++){
    min = i;
    for(var j= i+1; j< this.length; j++){
    if(this[j] > this[min]){
    min = j;
    }
    }
    if(min !== i){
    this.swap(i, min)
    }
    }
    }