Insertion Sort Example program in JAVA


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

Bubble Sort Example in JAVA

Quick Sort Example Program in Java

Merge Sort Example in Java

4 thoughts on “Insertion Sort Example program in JAVA

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.