Insertion sort iterates, consuming one input element each
repetition, and grows a sorted output list. At each iteration, insertion sort
removes one element from the input data, finds the location it belongs within
the sorted list, and inserts it there. It repeats until no input elements
remain
java
package
com.kartik.sorting; |
The code you've provided is an implementation of the
Insertion Sort algorithm in Java. Insertion Sort is a simple and intuitive
sorting algorithm, which is efficient for small or nearly sorted datasets.
Explanation of the Code:
- Insertion
Sort Algorithm:
- Insertion
sort builds the sorted array one item at a time. It takes each element
from the unsorted portion of the array and inserts it into its correct
position in the sorted portion.
- The
algorithm works similarly to how you might sort playing cards in your
hands.
- doInsertionSort
Method:
- Parameters:
Takes an integer array input as input.
- Logic:
- The
outer loop iterates through each element in the array starting from the
second element (index 1).
- The
inner loop compares the current element with elements in the sorted
portion (on the left side of the current element) and swaps them if they
are in the wrong order.
- After
each insertion, the current state of the array is printed to the
console.
- printNumbers
Method:
- A
utility method to print the current state of the array to the console.
- main
Method:
- Creates
an array arr1 and calls the doInsertionSort method to sort the array.
- The
array is printed after each insertion to show the sorting process step by
step.
Example Output:
When you run the code, it will print the array before
sorting, after each insertion, and the final sorted array.
plaintext
Before
Insertion Sorting --->> 10, 34, 2,
56, 7, 67, 88, 42, After
Insertion Sorting start--->> 10, 34, 2,
56, 7, 67, 88, 42, 10, 2, 34,
56, 7, 67, 88, 42, 2, 10, 34,
56, 7, 67, 88, 42, 2, 10, 34,
56, 7, 67, 88, 42, 2, 7, 10, 34,
56, 67, 88, 42, 2, 7, 10, 34,
56, 67, 88, 42, 2, 7, 10, 34,
56, 67, 42, 88, 2, 7, 10, 34,
56, 42, 67, 88, |
Explanation of the Output:
- Initial
Array: The state of the array before sorting starts.
- Sorting
Steps: After each insertion, the array is printed to show how elements
are being moved to their correct positions.
- Final
Sorted Array: After all elements have been inserted in the correct
positions, the array is fully sorted.
Key Points:
- Time
Complexity: The time complexity of insertion sort is O(n^2) in the worst case, where n is the number of elements in the array. This
happens when the array is sorted in reverse order. However, its average
and best-case time complexity is O(n), making it efficient for
small or nearly sorted datasets.
- Space
Complexity: The space complexity is O(1), as the sorting is
done in-place without requiring additional memory.
- Stability:
Insertion sort is a stable sorting algorithm, meaning that the relative
order of equal elements is preserved.
- Adaptive:
Insertion sort is adaptive, which means it performs better (almost linear
time) for data that is already sorted or nearly sorted.
Potential Enhancements:
- Generic
Implementation: Modify the implementation to work with generic types
instead of just integers.
- Optimizations:
Although insertion sort is simple, it could be optimized slightly by
reducing the number of swaps.
- Visualization:
Integrate a visual representation of the sorting process to better
understand how the algorithm works.
This code provides a clear and straightforward
implementation of insertion sort, allowing you to observe each step of the
sorting process through printed output in the console.
0 Comments