5 Java Sort Algorithm Rollup tool class _java

Source: Internet
Author: User

The

Tool class summarizes the rapid ordering of Java in simple and straightforward way. Hill sort, insert sort, heap sort, merge sort five sort algorithms, the code does not have a description of these sorts of algorithms, about the thought part wants to check the relevant instructions, here is only a summary of these algorithms for everyone to use.

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;//record the position of the first bit of element that is smaller than TMP for T 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 = AR
    R[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; } privatestatic <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 mi
   D = (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)
 
   YType 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, right of int) {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; }

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.