Java implementation of eight sorting algorithms

Source: Internet
Author: User

JDK7 's Collections.sort () algorithm is timsort, adaptive merge sort, more obscure, not realized here

public class Mysort {//bubble sort public static void Mybubblesort (int[] array) {int lastexchange = Array.len    Gth-1;                Record the last interchange position to avoid repeating comparisons for (int i = lastExchange-1; I >= 0; i) {for (int j = 0; J <= i; ++j) {                    if (Array[j] > array[j + 1]) {int temp = array[j];                    ARRAY[J] = array[j + 1];                    Array[j + 1] = temp;   Lastexchange = j; Attribute: The element after the last interaction position has been ordered}}}}//insert sort public static void Myinsertsort (int[            ] array) {for (int i = 1; I <= array.length-1; ++i) {int temp = Array[i]; int j = 0; Shift the value and look for the insertion point for (j = i-1; J >= 0 && array[j] > temp;--j) {array[j + 1] = arr            AY[J];        } array[j + 1] = temp; }}//select sort public static void Myselectsort (int[] array) {for (int i = 0; i < Array.leNgth-1;            ++i) {int minindex = i; Select an extremum for (int j = i + 1; j <= array.length-1; ++j) {if (Array[j] < Array[minindex) at each time                ]) {minindex = J;                }}//Extremum return if (minindex! = i) {int temp = Array[minindex];                Array[minindex] = Array[i];            Array[i] = temp;        }}}//hill sort public static void Myshellsort (int[] array) {int gap = 5; while (gap! = 0) {//Do not have to be deliberately grouped, group 1-> Group 2-> Group 1-> Group 2 ...                Take turns processing for (int j = gap; J <= Array.length-1; ++j) {int temp = array[j];                int k = 0;                for (k = j-gap; k >= 0 && array[k] > temp; k-= Gap) {array[k + gap] = array[k];            } Array[k + gap] = temp;   } Gap/= 2; Re-grouping}}//Quick Sort Public static void Myquicksort (int[] array) {Myquicksortcore (array, 0, array.length-1);            } private static void Myquicksortcore (int[] array, int left, int. right) {if (left >= right) {//Recursive exit        Return        } int mleft = left;                int mright = right;  int base = Array[mleft]; The first element as a datum, the left vacancy can occupy while (mleft! = mright) {while (Mright > Mleft && array[mright] >= b            ASE) {--mright;  } Array[mleft] = Array[mright];            Right can overwrite while (Mright > Mleft && array[mleft] <= base) {++mleft;        } Array[mright] = Array[mleft]; } Array[mright] = base;  Datum element homing, L=r Myquicksortcore (array, left, mLeft-1);  Recursive datum with left Myquicksortcore (array, mright + 1, right);  Recursive datum with Right}//merge sort public static void Mymergesort (int[] array) {//each grouping has two ordered groups from the upper iteration, Gap is an ordered group length, 2 * gThe AP is a packet length for (int gap = 1; gap < Array.Length; gap *= 2) {int i = 0;//array subscript//grouping and internal sorting                 while (i + 2 * gap-1 < Array.Length) {mergepiece (array, I, i + gap-1, i + 2 * gap-1);            i + = 2 * gap; }//Grouping the remainder of the order, only more than one gap has the meaning of the internal sort if (i + gap-1 < Array.Length) {mergepiece (arr            Ay, I, i + gap-1, array.length-1); }}}//combine the ordered two segments of PIECEWISE1 and Piecewise2 in an array into a whole ordered public static void Mergepiece (int[] array, int head, int mid, int tail) {int i = head;//Piecewise1 subscript [head, mid] int J = mid + 1;//PIECEWISE2 Subscript [mid + 1, TA IL]//temporary array, save result int[] Arraytemp = new Int[tail-head + 1]; Combine int k = 0;                Combine subscript while (I <= mid && J <= tail) {if (Array[i] <= array[j]) {                ARRAYTEMP[K] = Array[i];                ++i;       ++k;     } else {arraytemp[k] = array[j];                ++j;            ++k;            }}//Copy extra part piecewise1 while (I <= mid) {arraytemp[k] = Array[i];            ++i;        ++k;            }//Copy redundant Parts Piecewise2 while (J <= tail) {arraytemp[k] = array[j];            ++j;        ++k;        }//The result is copied to the original array k = 0; i = head;            Reset subscript, I piecewise1 + piecewise2 while (i <= tail) {Array[i] = arraytemp[k];            ++i;        ++k; }}//heap sort public static void Myheapsort (int[] array) {//Adjustment heap--Large top heap for (int i = Array.lengt H/2-1; I >= 0;        -i) {//starting from the last non-leaf node adjustheap (array, I, array.length);            }//Adjustment heap--swap heap top/Bottom element for (int j = array.length-1; j > 0;--j) {int temp = array[0];            Array[0] = Array[j];            ARRAY[J] = temp; Adjustheap (Array, 0, J); Simply adjust the heap Top parent Node}}//adjust to a large top heap distribution, node is the parent node subscript, Adjustlen for the length of the adjustment (used for sorting) private static void Adjustheap (int[] ar Ray, int node, int adjustlen) {int temp = Array[node];//Take out node formation can occupy space for (int i = node * 2 + 1; i < ad Justlen; i = node * 2 + 1) {if (i + 1 < Adjustlen && Array[i] < Array[i + 1]) {++i;//Get                Maximum child node} if (Array[i] > Temp) {Array[node] = Array[i]; node = i;            Update the parent node for the next iteration, and finally the leaf} else {break;    }} Array[node] = temp;        }//radix sort public static void Myradixsort (int[] array) {int d = maxbit (array); int dec = 1;  Binary Iteration Final int R = 10;  Number of barrels int[] Temparray = new Int[array.length];  Temporary array, instead of bucket storage array, the cost is to record subscript/quantity to split bucket int[] bucketcapacity = new Int[r];  Bucket count for (int i = 1, i <= D; ++i) {for (int j = 0; j < R; ++j) {              BUCKETCAPACITY[J] = 0; Empty bucket capacity}//Count 1 for (int j = 0; j < Array.Length; ++j) {int k = array[                  J]/Dec% R;            ++BUCKETCAPACITY[K]; }//Count 2 variable cumulative, for split for (int j = 1; j < R; ++j) {Bucketcapacity[j] = bucketcapacity[            J-1] + bucketcapacity[j]; }//stored in bucket for (int j = array.length-1; J >= 0;--j) {int k = array[j]/Dec% R                 ;                TEMPARRAY[BUCKETCAPACITY[K]-1] = array[j];              --BUCKETCAPACITY[K];            }//write out for (int j = 0; j < Array.Length; ++j) {array[j] = temparray[j];        }//Next Dec *= 10;        }}//To find the maximum number of digits of an array element private static int maxbit (int[] array) {int bit = 1;        int dec = 10;  for (int i = 0, i < array.length; ++i) {while (Array[i] >= Dec) {              ++bit;            Dec *= 10;    }} return bit; }}

Java implementation of eight sorting algorithms

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.