La classe d'outils résume brièvement et clairement cinq algorithmes de tri de Java: tri rapide, tri des collines, tri d'insertion, tri de tas et tri de fusion. Il n'y a aucune explication à ces algorithmes de tri dans le code. J'espère vérifier moi-même les instructions pertinentes pour la partie de l'idée. Ici, je ne résumerai que ces algorithmes pour que tout le monde puisse utiliser.
classe publique Sort {public static <anyType étend comparable <? Super AnyType >> void insertionsort (anyType [] a) {insertionsort (a, 0, a.length - 1); } Private Static <AnyType étend comparable <? Super anyType >> void insertionsort (anyType [] a, int Left, int droit) {int j; // Enregistrez la position du chiffre suivant du premier élément plus petit que TMP pour (int p = gauche; p <= droite; p ++) {AnyType tmp = a [p]; pour (j = p; j> Left && tmp ....pareto (a [j - 1]) <0; j--) {a [j] = a [j - 1]; } a [j] = tmp; }} public static <anyType étend comparable <? superyType >> void shellSort (anyType [] arr) {int j; for (int gp = arr.length / 2; gap> 0; gap / = 2) {for (int i = gap; i <arr.length; i ++) {anyType tmp = arr [i]; pour (j = i; j> = gap && tmp ....pareto (arr [j - gap]) <0; j - = gap) {arr [j] = arr [j - gap]; } arr [j] = tmp; }}} private static int Leftchild (int i) {return i * 2 + 1; } Private Static <AnyType étend comparable <? superyType >> void perculedown (anyType [] arr, int i, int size) {anyType tmp = arr [i]; pour (int enfant; (enfant = LeftChild (i)) <size; i = enfant) {if (enfant! = size - 1 && arr [child] .compareto (arr [child + 1]) <0) {enfant ++; } if (tmp ....pareto (arr [child]) <0) {arr [i] = arr [enfant]; } else {break; }} arr [i] = tmp; } public static <anyType étend comparable <? Super AnyType >> void heapsort (anyType [] arr) {for (int i = arr.length / 2; i> = 0; i--) {perculewown (arr, i, arr.length); } pour (int i = arr.length - 1; i> = 0; i--) {swapReferences (arr, 0, i); Perculedown (arr, 0, i); }} Private Static <AnyType étend comparable <? superype >> void swapreferences (anyType [] arr, int i, int j) {anyType tmp = arr [i]; arr [i] = arr [j]; arr [j] = tmp; } public static <anyType étend comparable <? super anyType >> void Mergesort (anyType [] arr) {anyType [] tmp = ((anyType []) new comparable [arr.length]); Mergesort (arr, 0, arr.length - 1, tmp); } Private Static <AnyType étend comparable <? 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); Merge (Arr, Start, Mid, End, TMP); }} Private Static <AnyType étend comparable <? 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 ++]; } pour (int m = start; m <= end; m ++) {arr [m] = tmp [m]; }} public static <anyType étend comparable <? superyType >> void Quicksort (anyType [] arr) {Quicksort (arr, 0, arr.length - 1); } Private Static <AnyType étend comparable <? super anyType >> void Quicksort (anyType [] arr, int Left, int droit) {if (gauche + longueur_diff <= droit) {anyType pivot = medium (arr, gauche, droite); int i = gauche, j = droit; 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, à droite); Quicksort (arr, à gauche, i - 1); Quicksort (arr, i + 1, à droite); } else {insertionsort (arr, gauche, droite); }} Private Static <AnyType étend comparable <? Super AnyType >> AnyType Medium (anyType [] arr, int Left, int droit) {int Centre = (gauche + droit) / 2; if (arr [centre] .compareto (arr [gauche]) <0) {swapreferences (arr, centre, gauche); } if (arr [gauche] .compareto (arr [droit])> 0) {swapReferences (arr, gauche, droite); } if (arr [Center] .Compareto (arr [à droite]) <0) {swapReferences (arr, centre, droit); } return arr [à droite]; } private final static int long_diff = 20;}Ce qui précède est tout le contenu de cet article. J'espère que cela sera utile à l'apprentissage de tous et j'espère que tout le monde soutiendra davantage Wulin.com.