Created
June 27, 2013 15:51
Sort color
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
class Solution { | |
public: | |
void sortColors(int A[], int n) { | |
// Start typing your C/C++ solution below | |
// DO NOT write int main() function | |
//enhanced quick sort | |
if(n==0) return; | |
int m1=0, m2=n-1, p; | |
while(A[m1]==0) | |
{ | |
m1++; | |
} | |
while(A[m2]==2) | |
{ | |
m2--; | |
} | |
p=m1; | |
while(p<=m2) | |
{ | |
if(A[p]==0) | |
{ | |
int temp = A[p]; | |
A[p] = A[m1]; | |
A[m1] = temp; | |
m1++; | |
} | |
else if(A[p] == 2) | |
{ | |
int temp = A[p]; | |
A[p] = A[m2]; | |
A[m2] = temp; | |
m2--; | |
} | |
if((A[p]!=0&&A[p]!=2)||p==m1) | |
p++; | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment