# Insertion Sort Java Algorithm – 2 Ways | Java Sortings

Implement Insertion Sort Algorithm In Java – We will discuss the various methods to Implement Insertion Sort Algorithm In Java. The compiler has been added so that you can execute the programs easily, alongside suitable examples and sample outputs. The methods are – Also check Heap Sort In Java

• Algorithm – Using Array.
• Algorithm – Using Buffered Reader.

## Insertion Sort Java – Using Array

1) We are using an array for insertion sort.

2) The print method will print the array elements, the sort method will sort the array elements.

3) The elements in the array are 9, 5, 0, 1, 8.

For 1st iteration, the inner loop compares the number with the previous number, if the previous number is greater than this number then shift the least number to left.

For i=1,  inner loop compares the numbers 5<9, then 5 will be shifted to left. Then the series is 5, 9, 0, 1, 8. In this sorted subarray is 5,9.

For i=2, the inner loop compares the numbers 0<9, shift 0 to left, compare 0<5, shift 0 to left. Then the sorted subarray is 0, 5, 9. The series is 0, 5, 9, 1, 8.

For i=3, the inner loop will compare the numbers 1<9, shift 1 to left, compare 1<5, shift 1 to left, compare 1,0. The sorted subarray is 0, 1, 5, 9 and the series is 0, 1, 5, 9, 8.

For i=4, the inner loop will compare the numbers 8<9, shift 8 to left. The sorted series is 0, 1, 5, 8, 9.

Output:

### Insertion Sort Using Buffered Reader

1) Buffered Reader reads the data from the character-based input stream. In this program, InputStreamReader is the character based input stream.

2) Using readLine() method read the data, parseInt() method converts the data into an integer value and store the numbers into the array a[].

The given series is 10, 2, 1, 56, 48, 11.

3) For each iteration of i, the inner loop will generate a sub-sorted array in which the least number is shifted to left by comparing with previous numbers.

4) 1st iteration of i, compare 10,2 and 2 shifted to left. Then the sorted subarray is 2, 10.

• 2nd iteration of i, compare 10,1 and shift 1 to left.compare 2,1 and shift 1 to left, then the sorted subarray is 1, 2, 10.
• 3rd iteration of i, compare 56 with the previous numbers of the sorted subarray, then the sorted subarray is 1, 2, 10, 56.
• 4th iteration of i, compare 48 with previous numbers of the sorted subarray, then the subarray is 1, 2, 10, 48, 56.
• 5th iteration of i, compare 11 with the previous numbers of the subarray, then the sorted array is 1, 2, 10, 11, 48, 56.

Output:

If you have any queries or suggestion related to Implementation of Insertion Sort Java Algorithm program & Tutorial do leave a comment here.

x

## X Star Pattern Java Program – Patterns

Java program to print X star pattern program – We have written the below print/draw ...