title: Where is my error in my binary insertion sort algorithm?
body: <p>I have tried to implement binary insertion sort algorithm.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>public class binary_insertion {
      public static void sort(int a[],int n){
          for (int i=0;i&lt;n;++i){
              int temp=a[i];
              int left=0;
              int right=i;
              while (left&lt;right){
                  int middle=(left+right)/2;
                  if (temp&gt;=a[middle])
                      left=right+1;
                  else
                      right=middle;
              }
              for (int j=i;j&gt;left;--j){
                  swap(a,j-1,j);
              }
          }
      }
  
      public static void main(String[] args){
          int a[]=new int[]{12,10,34,23,9,7,8,5,6};
          sort(a,a.length);
          for (int i=0;i&lt;a.length;i++){
              System.out.println(a[i]);
          }
      }
      public static void swap(int a[],int i,int j){
          int k=a[i];
          a[i]=a[j];
          a[j]=k;
      }
  }
  </code></pre>
  
  <p>and my result is wrong:</p>
  
  <pre><code>5
  7
  6
  9
  8
  10
  12
  34
  23
  </code></pre>
  
  <p>What did I do wrong?</p>