-
Notifications
You must be signed in to change notification settings - Fork 215
/
Copy pathinsertion_sort.java
47 lines (36 loc) · 1.76 KB
/
insertion_sort.java
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
/*
In this implementation, we define a function called InsertionSort that takes an array of integers and sorts
it in ascending order using the Insertion Sort algorithm.
The algorithm works by iterating over the array from the second element to the end.
For each element, it compares it with the previous elements in the array and inserts it in the correct position.
The current variable holds the value of the current element being compared.
The j variable holds the index of the previous element being compared.
The loop compares the current value with the previous values in the array and shifts the values to the right to make space for the current value.
Once the correct position is found, the current value is inserted into the array.
Finally, the sorted array is returned. In the main function, we define an array of integers, sort it using the InsertionSort function, and print the sorted array.
Sample input: [0, 2, 1,-1, 10, 3, 4]
Output: [-1 0 1 2 3 4 10]
*/
// InsertionSort is a function that takes an array of integers and sorts it in
// ascending order using the Insertion Sort algorithm.
public class InsertionSort {
public static 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) {
int[] arr = { 12, 11, 13, 5, 6 };
insertionSort(arr);
System.out.println(Arrays.toString(arr));
}
}