📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (176K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
What is Insertion Sort Algorithm?
Advantages
- Efficient for small data
- Adaptive: If the input list is presorted [may not be complete] then insertions sort takes O(n + d), where d is the number of inversions
- Practically more efficient than selection and bubble sorts, even though all of them have O(n2) worst case complexity
- Stable: Maintains relative order of input data if the keys are same
- In-place: It requires only a constant amount O(1) of additional memory space
- Online: Insertion sort can sort the list as it receives it
How Insertion Sort Works?
12, 11, 13, 5, 6
11, 12, 13, 5, 6
11, 12, 13, 5, 6
5, 11, 12, 13, 6
5, 6, 11, 12, 13
Insertion Sort Implementation in Java
import java.util.Arrays;
/**
* Class demonstrate the Insertion sort algorithm
* @author Ramesh Fadatare
*
*/
public class InsertionSort {
/* Function to sort array using insertion sort */
private void insertionSort(int arr[]) {
int n = arr.length;
for (int i = 1; i < n; ++i) {
int key = arr[i];
int j = i - 1;
/*
* Move elements of arr[0..i-1], that are greater than key, to one
* position ahead of their current position
*/
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
public static void main(String args[]) {
InsertionSort insertionSort = new InsertionSort();
int arr[] = { 20, 10, 5, 6, 2, 3, 4 };
System.out.println("Before Sorting an array : " + Arrays.toString(arr));
insertionSort.insertionSort(arr);
System.out.println("After Sorting an array : " + Arrays.toString(arr));
}
}
Before Sorting an array : [20, 10, 5, 6, 2, 3, 4]
After Sorting an array : [2, 3, 4, 5, 6, 10, 20]
Generic Implementation
import java.util.Arrays;
/**
* Insertion sort is a simple sorting algorithm: a comparison sort in which the
* sorted array (or list) is built one entry at a time. It is much less
* efficient on large lists than more advanced algorithms such as quicksort,
* heapsort, or merge sort.
*
* @author Ramesh Fadatare
*/
public class InsertionSort<T extends Comparable<T>> {
private InsertionSort() {
}
public static <T extends Comparable<T>> T[] sort(T[] unsorted) {
int length = unsorted.length;
for (int i = 1; i < length; i++) {
sort(i, unsorted);
}
return unsorted;
}
private static <T extends Comparable<T>> void sort(int i, T[] unsorted) {
for (int j = i; j > 0; j--) {
T jthElement = unsorted[j];
T jMinusOneElement = unsorted[j - 1];
if (jthElement.compareTo(jMinusOneElement) < 0) {
unsorted[j - 1] = jthElement;
unsorted[j] = jMinusOneElement;
} else {
break;
}
}
}
public static void main(String[] args) {
Integer arr[] = { 20, 10, 5, 6, 2, 3, 4 };
System.out.println("--------Sorting integer array using insertion sort algorithm--------");
System.out.println();
System.out.println("Before Sorting an array : " + Arrays.toString(arr));
InsertionSort.sort(arr);
System.out.println("After Sorting an array : " + Arrays.toString(arr));
System.out.println();
System.out.println("--------Sorting String array using insertion sort algorithm--------");
System.out.println();
String[] stringArray = { "c", "a", "e", "b", "d" };
System.out.println("Before sorting the array elements : " + Arrays.toString(stringArray));
InsertionSort.sort(stringArray);
System.out.println("After sorting the array elements : " + Arrays.toString(stringArray));
}
}
--------Sorting integer array using insertion sort algorithm--------
Before Sorting an array : [20, 10, 5, 6, 2, 3, 4]
After Sorting an array : [2, 3, 4, 5, 6, 10, 20]
--------Sorting String array using insertion sort algorithm--------
Before sorting the array elements : [c, a, e, b, d]
After sorting the array elements : [a, b, c, d, e]
Performance
- Worst case complexity: Θ(n2)
- Best case complexity: Θ(n)
- Average case complexity: Θ(n2)
- Worst case space complexity: O(n2) total, O(1) auxiliary
Comments
Post a Comment
Leave Comment