Created
June 30, 2016 18:44
-
-
Save chirag64/91d65934ea394e1399eea07afbbe119b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function GnomeSort(arr) { | |
| for (var i = 0; i < (arr.length - 1);) { | |
| // Compare next 2 elements, if they are in correct order, go ahead, else swap them and go back one step | |
| if (arr[i] <= arr[i + 1]) { | |
| i++; | |
| } else { | |
| var temp = arr[i]; | |
| arr[i] = arr[i + 1]; | |
| arr[i + 1] = temp; | |
| if (i > 0) { | |
| i--; | |
| } | |
| else { | |
| i++; | |
| } | |
| } | |
| } | |
| return arr; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment