Q:

Java Program to implement Insertion Sorting

0

Program to implement Insertion Sorting with predefined values.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

public class demo1 
{  
 public static void insertionSort(int array[]) 
 {  
  int arrSize = array.length;  
  for (int b = 1; b< arrSize; b++) 
  {  
   int key = array[b];  
   int a = b-1;  
   while ( (a > -1) && ( array [a] > key ) ) 
   {  
    array [a+1] = array [a];  
    a--;  
   }  
    array[a+1] = key;  
  }  
 }  
 public static void main(String arr[])
 {    
  int[] array1 = {45,32,43,2,4,4,53};    
  System.out.println("Before Insertion Sort");    
  for(int a:array1)
  {    
   System.out.print(a+" ");    
  }    
   System.out.println();                
   insertionSort(array1);             
   System.out.println("After Insertion Sort");    
   for(int a:array1){    
   System.out.print(a+" ");    
  }    
 }    
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now