Seven ways to sort code in Java

Source: Internet
Author: User

 PackageBasejava;/*** @title Sortmethods.java *@authorDonsenchen * @Date May 2, 2018 Morning 10:16:03 * @Description*/ Public classSortmethods { Public Static voidMain (string[] args) {int[] arr = {3, 7, 9, 1, 4, 8, 2, 6, 5 };        Binarysort (arr);        Bubblesort (arr); QuickSort (arr,0, 0);        Selectsort (arr);        Insertsort (arr);        Shellsort (arr); MergeSort (arr,0, 0); }    //1. Two-way sorting     Public Static voidBinarysort (int[] numbers) {        intI, J, temp; intLow , hight, mid;  for(i = 1; i < numbers.length; i++) {Temp=Numbers[i]; Low= 0; Hight= I-1;  while(Low <=hight) {Mid= (low + hight)/2; if(Numbers[mid] >temp) hight= Mid-1; Else Low= Mid + 1; }             for(j = i-1; j > hight; j--) Numbers[j+ 1] =Numbers[j]; Numbers[hight+ 1] =temp; } System.out.print ("The dichotomy of the order:");        Printarr (numbers);    System.out.println (); }    //2. Bubble sort     Public Static voidBubblesort (int[] numbers) {        intTemp//Record interim intermediate values        intsize = Numbers.length;//Array Size         for(inti = 0; i < size-1; i++) {             for(intj = i + 1; J < size; J + +) {                if(Numbers[i] > Numbers[j]) {//Exchange Two number position (PS: Here the greater than number less than the number determines the ascending or descending order of the array)temp =Numbers[i]; Numbers[i]=Numbers[j]; NUMBERS[J]=temp; }}} System.out.print ("Bubble Sort:");        Printarr (numbers);    System.out.println (); }    //3. Quick Sort     Public Static voidQuickSort (int[] numbers,intStartintend) {        if(Start <end) {            intbase = Numbers[start];//Selected Datum value (first value as Datum)            intTemp//Record interim intermediate values            inti = start, j =end;  Do {                 while((Numbers[i] < base) && (I <end)) I++;  while((Numbers[j] > Base) && (J >start)) J--; if(I <=j) {Temp=Numbers[i]; Numbers[i]=Numbers[j]; NUMBERS[J]=temp; I++; J--; }            }  while(I <=j); if(Start <j) QuickSort (Numbers, start, j); if(End >i) QuickSort (numbers, I, end); } System.out.print ("Quick sort:");        Printarr (numbers);    System.out.println (); }    //4. Select Sort     Public Static voidSelectsort (int[] numbers) {        intSize =Numbers.length, temp;  for(inti = 0; i < size; i++) {            intK =i;  for(intj = size-1; J > i; j--) {                if(Numbers[j] <Numbers[k]) k=J; } temp=Numbers[i]; Numbers[i]=Numbers[k]; NUMBERS[K]=temp; } System.out.print ("Select Sort:");        Printarr (numbers);    System.out.println (); }    //5. Insert Sort     Public Static voidInsertsort (int[] numbers) {        intSize =Numbers.length, temp, J;  for(inti = 1; i < size; i++) {Temp=Numbers[i];  for(j = i; j > 0 && Temp < numbers[j-1]; j--) Numbers[j]= Numbers[j-1]; NUMBERS[J]=temp; } System.out.print ("Insert Sort:");        Printarr (numbers);    System.out.println (); }    //6. Hill Sort     Public Static voidShellsort (int[] numbers) {        intn =numbers.length; //to group, the first increment (GAP) is half the length of the array         for(intGap = N/2; Gap > 0; Gap/= 2) {            //inserting sorting on individual groupings             for(inti = gap; I < n; i++) {                //Insert art[i] into the correct position of the group in which you are locatedInserti (numbers, Gap, I); }} System.out.print ("Hill Sort:");        Printarr (numbers);    System.out.println (); }    //Hill Sort Insert Method//Insert art[i] into the correct position of the group in which you are located    Private Static voidInserti (int[] arr,intGapinti) {intinserted =Arr[i]; intJ;  for(j = i-gap; J >= 0 && inserted < arr[j]; J-=Gap) {Arr[j+ Gap] =Arr[j]; } arr[j+ Gap] =inserted; }    //7. Merge sort     Public Static voidMergeSort (int[] numbers,intLeftintRight ) {        intt = 1;//number of elements per group        intSize = Right-left + 1;  while(T <size) {            ints = t;//number of elements per group in this cyclet = 2 *s; inti =Left ;  while(i + (t-1) <size) {Merge (numbers, I, I+ (s-1), i + (t-1)); I+=T; }            if(i + (s-1) <Right ) Merge (numbers, I, I+ (s-1), right); } System.out.print ("Merge sort:");        Printarr (numbers);    System.out.println (); }    //Merging algorithm Implementation    Private Static voidMergeint[] Data,intPintQintr) {int[] B =New int[Data.length]; ints =p; intt = q + 1; intK =p;  while(s <= q && t <=r) {if(Data[s] <=Data[t]) {B[k]=Data[s]; S++; } Else{B[k]=Data[t]; T++; } k++; }        if(s = = q + 1) B[k+ +] = data[t++]; Elseb[k+ +] = data[s++];  for(inti = P; I <= R; i++) Data[i]=B[i]; }    //Print Array    Private Static voidPrintarr (int[] arr) {System.out.print ("[");  for(inti = 0; i < arr.length; i++) {            if(i = = Arr.length-1) {System.out.print (Arr[i]+ "]"); } Else{System.out.print (Arr[i]+ ", "); }        }    }}

***********************

Comments: Welcome to correct, study together.

***********************

Seven ways to sort code in Java

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.