Friday, December 1, 2017

InsersionSort Java Program

public class InsersionSort {
    
    public static void main(String[] args) {
 int[] arr1 = {10,34,2,56,7,67,88,42};        
        int[] arr3 = doInsertionSort(arr1);
        for(int i:arr3){
            System.out.print(i);
            System.out.print(", ");
        }
    }

    private static int[] doInsertionSort(int[] arr1) {
 
 for(int i=0;i<arr1.length;i++){
     for(int j=0;j<arr1.length;j++){
  if(arr1[i]<arr1[j] && i!=j){
      int temp = arr1[j];
      arr1[j] = arr1[i];
      arr1[i] = temp;
  }
     }
 }
 return arr1;
    }   

}

No comments: