Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static long quickSort(int[] list) {
- quickSortHelper(list, 0, list.length - 1);
- return list;
- }
- public static void quickSortHelper(int[] list, int bottom, int top) {
- if(bottom < top) {
- int midpoint = partition(list, bottom, top);
- quickSortHelper(list, bottom, midpoint - 1);
- quickSortHelper(list, midpoint + 1, top);
- }
- }
- protected static int partition(int[] list, int bottom, int top) {
- int pivot = list[top];
- int firstAfterSmall = bottom;
- for(int i = bottom; i < top; i++) {
- if(list[i] <= pivot) {
- swap(list, firstAfterSmall, i);
- return firstAfterSmall;
- }
- }
- swap(list, firstAfterSmall, top);
- return firstAfterSmall;
- }
- protected static void swap(int[] list, int i, int j) {
- int temp = list[i];
- list[i] = list[j];
- list[j] = temp;
- }
Advertisement
Add Comment
Please, Sign In to add comment