Monday, 15 October 2012

insertion sorting



public class insertionSorting {

 /**
  * @param args
  */
 
 
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  insertionSorting test = new insertionSorting();
 }
 
 public insertionSorting(){
  int array[] = {2,5,1,9,12,3,6,7,11,11,11,8,21,17,45,46,33,22};
  sorting(array);
  print(array);
 }
 
 public void sorting(int[] buffer){
  int key,i;
  for(int j = 1;j<buffer.length;j++)
  {
   key = buffer[j];
   i = j-1;
   while((i>=0) && buffer[i] > key){
    buffer[i+1] = buffer[i];
    i = i-1;
   }
   buffer[i+1] = key;
  }
 }
 public void print(int[] buffer){
  for(int i = 0;i<buffer.length;i++)
   System.out.print(buffer[i]+", ");
 }
 
}

No comments:

Post a Comment