Java implementation of sorting algorithm

Source: Internet
Author: User

Bubbles, the choice will not write. Very common

One: Insert sort:

/*** Insert Sort*/ Public classP4_3 {Static voidInsertsort (int[] a) {        intj,t; /**         *         */         for(inti = 0; i < a.length; i++) {T=A[i]; J=i-1;  while(j>=0&&t<A[j]) {A[j+1]=A[j]; J--; } a[j+1]=T; }    }     Public Static voidMain (string[] args) {int[] A=New int[]{3,4,2,6,2,5,1,9,28};        Insertsort (a);  for(inti = 0; i < a.length; i++) {System.out.println (a[i]); }    }}

Two: Shell sort

/*** Shell Sort*/ Public classP4_4 {Static voidShellsort (int[] a) {         for(intR = A.LENGTH/2; R >=1; r/=2) {             for(inti = r; i < a.length; i++) {                intt=A[i]; intj=i-R;  while(j>=0&&t<A[j]) {A[j+r]=A[j]; J=j-R; } a[j+r]=T; }        }    }     Public Static voidMain (string[] args) {int[] A=New int[]{3,4,2,6,25,1,9,28};        Shellsort (a);  for(inti = 0; i < a.length; i++) {System.out.println (a[i]); }    }}

Three: Quick-line

/*** Qicksort*/ Public classP4_5 {Static voidQicksort (int[] A,intLeftintRight ) {        intf,t; intrtemp,ltemp; Rtemp=Right ; Ltemp=Left ; F=a[(right+left)/2];  while(ltemp<rtemp) {             while(a[ltemp]<f) {Ltemp++; }             while(a[rtemp]>f) {Rtemp--; }            if(ltemp<rtemp) {                //will the left greater than the demarcation worth the number of exchanges to zip less than the demarcation worthy positiont=A[rtemp]; A[rtemp]=A[ltemp]; A[ltemp]=T; Ltemp++; Rtemp--; }        }        if(ltemp==rtemp) {Ltemp++; }        if(right>ltemp) {Qicksort (a,rtemp+1, right); }        if(rtemp>Left ) {Qicksort (a,left,ltemp-1); }    }     Public Static voidMain (string[] args) {int[] A=New int[]{3,4,2,6,25,1,9,73}; Qicksort (A,0,a.length-1);  for(inti = 0; i < a.length; i++) {System.out.println (a[i]); }    }}

Four: Heap Sort

/*** Heapsort*/ Public classP4_6 {Static voidHeapsort (int[] A,intN) {        inti,j,h,k; intT; //Build Heap         for(i = n/2-1; I >=0; i--) {//find the parent node of the current last node//There is a right sub-tree             while(2*i+1<N) {J=2*i+1; if(j+1<N) {                    if(a[j]<a[j+1]) {J++; }                }                //Place the largest position on the node                if(a[i]<A[j]) {T=A[i]; A[i]=A[j]; A[J]=T; I=J; }Else {                     Break; }            }        }        //Output heap:System.out.println ("Heap of output:");  for(intL = 0; L < A.length; l++) {System.out.print (a[l]); System.out.print (" ");        System.out.println (); }        //Find the maximum         for(i = n-1; I >=0; i--) {            //put the found data on the far rightT=a[0]; a[0]=A[i]; A[i]=T; K=0; //K There is a subtree             while((2*k+1) <i) {J=2*k+1; if((j+1) <i) {                    if(a[j]<a[j+1]) {J++; }                }                if(a[k]<A[j]) {T=A[k]; A[K]=A[j]; A[J]=T; K=J; }Else {                     Break; }            }        }    }     Public Static voidMain (string[] args) {int[] A=New int[]{1,3,5,7,9,2,4,6,8,0};        Heapsort (a,a.length);  for(intL = 0; L < A.length; l++) {System.out.print (a[l]); System.out.print (" "); }    }}

V: Merge sort

/*** MergeSort*/ Public classp4_7 {Static voidMergeone (int[] A,int[] B,intNintLen) {        inti,j,k,s,e; S= 0;//Start Position         while((S+len) <N) {            //End PositionE=s+2*len-1; if(e>N) {e=n-1; }            //Mergingk=s; I=s; J=s+Len;  while(i< (S+len) &&j<=e) {                if(a[i]<A[j]) {B[k++]=a[i++]; }Else{b[k++]=a[j++]; }            }            //Copy an array that is not merged             while(i< (s+len)) {B[k++]=a[i++]; }             while(j<=e) {B[k++]=a[j++]; } s=e+1;//where to start next        }        //No comparison of the merger came over        if(s<N) {             for(; s<n;s++) {B[s]=A[s]; }        }    }    Static voidMergeSort (int[] A,intN) {        int[] p=New int[n]; intF,len; Len=1; F=0;  while(len<N) {            if(f==1) {mergeone (P,a,n,len); }Else{mergeone (A,p,n,len); } Len=2*Len; F=1-F; }        if(f==1){             for(inth = 0; h < n; h++) {A[h]=P[h]; }        }    }     Public Static voidMain (string[] args) {int[] A=New int[]{3,4,2,6,25,1,9,92,19};        MergeSort (a,a.length);  for(inti = 0; i < a.length; i++) {System.out.println (a[i]); }    }}

Java implementation of sorting algorithm

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.