Java uses the binary insertion sort to be similar to the direct insertion sort speed

Source: Internet
Author: User

?? Java uses the binary insertion sort to be similar to the direct insertion sort speed


Previously tested python uses a binary insertion sort to be 99 times times faster than a direct insert sort!

Now test the Java,linux test results as follows:

Javac Test.java

Java test
Insertsort Total milliseconds:15769
Insertsortwithbinaryserach Total milliseconds:15657


The procedure is as follows:

Import Java.util.date;public class test{public static void Main (String []args) {Date D1 = new Date ();       Int[] A = new int[200000];       for (int i=0;i<200000;i++) {a[i]=100-i;       } insertsort (a);       Date D2 = new Date ();  System.out.println ("Insertsort total milliseconds:" + (D2.gettime ()-d1.gettime ())); Printing the elements//for (int i=0;i<a.length;i++) {//system.out.println (i+ ":" +a[i]);       } Date D3 = new Date ();       int[] A2 = new int[200000];       for (int i=0;i<200000;i++) {a2[i]=100-i;       } insertsortwithbinaryserach (A2);       Date D4 = new Date ();  System.out.println ("Insertsortwithbinaryserach total milliseconds:" + (D4.gettime ()-d3.gettime ())); Printing the elements//for (int j=0;j<a2.length;j++) {//system.out.println (j+ ":" +a2[j]);       }} public static void Insertsort (int[] A) {for (int i = 1; i < a.length; i++) {int value = A[i];       int j = i-1; while (j >= 0 && A[j] > value) {a[j + 1] = A[j];       j = j-1;     } a[j + 1] = value;     }} public static void Insertsortwithbinaryserach (int[] A) {for (int i=1;i<a.length;i++) {int key=a[i]; int Pos=binarysearch (A,0,i-1,key); Finds where the element is stored for (int. j=i;j>pos;j--)//shifts The other elements by 1 position to the RI     Ght A[j]=a[j-1]; A[pos]=key;  Places the key element in the POS position}}//uses binary search technique to find the position where the element     would be inserted public static int BinarySearch (int[] a,int low,int high,int key) {int mid;         while (Low<=high) {mid= (Low+high)/2;         if (Key>a[mid]) low=mid+1;         else if (Key<a[mid]) high=mid-1;    else return mid; } return low;  }}
is there a small difference between the two algorithms running in the JVM?


Java uses the binary insertion sort to be similar to the direct insertion sort speed

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.