package in.malliktalksjava;
/**
* @author malliktalksjava
* This class sort an array using Insertion sort algorithm.
*
*/
public class InsertionSortExample {/**
* @param args
*/
public static void main(String args[]){
int[] arr1 = {10,34,2,56,7,67,88,42};
int[] arr2 = doInsertionSort(arr1);
for(int counts:arr2){
System.out.print(counts);
System.out.print(“, “);
}
}/**
* @param input
* @return
*/
/**
* @param input
* @return
*/
public static int[] doInsertionSort(int[] input){int temp;
for (int count = 1; count < input.length; count++) {
for(int count2 = count ; count2 > 0 ; count2–){
if(input[count2] < input[count2-1]){
temp = input[count2];
input[count2] = input[count2-1];
input[count2-1] = temp;
}
}
}
return input;
}
}
Other Useful Links:
Selection Sort Example Program in JAVA
4 thoughts on “Insertion Sort Example program in JAVA”