ツールクラスは、Javaの5つのソートアルゴリズムの5つのソーティングアルゴリズムを簡単に明確に要約しています。クイックソート、ヒルソート、挿入並べ替え、ヒープソート、ソーティングのマージ。コードにこれらのソートアルゴリズムの説明はありません。アイデアの部分に関連する指示を自分で確認したいと思っています。ここでは、すべての人が使用できるこれらのアルゴリズムのみを要約します。
public class sort {public static <anytype extends carpleable <? Super AnyType >> void Insertionsort(anyType [] a){insertionsort(a、0、a.length -1); } private static <anytype extends carpleable <? Super AnyType >> void Insertionsort(anyType [] a、int left、int右){int j; // TMPよりも小さい最初の要素の次の桁の位置を(int p =左; p <=右; p ++){anytype 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 <anytype extends carpleable <? 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 leftchild(int i){return i * 2 + 1; } private static <anytype extends carpleable <? Super AnyType >> void Perculatedown(anyType [] arr、int i、int size){anytype tmp = arr [i]; for(int child;(child = leftchild(i))<size; i = child){if(child!= size -1 && arr [child] .compareto(arr [child+1])<0){child ++; } if(tmp.compareto(arr [child])<0){arr [i] = arr [child]; } else {break; }} arr [i] = tmp; } public static <anytype extends carpleable <? Super AnyType >> void Heapsort(anyType [] arr){for(int i = arr.length / 2; i> = 0; i-){perculatedown(arr、i、arr.length); } for(int i = arr.length-1; i> = 0; i--){swapReferences(arr、0、i); Perculatedown(arr、0、i); }} private static <anytype extends carpleable <? Super AnyType >> void swapReferences(anyType [] arr、int i、int j){anytype tmp = arr [i]; arr [i] = arr [j]; arr [j] = tmp; } public static <anytype extends carpleable <? Super AnyType >> void mergesort(anyType [] arr){anyType [] tmp =((anyType [])new Camparable [arr.length]); mergesort(arr、0、arr.length -1、tmp); } private static <anytype extends carpleable <? 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 <anytype extends carpleable <? 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 <anytype extends carpleable <? Super AnyType >> void QuickSort(anyType [] arr){QuickSort(arr、0、arr.length -1); } private static <anytype extends carpleable <? Super AnyType >> void QuickSort(AnyType [] arr、int left、int right){if(left + length_diff <=右){anytype pivot = medium(arr、左、右); int i =左、j =右; 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、右); QuickSort(arr、左、i -1); QuickSort(arr、i + 1、右); } else {insertionsort(arr、左、右); }} private static <anytype extends carpleable <? Super AnyType >> AnyType Medium(anyType [] arr、int left、int右){int center =(左 +右) / 2; if(arr [center] .compareto(arr [左])<0){swapReferences(arr、center、left); } if(arr [左] .compareto(arr [右])> 0){swapReferences(arr、左、右); } if(arr [center] .compareto(arr [right])<0){swapReferences(arr、center、right); } return arr [右]; } private final static int length_diff = 20;}上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。