Java implements six common sorting algorithms

Source: Internet
Author: User

import Java.util.Random; Public classSort { PublicSort () {//TODO auto-generated Constructor stub    }    //bubble Sort, the sort of a total of n-1, the number of comparisons per trip is decreasing. Stable sequencing with O (n^2) complexity.     Public voidBubblesort (int[] a) {if(A = =NULL) {            return; }        intLen =a.length;  for(inti =0; I < a.length-1; i++)             for(intj =0; J < Len-i-1; J + +) {                if(A[j] > a[j +1]) Swrap (A, J, J+1); }    }    //Direct Insert Sort, the sort first to set a Sentinel, because in the sequencing of the forward comparison process will not cross, A<a, impossible to set up. Stable sequencing, time complexity O (n^2).     Public voidInsertsort (int[] a) {if(A = =NULL) {            return; }        intLen =a.length; int[] B =New int[Len +1]; System.arraycopy (A,0B1, Len);  for(inti =2; I <= Len; i++) {b[0] =B[i];  for(intj = i-1; J >0; j--) {                if(B[j] > b[0]) {b[j+1] = B[j];//element to move back oneB[J] = b[0]; }}} system.arraycopy (b,1A0, Len); }    //Shell sort, which is a sort of insertion order, except that the interval is no longer 1 but d. Unstable ordering, time complexity O (n^1.3)     Public voidShellsort (int[] a) {if(A = =NULL)            return; intLen =a.length; int[] B =New int[Len +1]; System.arraycopy (A,0B1, Len);  for(intD = len/2; D >=1; D/=2) {             for(inti = d +1; I <= Len; i++) {b[0] =B[i];  for(intj = i-d; J >0; J-=d) {if(B[j] > b[0]) {b[j+ d] =B[j]; B[J]= b[0]; }}}} system.arraycopy (b,1A0, Len); }    //Select the sorting, choose the smallest value to find the whole sequence of the array, as long as the n-1 times to select the OK. Stable sequencing, time complex pairs for O (n^2)     Public voidSelectsort (int[] a) {if(A = =NULL)            return; intLen =a.length;  for(inti =0; I < Len-1; i++) {            intmin = i;//initializes the subscript for the smallest element.              for(intj = i +1; J < Len; J + +) {                if(A[j] <A[min]) {min= J;//Select the subscript for the smallest element. } swrap (A, I, min); }        }    }    //quickly sort the result of a sort once, the value to the left of index is less than it, and the value on the right is greater than it.     Public intParition (int[] A,intStartintend) {Random Rand=NewRandom (); intIndex = start + rand.nextint (End-start);        Swrap (A, index, end); intsmall = Start-1;  for(index = start; index < end; index++) {            if(A[index] <A[end]) {Small++; if(Small! =index)                {Swrap (A, small, index); } }} Small++;        Swrap (A, small, end); returnSmall; }    //Quick Sort, determines the index value by sorting the sort of a trip quickly, and then sorts it recursively. Unstable sort. The time complexity is O (nlogn).     Public voidQuickSort (int[] A,intStartintend) {        if(A = =NULL|| Start <0|| End > A.length | | Start = =end)return; intindex =Parition (A, start, end); if(Index >start) QuickSort (A, start, index-1); if(Index <end) QuickSort (A, index+1, end); }    //heap Sort, select sort of one, first to build the heap, we take Dagen as an example, is the Dagen of the left and right child nodes are smaller than itself. Unstable sorting, time complexity O (NLOGN)     Public voidHeapsort (int[] a) {if(NULL!=a) {intEnd = A.length-1;  for(inti = (End-1) /2; I >=0; i--) Adjusthead (A, I, end);  for(inti = end; I >=0; i--) {Swrap (A,0, i); Adjusthead (A,0I1); }        }    }     Public voidAdjusthead (int[] A,intStartintend) {        if(A = =NULL|| Start <0|| End > A.length | | Start = =end)return; inttemp =A[start];  for(inti =2* Start +1; I <= end; i = i *2+1) {            if(I < end && A[i] < A[i +1]) I++; if((A[i) >temp)) {A[start]=A[i]; Start=i; } Else                 Break; } A[start]=temp; }    Private voidSwrap (int[] A,intJinti) {//TODO auto-generated Method Stub        inttemp =A[j]; A[J]=A[i]; A[i]=temp; }    Private voidPrintint[] a) { for(inttemp:a) System. out. print (temp +" "); System. out. println (); }     Public Static voidMain (string[] args) {int[] A = {3,5,7,2,1,8,4 }; Sort s=NewSort ();        S.bubblesort (a);        S.print (a);        S.insertsort (a);        S.print (a);        S.shellsort (a);        S.print (a);        S.selectsort (a);        S.print (a); S.quicksort (A,0, A.length-1);        S.print (a);        S.heapsort (a);    S.print (a); }}

Java implements six common sorting algorithms

Related Article

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.