Класс инструментов кратко и четко обобщает пять алгоритмов сортировки Java: быстрая сортировка, сортировка холма, сортировка вставки, сортировка кучи и сортировка слияния. В коде нет объяснений для этих алгоритмов сортировки. Я надеюсь самостоятельно проверить соответствующие инструкции для мыслительной части. Здесь я только суммирую эти алгоритмы для всех.
Общедоступный класс {public static <ancytype расширяется сопоставимо <? Super Anytype >> void insertionstort (anytype [] a) {insertionst (a, 0, a.length - 1); } Private Static <AnyType расширяет сопоставимо <? Super Anytype >> void insertionstort (anytype [] a, int left, int right) {int j; // Записать положение следующей цифры первого элемента меньше, чем TMP для (int p = слева; p <= справа; p ++) {AnyyType tmp = a [p]; for (j = p; j> Left && tmp.compareto (a [j - 1]) <0; j--) {a [j] = a [j - 1]; } a [j] = tmp; }} public static <ancytype расширяется сопоставимо <? Super Anytype >> void shellsort (anytype [] arr) {int j; for (int GAP = arr.Length / 2; GAP> 0; GAP / = 2) {for (int i = GAP; i <arr.length; i ++) {anyType tmp = arr [i]; for (j = i; j> = gap && tmp.compareto (arr [j - gap]) <0; j - = gap) {arr [j] = arr [j - gap]; } arr [j] = tmp; }}} private static int Leathild (int i) {return i * 2 + 1; } Private Static <AnyType расширяет сопоставимо <? Super Anytype >> void perculationown (anyype [] arr, int i, int size) {anytype tmp = arr [i]; for (int child; (child = LeathChild (i)) <size; i = ребенок) {if (ребенок! = size - 1 && arr [arl] .compareto (arr [arr+1]) <0) {Child ++; } if (tmp.com ppareto (arr [arld]) <0) {arr [i] = arr [child]; } else {break; }} arr [i] = tmp; } public static <AnyType расширяется сопоставимо <? Super Anytype >> void heapsort (anytype [] arr) {for (int i = arr.length / 2; i> = 0; i--) {perculationown (arr, i, arr.length); } for (int i = arr.length-1; i> = 0; i--) {Swapreferences (arr, 0, i); Перекулированный (ARR, 0, I); }} private static <ancytype расширяется сопоставимо <? Super Anytype >> void Swarferences (Anytype [] arr, int i, int j) {anytype tmp = arr [i]; arr [i] = arr [j]; arr [j] = tmp; } public static <AnyType расширяется сопоставимо <? Super Anytype >> void mergesort (anytype [] arr) {anytype [] tmp = ((AnyType []) Новый сопоставимый [arr.length]); Mergesort (arr, 0, arr.length - 1, tmp); } Private Static <AnyType расширяет сопоставимо <? Super Anytype >> void mergesort (anytype [] arr, int start, int end, anytype [] tmp) {if (start <end) {int mid = (start + end) >> 1; Mergesort (Arr, Start, Mid, TMP); Mergesort (Arr, Mid + 1, End, TMP); слияние (ARR, Start, Mid, End, TMP); }} private static <ancytype расширяется сопоставимо <? Super Anytype >> void merge (Anytype [] arr, int start, int mid, int end, anytype [] tmp) {int i = start, j = mid + 1, k = start; while (i <= mid && j <= end) {if (arr [i] .compareto (arr [j]) <0) {tmp [k ++] = arr [i ++]; } else {tmp [k ++] = arr [j ++]; }} while (i <= mid) {tmp [k ++] = arr [i ++]; } while (j <= end) {tmp [k ++] = arr [j ++]; } for (int m = start; m <= end; m ++) {arr [m] = tmp [m]; }} public static <ancytype расширяется сопоставимо <? Super AnyType >> void QuickSort (anyType [] arr) {QuickSort (arr, 0, arr.length - 1); } Private Static <AnyType расширяет сопоставимо <? Super Anytytype >> void QuickSort (anyType [] arr, int letfe, int rair) {if (левый + length_diff <= справа) {ancytype pivot = medium (arr, левый, справа); int i = слева, j = справа; while (true) {while (arr [++ i] .compareto (pivot) <0); while (arr [-j] .compareto (pivot)> 0); if (i <j) {Swaperferences (arr, i, j); } else {break; }} Swapreferences (arr, i, справа); Quicksort (arr, слева, я - 1); Quicksort (arr, i + 1, справа); } else {insertionSt (arr, left, справа); }} private static <ancytype расширяется сопоставимо <? Super AnyType >> AnyType Medium (AnyType [] arr, int left, int rair) {int center = (слева + справа) / 2; if (arr [center] .compareto (arr [left]) <0) {Swapreferences (arr, center, слева); } if (arr [left] .compareto (arr [right])> 0) {swapreferences (arr, ruge, raight); } if (arr [center] .compareto (arr [right]) <0) {Swapreferences (arr, center, справа); } return arr [справа]; } Частный окончательный статический int length_diff = 20;}Выше всего содержание этой статьи. Я надеюсь, что это будет полезно для каждого обучения, и я надеюсь, что все будут поддерживать Wulin.com больше.