Die Werkzeugklasse fasst kurz und klar fünf Sortieralgorithmen von Java zusammen: Schnellsortierung, Hügelsortierung, Insertionssortierung, Haufensortierung und Zusammenführungssortierung. Es gibt keine Erklärung für diese Sortieralgorithmen im Code. Ich hoffe, die entsprechenden Anweisungen für den Gedankenteil alleine zu überprüfen. Hier werde ich diese Algorithmen nur für alle zusammenfassen.
öffentliche Klasse Sortierung {public static <Anytype erweitert vergleichbar <? Super Anytype >> void Insertionsort (Anytype [] a) {Insertionsort (a, 0, A.Length - 1); } private static <Anytype erweitert vergleichbar <? Super Anytype >> void Insertionsort (Anytype [] a, int links, int rechts) {int j; // Aufzeichnen Sie die Position der folgenden Ziffer des ersten Elements, die kleiner als tmp für (int p = links; p <= rechts; p ++) {AnyType tmp = a [p]; für (j = p; j> links && tmp.comPareto (a [j - 1]) <0; j--) {a [j] = a [j - 1]; } a [j] = tmp; }} public static <Anytype erweitert vergleichbar <? Super Anytype >> void ShellSort (Anytype [] arr) {int j; für (int gap = arr.length / 2; gap> 0; gap / = 2) {für (int i = gap; i <arr.length; i ++) {AnyType tmp = arr [i]; für (j = i; j> = gap && tmp.comPareto (arr [j - gap]) <0; j - = gap) {arr [j] = arr [j - gap]; } arr [j] = tmp; }}} private statische int links (int i) {return i * 2 + 1; } private static <Anytype erweitert vergleichbar <? Super Anytype >> void perculedown (Anytype [] arr, int i, int size) {AnyType tmp = arr [i]; für (int child; (child = linkChild (i)) <Größe; i = child) {if (child! } if (tmp.comPareto (arr [child]) <0) {arr [i] = arr [child]; } else {break; }} arr [i] = tmp; } public static <Anytype erweitert vergleichbar <? Super Anytype >> void Heapsort (Anytype [] arr) {for (int i = arr.length / 2; i> = 0; i--) {perculedown (arr, i, arr.length); } für (int i = arr.length-1; i> = 0; i--) {swapReferences (arr, 0, i); perculedown (arr, 0, i); }} private static <Anytype erweitert vergleichbar <? Super Anytype >> void swapReferences (Anytype [] arr, int i, int j) {Anytype tmp = arr [i]; arr [i] = arr [j]; arr [j] = tmp; } public static <Anytype erweitert vergleichbar <? Super Anytype >> void mergesort (Anytype [] arr) {AnyType [] tmp = ((AnyType []) Neu vergleichbar [arr.length]); mergesort (arr, 0, arr.length - 1, tmp); } private static <Anytype erweitert vergleichbar <? 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, Ende, TMP); merge (arr, start, mid, enden, tmp); }} private static <Anytype erweitert vergleichbar <? 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 ++]; } für (int m = start; m <= end; m ++) {arr [m] = tmp [m]; }} public static <Anytype erweitert vergleichbar <? Super Anytype >> void QuickSort (Anytype [] arr) {schnaszsort (arr, 0, arr.length - 1); } private static <Anytype erweitert vergleichbar <? Super Anytype >> void QuickSort (Anytype [] arr, int links, int rechts) {if (links + length_diff <= rechts) {Anytype pivot = medium (arr, links, rechts); int i = links, j = rechts; while (true) {while (arr [++ i] .Compareto (pivot) <0); while (arr [-j] .Compareto (pivot)> 0); if (i <j) {SwapReferences (arr, i, j); } else {break; }} SwapReferences (arr, i, rechts); Quicksort (arr, links, i - 1); QuickSort (arr, i + 1, rechts); } else {Insertionsort (arr, links, rechts); }} private static <Anytype erweitert vergleichbar <? Super Anytype >> AnyType Medium (Anytype [] arr, int links, int rechts) {int Center = (links + rechts) / 2; if (arr [center] .Compareto (arr [links]) <0) {SwapReferences (arr, Mitte, links); } if (arr [links] .Compareto (arr [rechts])> 0) {SwapReferences (arr, links, rechts); } if (arr [center] .Compareto (arr [rechts]) <0) {swapReferences (arr, center, rechts); } return arr [rechts]; } private endgültige statische int länge_diff = 20;}Das obige ist der gesamte Inhalt dieses Artikels. Ich hoffe, es wird für das Lernen aller hilfreich sein und ich hoffe, jeder wird Wulin.com mehr unterstützen.