Hey there!
Welcome to ClearUrDoubt.com.
In this post, we will look at a Core Java program to sort the elements of an array.
We will use the Insertion Sort technique to sort the array elements. Insertion Sort is good for small datasets.
Insertion sort iterates, consuming one input element each repetition and growing 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.
Let’s look at an example flow for {5, 12, 92, 76, 1, 58, 10, 2, 5}:
Outer Loop iteration | Inner Loop iteration | Array | current | Action to be done | ||||||||||
1 | 0 | 5 | 12 | 92 | 76 | 1 | 58 | 10 | 2 | 5 | 12 | no need to move | ||
2 | 0 | 5 | 12 | 92 | 76 | 1 | 58 | 10 | 2 | 5 | 92 | no need to move | ||
3 | 1 | 5 | 12 | 92 | 76 | 1 | 58 | 10 | 2 | 5 | 76 | Move 92 to next position | ||
5 | 12 | 76 | 92 | 1 | 58 | 10 | 2 | 5 | 76 | Insert 72 in previous position | ||||
4 | 1 | 5 | 12 | 76 | 92 | 1 | 58 | 10 | 2 | 5 | 1 | Move 92 to next position | ||
2 | 5 | 12 | 76 | 92 | 58 | 10 | 2 | 5 | 1 | Move 76 to next position | ||||
3 | 5 | 12 | 76 | 92 | 58 | 10 | 2 | 5 | 1 | Move 12 to next position | ||||
4 | 5 | 12 | 76 | 92 | 58 | 10 | 2 | 5 | 1 | Move 5 to next position | ||||
1 | 5 | 12 | 76 | 92 | 58 | 10 | 2 | 5 | 1 | Insert 1 in previous position | ||||
5 | 1 | 1 | 5 | 12 | 76 | 92 | 58 | 10 | 2 | 5 | 58 | Move 92 to next position | ||
2 | 1 | 5 | 12 | 76 | 92 | 10 | 2 | 5 | 58 | Move 76 to next position | ||||
3 | 1 | 5 | 12 | 58 | 76 | 92 | 10 | 2 | 5 | 58 | Insert 58 in previous position | |||
6 | 1 | 1 | 5 | 12 | 58 | 76 | 92 | 10 | 2 | 5 | 10 | Move 92 to next position | ||
2 | 1 | 5 | 12 | 58 | 76 | 92 | 2 | 5 | 10 | Move 76 to next position | ||||
3 | 1 | 5 | 12 | 58 | 76 | 92 | 2 | 5 | 10 | Move 58 to next position | ||||
4 | 1 | 5 | 12 | 58 | 76 | 92 | 2 | 5 | 10 | Move 12 to next position | ||||
1 | 5 | 10 | 12 | 58 | 76 | 92 | 2 | 5 | 10 | Insert 10 in previous position | ||||
7 | 1 | 1 | 5 | 10 | 12 | 58 | 76 | 92 | 2 | 5 | 2 | Move 92 to next position | ||
2 | 1 | 5 | 10 | 12 | 58 | 76 | 92 | 5 | 2 | Move 76 to next position | ||||
3 | 1 | 5 | 10 | 12 | 58 | 76 | 92 | 5 | 2 | Move 58 to next position | ||||
4 | 1 | 5 | 10 | 12 | 58 | 76 | 92 | 5 | 2 | Move 12 to next position | ||||
5 | 1 | 5 | 10 | 12 | 58 | 76 | 92 | 5 | 2 | Move 10 to next position | ||||
6 | 1 | 5 | 10 | 12 | 58 | 76 | 92 | 5 | 2 | Move 5 to next position | ||||
1 | 2 | 5 | 10 | 12 | 58 | 76 | 92 | 5 | 2 | Insert 2 in previous position | ||||
8 | 1 | 1 | 2 | 5 | 10 | 12 | 58 | 76 | 92 | 5 | 5 | Move 92 to next position | ||
2 | 1 | 2 | 5 | 10 | 12 | 58 | 76 | 92 | 5 | Move 76 to next position | ||||
3 | 1 | 2 | 5 | 10 | 12 | 58 | 76 | 92 | 5 | Move 58 to next position | ||||
4 | 1 | 2 | 5 | 10 | 12 | 58 | 76 | 92 | 5 | Move 12 to next position | ||||
5 | 1 | 2 | 5 | 10 | 12 | 58 | 76 | 92 | 5 | Move 10 to next position | ||||
1 | 2 | 5 | 5 | 10 | 12 | 58 | 76 | 92 | 5 | Insert 5 to next position | ||||
We can observe that after each outer loop iteration, the array is sorted till the outer loop index. The inner loop is used for moving the elements to next positions.
Here is the program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
package com.clearurdoubt; import java.util.Arrays; public class ArraySorter { public static void main(String[] args) { int[] input = {5, 12, 92, 76, 1, 58, 10, 2, 5}; // before sorting System.out.println("Before sorting: "); System.out.println(Arrays.toString(input)); input = sortArray(input); // after sorting System.out.println(); System.out.println("After sorting: "); System.out.println(Arrays.toString(input)); } /** * Method to sort the elements of the array using Insertion sort * @param input array of elements * @return sorted array */ public static int[] sortArray(int[] input) { /* * Check for null and size of the array * proceed only if array size is greater than 1 */ if(input != null && input.length > 1) { int currentValue = 0; // Outer loop to go through each element in the array for(int i = 1; i < input.length; i++) { currentValue = input[i]; // Take the backup of current value int j = i; /* * Starting from the current position move backwards * if input[j-1] is greater than currentValue, input[j-1] should move to next position */ while(j > 0 && input[j-1] > currentValue) { input[j] = input[j-1]; // move input[j-1] to next position j--; // decrement j to move backward } /* * we found the correct position(j) for currentValue * insert currentValue at input[j] */ input[j] = currentValue; } } return input; // return the sorted elements } } |
Output:
Happy Learning :).
Please leave a reply in case of any queries.