Java implementations of several sorting methods (01: Insert Sort, bubble sort, select sort, quick sort)

Source: Internet
Author: User

The following is a centralized sort of Java code implementation (partially referencing someone else's code): Insert Sort (insertsort):
//Code Principle Public Static voidIsort (int[] a) {     for(inti = 1;i < a.length; i++){        if(A[i] < a[i-1]){            inttemp =A[i];  while(Temp < A[i-1]) {A[i]= A[i-1]; if(I-1 > 0) {i--; }Else{                     Break; }} a[i-1] =temp; }    }}//Thin Code 1 Public Static voidISort2 (int[] a) {         for(inti = 1;i < a.length; i++){            if(A[i] < a[i-1]){                inttemp =A[i];  while(Temp < A[i-1]) {A[i]= A[i-1]; if(I-1 > 0) {i--; }Else{                         Break; }} a[i-1] =temp; }        }    }//Thin Code 2 for(current=1;current<=arr.length-1;current++){    //whenever current changes, key and before changeKey =Arr[current]; Before= Current-1;//The first value of the red ordered index.          while(Before>=0 && arr[before]>key) {Arr[before+1] = Arr[before];//large values move backward onebefore--; }        //insertion point: before+1ARR[BEFORE+1] =key;}

Bubble sort (bubbletsort):
 Public classBubblesort { Public Static int[] Bsort (int[] a) {         for(intj = a.length-1; J >= 1; j--){//If flag does not turn false then it proves that the array itself is ordered            BooleanFlag =true;  for(inti = 0; I <= j-1; i++){                if(A[i] > a[i+1]){                    inttemp =A[i]; A[i+1] = a[1]; A[i]=temp; Flag=false; }            }            if(flag) Break; }        returnA; }}

Select Sort (selectionsort):
/** Select the basic idea of sorting: * Compare the first element in turn and all the elements behind it. * At the end of the first time, there will be a minimum value appearing at the front. * In turn*/ Public classSelectionsort { Public Static voidSortint[] data) {         for(intx = 0; x < data.length-1; X + +) {             for(inty = x + 1; Y < data.length; y++) {                if(Data[y] <Data[x])                {Sorttest.swap (data, x, y); }            }        }    }}

Select Sort (QuickSort):
/*** Quick Sort *@authorMly11 **/ Public classQuickSort {Static intLow = 0; Static intHigh = 0; Static intMID = 0;  Public Static voidMain (string[] args) {int[] arr = {5,1,3,9,2,7,2,4}; //The starting array isSystem.out.println ("The Starting array is:");  for(inti = 0; i < arr.length; i++) {System.out.print (Arr[i]+ "\ T"); } System.out.println ();//Sortjudge (arr); //sort after traversalSystem.out.println ("Sorted array is:");  for(inti = 0; i < arr.length; i++) {System.out.print (Arr[i]+ "\ T");    } System.out.println (); }    //determine if the array is empty     Public Static voidJudgeint[] arr) {        if(Arr.length > 0) {All (arr,0,arr.length-1); }Else{System.out.println ("Change it.");; }    }    //loop condition, recursive call loop body     Public Static voidAllint[] arr,intLowintHigh ) {        if(Low <High ) {//use mid to record the corner mark of each selected dividing number,MID =structure (arr, low, high); //when Low < mid-1, recursion from low to mid-1            if(Low < Mid-1) {All (arr, Low, mid-1); }            //when high>mid+1, from mid+1 to high recursion            if(High > Mid +1) {All (arr, mid+1, high); }        }    }    //loop body One cycle     Public Static intStructureint[] arr,intLowintHigh ) {//when index low is less than high, perform the operation so that the selected value is less than the left, otherwise on the right/*principle: Remove A[low] data into temp, so that a[low] is empty; start loop: When Low < High, always loop while (low < high) { When low starts from the first, it looks from the back forward one by one, if it is larger than the temp, then high--; otherwise swaps a[low] and A[high], at which time the value of A[low] is A[high],a[high] is empty; now in the past (a[l OW] (A[high] occupied position), if smaller than temp, then low++, otherwise a[low] position in A[high] (the previous step is empty A[high]), at this time A[low] is empty;} A[low]        = temp; Returns low, at which point the position of the data before all <a[low],low is all >a[low];*/                inttemp =Arr[low];  while(Low <High ) {             while(Low < High && Arr[high] >=temp) { High--; } Arr[low]=Arr[high];  while(Low < High && Arr[low] <temp) { Low++; } Arr[high]=Arr[low]; } Arr[low]=temp; returnLow ; /*wrong method while (low < high) {int temp = Arr[low];            while (Arr[low] <= Arr[high]) {high--;            } Arr[low] = Arr[high];            low++;            Arr[high] = Arr[low];        Arr[low] = temp; } return low;*/    }    }

The above code for oneself in the first contact at the time of writing, the shortcomings please understand.

Java implementations of several sorting methods (01: Insert Sort, bubble sort, select sort, quick sort)

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.