Skip to content

Instantly share code, notes, and snippets.

@agustarc
Last active December 21, 2016 20:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agustarc/b896f2925b3256ecfb75fa5e4fd014aa to your computer and use it in GitHub Desktop.
Save agustarc/b896f2925b3256ecfb75fa5e4fd014aa to your computer and use it in GitHub Desktop.
public class QuickSort {
public static void quickSort(int[] arr) {
quickSort(arr, 0, arr.length -1);
}
private static void quickSort(int[] arr, int begin, int end) {
if (begin >= end) {
return;
}
int middle = begin + (end - begin) / 2;
int pivot = arr[middle];
int left = begin;
int right = end;
while (left <= right) {
while (arr[left] < pivot) {
left++;
}
while (arr[right] > pivot) {
right--;
}
if (left <= right) {
arr[left] = arr[left] + arr[right];
arr[right] = arr[left] - arr[right];
arr[left] = arr[left] - arr[right];
left++;
right--;
}
}
if (begin < right) {
quickSort(arr, begin, right);
}
if (end > left) {
quickSort(arr, left, end);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment