Created
January 30, 2014 06:44
-
-
Save sundeepblue/8703730 to your computer and use it in GitHub Desktop.
place odd number before even number in array
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
void place_odd_before_even(int A[], int n) { | |
if(n <= 1) return; | |
int left = 0, right = n-1; | |
while(left < right) { | |
while(left < right && A[left]%2 != 0) left++; | |
while(left < right && A[right]%2 == 0) right--; | |
if(left < right) | |
swap(A[left++], A[right--]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment