Java Edition sorting algorithm exercise summary

Source: Internet
Author: User

 Public classSorttest { Public Static voidMain (string[] args) {int[] arr = {23, 45, 12, 8, 3, 2, 55, 34, 87, 21}; //Buddlesort (arr); //Choicesort (arr); //Insertsort (arr);QuickSort (0, Arr.length-1, arr);    Printsort (arr); }        //Bubble Sort Stable     Public Static voidBuddlesort (int[] arr) {        inttemp;  for(inti = arr.length-1; i > 0; i--) {             for(intj = 0; J < I; J + +) {                if(Arr[j] > arr[j + 1]) {temp=Arr[j]; ARR[J]= arr[j+ 1]; Arr[j+1] =temp; }            }        }    }        //Choose an unstable sort     Public Static voidChoicesort (int[] arr) {         for(inti = 1; i < arr.length; i++) {             intK = 0, J;  for(j = 0; J <= Arr.length-i; j + +)) {                if(Arr[k] <Arr[j]) k=J; }            if(k! =j) {inttemp =Arr[k]; ARR[K]= Arr[j-1]; Arr[j-1] =temp; }        }    }       //Direct Insert Sort stable     Public Static voidInsertsort (int[] arr) {         for(inti = 1; i < arr.length; i++) {            inttemp =Arr[i], J;  for(j = i-1; J >= 0; j--) {                if(Arr[j] >temp) {Arr[j+1] =Arr[j]; } Else {                     Break; }} arr[j+1] =temp; }    }        //fast sequencing instability     Public Static voidQuickSort (intBeginintEndint[] arr) {        if(Begin <end) {            intMiddle =quickonce (begin, end, arr); QuickSort (begin, Middle-1, arr); QuickSort (Middle+ 1, end, arr); }    }        Public Static intQuickonce (intBeginintEndint[] arr) {        intFlag =Arr[begin];  while(Begin <end) {             while(Begin < End && Arr[end] >flag) {End--; }            if(Begin <end) {Arr[begin]=Arr[end]; }             while(Begin < End && Arr[begin] <flag) {Begin++; }            if(Begin <end) {Arr[end]=Arr[begin]; }} Arr[begin]=Flag; returnbegin; }        //Array Printing     Public Static voidPrintsort (int[] arr) {          for(inti = 0; i < arr.length; i++) {System.out.print (Arr[i]+ " ");    } System.out.println (); }}

Java Edition sorting algorithm exercise summary

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.