?? 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