Skip to content

Instantly share code, notes, and snippets.

@rohith2506
Created March 3, 2015 05:30
Show Gist options
  • Save rohith2506/fde23e1eb4e702a84494 to your computer and use it in GitHub Desktop.
Save rohith2506/fde23e1eb4e702a84494 to your computer and use it in GitHub Desktop.
Rearrange +ve and -ve elements in an array
// Use parititon method of quick sort
void quick_sort(vector<int> v){
int i=-1;
for(int j=0; j<n; j++){
if(a[j] < 0){
i++;
swap(a[i],a[j]);
}
}
int neg = 0, pos = i+1;
while(pos < n && neg < pos && a[neg] < 0){
swap(a[pos], a[neg]);
neg = neg + 2;
pos = pos + 1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment