-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathInsertionSort.java
More file actions
41 lines (29 loc) · 915 Bytes
/
InsertionSort.java
File metadata and controls
41 lines (29 loc) · 915 Bytes
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
public class InsertionSort {
void insertion_sort(int arr[], int n) {
for (int i = 1; i < n; i++) {
int value = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > value) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = value;
}
System.out.println("The sorted array:");
for (int i = 0; i < n; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
public static void main(String args[]) {
int arr[] = {15, 25, 30, 17, 9, 5, 20, 10, 11, 6};
int n = arr.length;
System.out.println("The original array:");
for (int i = 0; i < n; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
InsertionSort is = new InsertionSort();
is.insertion_sort(arr, n);
}
}