Last active
August 29, 2015 14:20
-
-
Save martinpinto/696a48a66accff345441 to your computer and use it in GitHub Desktop.
Pinto Sort
This file contains 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 swap(arr, a, b) { | |
var tmp = arr[a]; | |
arr[a] = arr[b]; | |
arr[b] = tmp; | |
} | |
function sort(arr) { | |
var i = 0; | |
var isSorting = false; | |
while (i < arr.length) { | |
if (i == 0) | |
isSorting = false; | |
if (i == arr.length - 1) { | |
if (!isSorting) break; | |
i = 0; | |
} | |
else if (arr[i + 1] < arr[i]) { | |
swap(arr, i, i + 1); | |
isSorting = true; | |
i++; | |
} else { | |
i++; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment