Skip to content

Instantly share code, notes, and snippets.

@Dimanaux
Created March 20, 2018 18:15
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 Dimanaux/e41addf35b789bb2af4797ed7f6ad0c9 to your computer and use it in GitHub Desktop.
Save Dimanaux/e41addf35b789bb2af4797ed7f6ad0c9 to your computer and use it in GitHub Desktop.
QuickSort for int[] array!
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;
}
}
@kehtolaulu
Copy link

and quicksort works because I helped you with lines 9 and 10

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment