Skip to content

Instantly share code, notes, and snippets.

@vigack
Last active August 3, 2017 08:36
Show Gist options
  • Save vigack/e5c213ab0ba494bd134d7b410ba98ae0 to your computer and use it in GitHub Desktop.
Save vigack/e5c213ab0ba494bd134d7b410ba98ae0 to your computer and use it in GitHub Desktop.
QuickSort-Lomuto's Version
void swap(int *a, int *b){
int tmp;
tmp = *a;
*a = *b;
*b = tmp;
}
void quickSort(int left, int right, int *arr){
// base
if(left>=right)
return;
int i, j;
int pivot, tmp;
pivot = arr[right];
i = left;
while(i<right&&arr[i]<pivot)
i++;
j = i+1;
while(j<right){
if(arr[j]<pivot){
swap(arr+j, arr+i);
i++;
}
j++;
}
swap(arr+i, arr+right);
// sort left and right part
quickSort(left, i-1, arr);
quickSort(i+1, right, arr);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment