Created
January 22, 2016 09:01
-
-
Save enriclluelles/0d989b0c1c5985ed8103 to your computer and use it in GitHub Desktop.
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 national_flag(a, p) { | |
function swap(i, j) { | |
// console.log("swapping a[" + i + "]:" + a[i] + " a[" + j + "]:" + a[j]); | |
var aux = a[i]; | |
a[i] = a[j]; | |
a[j] = aux; | |
} | |
var value = a[p]; | |
var i = 0; | |
var k = 0; | |
var j = a.length - 1; | |
while(i <= j) { | |
if (a[i] < value) { | |
swap(i,k); | |
i++; | |
k++; | |
} else if(a[i] > value) { | |
swap(i,j); | |
j--; | |
} else { | |
i++ | |
} | |
} | |
console.log(a) | |
} | |
national_flag([1,2,3,1,2,3,1,2,3], 1) | |
var a1 = [3,4,5,432,54,36,5,6,546,45,65,6,45,645,6,546,459,89,78,987,9,78]; | |
national_flag(a1, 1) | |
var a2 = [1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9]; | |
national_flag(a2, 1) | |
var a3 = [4,5,1,2,3,1,5,6,3,3,3,3,3,6,4, 4,5,6,4,4,5,6,4]; | |
national_flag(a3, 1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment