The Java Ultimate Fast sorting method for sorting algorithm

Source: Internet
Author: User

Package Net.qh.test.sort;import java.util.arraylist;import Java.util.calendar;import java.util.list;/** * Created by Administrator on 2016/03/01.  */public class Termsimplequick {public int[] sort (int[] arr,int left,int right) {if (arr = = NULL | | arr.length        <= 1) {return arr;                } if (Right-left >= 9) {while (Right-left >= 9) {int i = left;                int j = right;                int mid = (left + right)/2;                    if (Arr[i] > Arr[mid]) {int temp = Arr[i];                    Arr[i] = Arr[mid];                Arr[mid] = temp;                    } if (Arr[i] > Arr[j]) {int temp = Arr[i];                    Arr[i] = Arr[j];                ARR[J] = temp;                    } if (Arr[mid] > Arr[j]) {int temp = Arr[mid];                    Arr[mid] = Arr[j];                ARR[J] = temp;       }         int temp = Arr[mid];                Arr[mid] = arr[right-1];                ARR[RIGHT-1] = temp;                int k = i;                int L = right-1;                int m = k;                int n = l-1;                            while (true) {while (M < L) {if (Arr[m] > Arr[l]) {                        Break                    } m++;                        } while (K < n) {if (Arr[n] < arr[l]) {break;                    } n--;                    } if (M >= n) break;                    int tem = ARR[M];                    ARR[M] = Arr[n];                Arr[n] = tem;                } int tem = Arr[l];                ARR[L] = arr[m];                ARR[M] = tem;                    if (L-m > M-k) {sort (arr,m + 1, right); right =M-1;                    }else{sort (arr,left,m-1);                left = m + 1;            }}}} else {int i = left + 1;            int j = right;                for (; I <= J; i++) {int k = i-1;                int temp = Arr[i];                    while (true) {if (Arr[k] > Arr[i]) {arr[k + 1] = arr[k];                    }else{break;                    } Arr[k] = temp;                k--;    }}} return arr;        } public static void Main (string[] args) {list<integer> intlist = new arraylist<integer> ();        for (int i = 0; i < 1000000; i++) {intlist.add (1000000-i);        } int[] arr = new int[intlist.size ()];        for (int j = 0; J < Arr.length; J + +) {Arr[j] = Intlist.get (j); }//for (int temp:arr) {//SyStem.out.print (temp + ",");/} System.out.println ("");        Long start = Calendar.getinstance (). Gettimeinmillis ();        arr = new Termsimplequick (). sort (arr,0,arr.length-1);        Long end = Calendar.getinstance (). Gettimeinmillis ();        System.out.println ("Required milliseconds:" + (End-start));//for (int Temp:arr) {//System.out.print (temp + ",");// }}/** 1.         The quick Sort method is the ultimate version of the Quick sort.         2. Remove bottom recursion: Small partitions use recursion, large partitions use loops.         3. The selection of benchmarks is no longer blind: Arr[left] <= Arr[mid] <= Arr[right], interchange, Arr[mid] and Arr[right], the baseline is: Arr[right]. 4. Real-Combat testing: 4-core CPU and 8G of memory, 100,000 integer elements of the array ordering, reverse order requires 17 milliseconds, ordered: 28 milliseconds 4. Combat test: 4-core CPU and 8G of memory, 1 million integer elements of the array sort, reverse order requires 55 milliseconds, ordered required: 57 milliseconds */}

  

The Java Ultimate Fast sorting method for sorting algorithm

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.