Created
March 25, 2018 16:18
-
-
Save Dimanaux/2163e3ae0701855110d2535b8bfcea1e to your computer and use it in GitHub Desktop.
This file contains hidden or 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 ArrayAlgorithm { | |
| private static long iterations; | |
| public static long sort(int[] array) { | |
| iterations = 0; | |
| quickSort(array, 0, array.length - 1); | |
| return iterations; | |
| } | |
| 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 i = left; i < right; i++) { | |
| if (array[i] <= pivot) { | |
| swap(array, i, store); | |
| store++; | |
| } | |
| iterations++; | |
| } | |
| 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