Introduction
Insertion sort is the best sorting algorithm for small-sized list of elements as compared to selection sort and bubble sort. This sorting algorithm always maintains a sorted sublist within an iteration i.e. it finds the correct position of a single element at a time and inserts it in its correct position within the currently sorted sublist.
Worst Complexity : O(n*n)
Best Complexity : O(n)
Average Complexity: O(n*n)
Imperative style using Java 7
Declarative style using Java8
Input : 0,5,2,6,3,1,-9
Output: -9 0 1 2 3 5 6 (Ascending)
Output: 6 5 3 2 1 0 -9 (Descending)
Conclusion
In this post, we learned the implementation of insertion sort in Java language. Please leave your comments in the comments box. Thank you.
Insertion sort is the best sorting algorithm for small-sized list of elements as compared to selection sort and bubble sort. This sorting algorithm always maintains a sorted sublist within an iteration i.e. it finds the correct position of a single element at a time and inserts it in its correct position within the currently sorted sublist.
Worst Complexity : O(n*n)
Best Complexity : O(n)
Average Complexity: O(n*n)
Imperative style using Java 7
public class InsertionSortExample {
private static int array[] = {0,5,2,6,3,1,-9};
public static void main(String[] args) {
insertionSortAscending();
System.out.println("Ascending Sorted List");
for(int value : array) {
System.out.print(value + " ");
}
insertionSortDescending();
System.out.println("\nDescending Sorted List");
for(int value : array) {
System.out.print(value + " ");
}
}
private static void insertionSortAscending() {
//first element is considered as sorted sublist
for(int outer=1; outer<array.length; outer++) {
int temp = array[outer];
int inner = outer-1;
// array[inner] > temp is for ascending order
while(inner >= 0 && array[inner] > temp) {
array[inner+1] = array[inner];
inner--;
}
array[inner+1] = temp;
}
}
private static void insertionSortDescending() {
for(int outer=1; outer<array.length; outer++) {
int temp = array[outer];
int inner = outer-1;
// array[inner] < temp is for ascending order
while(inner >= 0 && array[inner] < temp) {
array[inner+1] = array[inner];
inner--;
}
array[inner+1] = temp;
}
}
}
import java.util.Arrays;
import java.util.stream.IntStream;
public class InsertionSortJava8Example {
private static int array[] = {0,5,2,6,3,1,-9};
static int temp = 0;
public static void main(String[] args) {
insertionSortAscending();
System.out.println("Ascending Sorted List");
Arrays.stream(array).forEach(System.out::print);
insertionSortDescending();
System.out.println();
System.out.println("Descending Sorted List");
Arrays.stream(array).forEach(System.out::print);
}
private static void insertionSortAscending() {
IntStream.range(1, array.length)
.forEach(outer -> {
int temp = array[outer];
int inner = outer-1;
// array[inner] > temp is for ascending order
while(inner >= 0 && array[inner] > temp) {
array[inner+1] = array[inner];
inner--;
}
array[inner+1] = temp;
});
}
private static void insertionSortDescending() {
IntStream.range(1, array.length)
.forEach(outer -> {
int temp = array[outer];
int inner = outer-1;
// array[inner] < temp is for ascending order
while(inner >= 0 && array[inner] < temp) {
array[inner+1] = array[inner];
inner--;
}
array[inner+1] = temp;
});
}
}
Output: -9 0 1 2 3 5 6 (Ascending)
Output: 6 5 3 2 1 0 -9 (Descending)
Conclusion
In this post, we learned the implementation of insertion sort in Java language. Please leave your comments in the comments box. Thank you.
Comments
Post a Comment