insertion Sort Program in java
Code:
import java.util.*;
import java.lang.*;
import java.io.*;
//Insertion Sort
class Test{
public static void main(String[] args){
int[] arr = {7,2,4,1,5,3};
insertion(arr);
for(int i : arr){
System.out.print(i+" ");
}
}
public static void insertion(int[] arr){
for(int i=1;i<arr.length;i++){
int hole = i;
int val = arr[i];
while(hole>0 && arr[hole-1]>val){
arr[hole] = arr[hole-1];
hole--;
}
arr[hole] = val;
}
}
}
Comments
Post a Comment