Last active
December 21, 2016 20:04
-
-
Save agustarc/b896f2925b3256ecfb75fa5e4fd014aa to your computer and use it in GitHub Desktop.
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
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