Created
March 20, 2018 18:15
-
-
Save Dimanaux/e41addf35b789bb2af4797ed7f6ad0c9 to your computer and use it in GitHub Desktop.
QuickSort for int[] array!
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 Array { | |
public static void sort(int[] array) { | |
quickSort(array, 0, array.length - 1); | |
} | |
private static void quickSort(int[] array, int begin, int end) { | |
if (begin < end) { | |
int p = partition(array, begin, end); | |
quickSort(array, begin, p - 1); | |
quickSort(array, p + 1, end); | |
} | |
} | |
private static int partition(int[] array, int left, int right) { | |
int pivot = array[left]; | |
// переместили pivot в конец ??? не знаю зачем, но так в учебнике | |
swap(array, left, right); | |
/* Все значения, не превышающие pivot, перемещаются в начало | |
* массива, и pivot вставляется сразу после них. */ | |
int store = left; | |
for (int idx = left; idx < right; idx++) { | |
if (array[idx] <= pivot) { | |
swap(array, idx, store); | |
store++; | |
} | |
} | |
swap(array, right, store); | |
return store; | |
} | |
private static void swap(int[] array, int i, int j) { | |
int tmp = array[i]; | |
array[i] = array[j]; | |
array[j] = tmp; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
and quicksort works because I helped you with lines 9 and 10