Java uses binary insertion sort unexpectedly and direct insertion sort speed comparison __java

Source: Internet
Author: User
 Java uses binary insertion sort unexpectedly and direct insertion sort speed comparison


Previously tested Python is 99 times times faster than a direct insertion sort, using a binary insertion sort.

Now test the Java,linux test results are as follows:

Javac Test.java

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

Correct, the above data is tested on a virtual machine, so the sample is not enough, in theory, the speed of these two algorithms should be a multiple difference.

Thanks for the blank corrections of the writing memory, again thanks to the comments of the friends.

Here is the blank test result of the writing memory:

Insertsort Total milliseconds:41496
Insertsortwithbinaryserach Total milliseconds:10166


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 would be stored for (int j=i;j>pos;j--)//shifts The other elements from 1 position to the R
     Ight 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 Elem
     ENT is inserted public static int BinarySearch (int[] a,int low,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 to Low;  }
}


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.