-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlab3.java
More file actions
28 lines (23 loc) · 756 Bytes
/
lab3.java
File metadata and controls
28 lines (23 loc) · 756 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
// insertion sort implementation in Java
import java.util.Arrays;
public class lab3 {
public static void insertionSort(int[] arr) {
int n = arr.length;
for (int i = 1; i < n; i++) {
int c = i;
while (c > 0 && arr[c] < arr[c - 1]) {
// Swap arr[c] and arr[c - 1]
int temp = arr[c];
arr[c] = arr[c - 1];
arr[c - 1] = temp;
c--;
}
}
}
public static void main(String[] args) {
int[] arr = {64, 34, 25, 12, 22, 11, 90};
System.out.println("Original array: " + Arrays.toString(arr));
insertionSort(arr);
System.out.println("Sorted array: " + Arrays.toString(arr));
}
}