title: Inserting an element into an array in sorted order
body: <p>I'm trying to add an element into an array in sorted order.</p>
  
  <p>This is my code :</p>
  
  <pre><code>public class SortedInsertion {
  
      public static void main(String[] args) {
  
          int[] arr=new int[6];
          arr[0]=5;
          arr[1]=6;
          arr[2]=9;
          arr[3]=11;
          System.out.println(Arrays.toString(arr));
          insert(7,arr);
  
  
      }
  
      public static void insert(int val,int[] arr){
          int i;
          for(i=0;i&lt;arr.length-1;i++){
  
              if(arr[i]&gt;val)
                  break;
          }
          for(int k=i;k&lt;arr.length-1;k++){
              arr[k+1]=arr[k];
              arr[i]=val;
          }
          System.out.println(Arrays.toString(arr));
  
  
      }
  
  }
  </code></pre>
  
  <p>I'm getting the output as:
  [5, 6, 9, 11, 0, 0]</p>
  
  <p>[5, 6, 7, 9, 9, 9]</p>
  
  <p>But the right out put is</p>
  
  <p>5,6,9,11,0,0</p>
  
  <p>5,6,7,9,11,0</p>