Skip to content

Instantly share code, notes, and snippets.

@Whosemario
Last active April 1, 2016 08:36
Show Gist options
  • Select an option

  • Save Whosemario/74edd90a4ab089c7533c4f49492752f5 to your computer and use it in GitHub Desktop.

Select an option

Save Whosemario/74edd90a4ab089c7533c4f49492752f5 to your computer and use it in GitHub Desktop.
void Partition(int* numbers, int left, int right) {
if(right - left <= 0) return 0;
int value = numbers[right];
int ind = 0;
for(int i = left; i < right; ++i) {
if(numbers[i] < value) {
int tmp = numbers[i];
numbers[i] = numbers[ind];
numbers[ind++] = tmp;
}
}
numbers[right] = numbers[ind];
numbers[ind] = value;
return ind;
}
void QuickSortEx(int* numbers, int left, int right) {
if(right - left <= 0) return;
int mid = Partition(numbers, left, right);
QuickSortEx(numbers, left, mid - 1);
QuickSortEx(numbers, mid + 1, right);
}
void QuickSort(int* numbers, int length) {
if(numbers == NULL || length <= 1) return;
QuickSortEx(numbers, 0, length-1);
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment