Four kinds of common sort

Source: Internet
Author: User
Tags stub
One, select the sort
Package testcode;

public class Run {public
    static void Main (string[] args) {  
    int[] arr = {5,4,8,9};
    Selectsort (arr);
    for (int i=0;i<arr.length;i++) {
        System.out.print (arr[i]+ "");
    }
    }

    private static void Selectsort (int[] arr) {
        //TODO auto-generated method stub
        int n = arr.length;
        int tmp =0;
        int flag = 0;
        for (int i=0;i<n;i++) {
            tmp = arr[i];
            Flag =i;
            for (int j=i+1;j<n;j++) {
                if (arr[j]<tmp) {
                    tmp = arr[j];
                    Flag = j;
                }
            }
            if (flag!=i) {
                Arr[flag] = arr[i];
                Arr[i] = tmp;}}}  

Results:

Second, insert sort
Package testcode;

public class Run {public
    static void Main (string[] args) {  
    int[] arr = {5,4,8,9};
    Insertsort (arr);
    for (int i=0;i<arr.length;i++) {
        System.out.print (arr[i]+ "");
    }
    }

    private static void Insertsort (int[] arr) {
        //TODO auto-generated method stub
        int temp=0;
        Int J =0;
        if (arr!=null) {for
            (int i=1;i<arr.length;i++) {
                temp =arr[i];
                j=i;
                if (arr[j-1]>temp) {while
                    (j>=1&&arr[j-1]>temp) {
                        arr[j] = arr[j-1];
                        j--
                    }
                }
                ARR[J] = temp;}}}  

Run Result:

Note: A For loop +while loop while (j>=1&&arr[j-1]>temp) termination condition

Two variables save Arr[i] and subscript information Three, merge sort MergeSort

Package testcode;
    public class Run {public static void main (string[] args) {int[] arr = {5,4,8,9};
    MergeSort (arr,0,arr.length-1);
    for (int i=0;i<arr.length;i++) {System.out.print (arr[i]+ "");
        } private static void MergeSort (int[] arr,int Start,int end) {//TODO auto-generated method stub
            if (start<end) {int mid = (start+end) >>1;
            MergeSort (Arr,start,mid);
            MergeSort (Arr,mid+1,end);
        Merge (Arr,start,mid,end); }} private static void merge (int[] arr, int left,int mid, int right) {//TODO auto-generated Method St
        UB int i = left;
        int j = mid+1;
        int k=0;
        int[] tmp = new Int[arr.length]; while (i<=mid&&j<=right) {//Termination condition with equal sign, closed interval if (Arr[i]<=arr[j]) {tmp[k++] = arr[i++]
            ;
            } else{tmp[k++] = arr[j++]; }} WHILe (i<=mid) {tmp[k++] = arr[i++];
        while (J<=right) {tmp[k++] = arr[j++]; for (int m = 0;m<k;m++) {Arr[left+m] = tmp[m];//starting position is left+m}}}

Run Result:

Three while (..) Cycle Four, fast row

Package testcode;

public class Run {public
    static void Main (string[] args) {  
    int[] arr = {5,4,8,9};
    QuickSort (arr,0,arr.length-1);
    for (int i=0;i<arr.length;i++) {
        System.out.print (arr[i]+ "");
    }
    }

    private static void QuickSort (int[] arr,int Start,int end) {
        //TODO auto-generated method stub
        int i=start;
  
   int J =end;
        int index = arr[i];
        if (i>=j) {return
            ;
        }
        while (I<J) {while
            (i<j && arr[j]>=index) {
                j--;
            }
            if (i<j) {
                arr[i++] = arr[j];
            }
            while (I<j && arr[i]<index) {
                i++;
            }
            if (i<j) {
                arr[j--] = Arr[i];
            }
        }
        Arr[i] = index;
        QuickSort (arr,start,i);
        QuickSort (arr,i+1,end);
    }


  

Run Result:

4 5 8 9 

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.