Skip to content

Instantly share code, notes, and snippets.

@mdsaleemj
Last active December 27, 2016 10:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mdsaleemj/9243b24673ec8298bf8dab52ef5e5bd1 to your computer and use it in GitHub Desktop.
Save mdsaleemj/9243b24673ec8298bf8dab52ef5e5bd1 to your computer and use it in GitHub Desktop.
sorting-algorithms
SELECTION SORT - BASIC SORTING (WITHOUT OPTIMIZATION)

for(i=0;i<array.length;i++) {
    //assume i to be  index value of smallest value
    var minIndex = i;
    //loop through the rest of element and find the index of smallest element
    for(j=i+1;j<array.length;j++){
      if(array[j] < array[minIndex]){
          minIndex = j;
      }
      
    }//by the end of the inner loop you will get the smallest element index
        
    //swap it with current index (i) of outer loop.
        var temp = array[i];
        array[i] = array[minIndex];
        array[minIndex] = temp;
  
    console.log("iteration i="+i+"  =>  "+ array.toString());
  
}
  console.log("OUTPUT:",array.toString());

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment