程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> 5種java排序算法匯總對象類

5種java排序算法匯總對象類

編輯:關於JAVA

5種java排序算法匯總對象類。本站提示廣大學習愛好者:(5種java排序算法匯總對象類)文章只能為提供參考,不一定能成為您想要的結果。以下是5種java排序算法匯總對象類正文


對象類簡略清楚明了地總結了java的疾速排序,希爾排序,拔出排序,堆排序,合並排序五種排序算法,代碼中並沒有對這幾種排序算法的一個解釋,關於思惟部門願望在自行查閱相干解釋,這裡只是對這幾種算法停止一個歸納綜合,以供年夜家應用。

public class Sort {
 public static <AnyType extends Comparable<? super AnyType>> void insertionSort(AnyType[] a) {
  insertionSort(a, 0, a.length - 1);
 }
 
 private static <AnyType extends Comparable<? super AnyType>> void insertionSort(AnyType[] a, int left, int right) {
  int j; // 記載第一個比tmp小的元素的後邊一名的地位
 
  for (int p = left; p <= right; 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 Comparable<? 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 Comparable<? 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 Comparable<? 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 Comparable<? 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 Comparable<? super AnyType>> void mergeSort(AnyType[] arr) {
  AnyType[] tmp = ((AnyType[]) new Comparable[arr.length]);
  mergeSort(arr, 0, arr.length - 1, tmp);
 }
 
 private static <AnyType extends 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 extends 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++];
  }
 
  for (int m = start; m <= end; m++) {
   arr[m] = tmp[m];
  }
 }
 
 public static <AnyType extends Comparable<? super AnyType>> void quickSort(AnyType[] arr) {
  quickSort(arr, 0, arr.length - 1);
 }
 
 private static <AnyType extends Comparable<? super AnyType>> void quickSort(AnyType[] arr, int left, int right) {
  if (left + LENGTH_DIFF <= right) {
 
   AnyType pivot = medium(arr, left, right);
 
   int i = left, j = right;
   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, right);
   quickSort(arr, left, i - 1);
   quickSort(arr, i + 1, right);
  } else {
   insertionSort(arr, left, right);
  }
 }
 
 private static <AnyType extends Comparable<? super AnyType>> AnyType medium(AnyType[] arr, int left,
   int right) {
  int center = (left + right) / 2;
  if (arr[center].compareTo(arr[left]) < 0) {
   swapReferences(arr, center, left);
  }
  if (arr[left].compareTo(arr[right]) > 0) {
   swapReferences(arr, left, right);
  }
  if (arr[center].compareTo(arr[right]) < 0) {
   swapReferences(arr, center, right);
  }
   
  return arr[right];
 }
 
 private final static int LENGTH_DIFF = 20;
}

以上就是本文的全體內容,願望對年夜家的進修有所贊助,也願望年夜家多多支撐。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved