-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsertion.java
More file actions
30 lines (27 loc) · 924 Bytes
/
insertion.java
File metadata and controls
30 lines (27 loc) · 924 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
//Insertion sort
public class insertion {
public static void printArray(int arr[]) {
for(int i=0; i<arr.length; i++) {
System.out.print(arr[i]+" ");
}
System.out.println();
}
public static void main(String args[]) {
int arr[] = {7, 8, 1, 3, 2};
//time complexity = O(n^2)
//outter loop to count n-1 iterations where arr.length represents n
for(int i=1; i<arr.length; i++) {
int current = arr[i];
//inner loop, we use j until its greater than 0 to track the sorted part
int j = i - 1;
while(j >= 0 && arr[j] > current) {
//Keep swapping
arr[j+1] = arr[j];
j--;
}
//placement of the element
arr[j+1] = current;
}
printArray(arr);
}
}