The Java implementation of array sorting

Source: Internet
Author: User

Array Sort Summary

 Packagemain.java.cellar.test;/*** Created by Cui on 9/27/14.*/ Public classArraysorts {/**     * @paramargs*/     Public Static voidMain (string[] args) {int[] Number = {95, 45, 15, 44, 66, 88, 77 }; //bubblesort (number); //insertsort (number); //shellsort (number); //quickSort (number, 0, number.length-1);Sort (number, 0, number.length-1);  for(intz = 0; Z < number.length; z++) System.out.print (Number[z]+ " "); }    /*** Bubble Sort * *@param Number*/     Public Static voidBubblesort (int[] number) {        inttemp = 0;  for(inti = 0; i < number.length-1; i++) {             for(intj = 0; J < number.length-1-I; J + +) {                if(Number[j] > number[j + 1]) {temp=Number[j]; NUMBER[J]= Number[j + 1]; Number[j+ 1] =temp; } //If End            }        }    }    /*** Insert Sort * *@param Number*/     Public Static voidInsertsort (int[] number) {        intn =number.length; if(N < 2)            return; intI, j, K;  for(i = 1; i < n; i++) {            //find a suitable position for a[i] in the orderly interval of the front a[0...i-1]             for(j = i-1; J >= 0; j--)                //if the current value is greater than the sorted maximum value, the direct end                if(Number[j] <Number[i]) Break; //if we find a suitable location ,            if(J! = I-1) {                //move data that is larger than a[i] back                inttemp =Number[i];  for(k = i-1; k > J; k--) Number[k+ 1] =Number[k]; //put A[i] in the right placeNumber[k + 1] =temp; }        }    }    /*** Shell Sort * *@param Number*/     Public Static voidShellsort (int[] number) {        intn =number.length; if(N < 2)            return; intI, J, Gap;  for(Gap = N/2; gap > 0; Gap/= 2)            //Step Size             for(i = 0; I < gap; i++)//Direct Insert Sort            {                 for(j = i + Gap; j < n; j + =Gap) {                    if(Number[j] < number[j-Gap]) {                        inttemp =Number[j]; intK = J-Gap;  while(k >= 0 && Number[k] >temp) {Number[k+ Gap] =Number[k]; K-=Gap; } number[k+ Gap] =temp; }                }            }    }    /*** Quick Sort (Divide-and-conquermethod) c.r.a.hoare Year 1962 * *@paramS *@paramL *@paramR*/     Public Static voidQuickSort (intS[],intLintr) {if(L <r) {//swap this number and the first number in the middle            inti = l, j = r, x =S[l];  while(I <j) { while(I < J && S[j] >=x)//find the first number less than x from right to leftj--; if(I <j) S[i++] =S[j];  while(I < J && S[i] <x)//find the first number greater than or equal to x from left to righti++; if(I <j) S[j--] =S[i]; } S[i]=x; //divide and conquer around, recursive callQuickSort (S, L, i-1); QuickSort (S, I+ 1, R); }    }    /*** Part Code of Dualpivotquicksort * *@paramA *@paramLeft *@param Right*/     Public Static voidSortint[] A,intLeftintRight ) {         for(inti = left, j = i; I < right; j = + +i) {intAI = a[i + 1];  while(Ai <A[j]) {A[j+ 1] =A[j]; if(j--= =Left ) {                     Break; }} a[j+ 1] =AI; }    }}

The Java implementation of array sorting

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.