-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsertionsort.java
More file actions
29 lines (26 loc) · 831 Bytes
/
insertionsort.java
File metadata and controls
29 lines (26 loc) · 831 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
public class Main {
public static void insertion(int arr[]) {
for (int i = 1; i < arr.length; i++) { // Start from the second element
int j = i;
while (j > 0 && arr[j - 1] > arr[j]) {
// Swap elements
int temp = arr[j];
arr[j] = arr[j - 1];
arr[j - 1] = temp;
j--; // Move to the previous element
}
}
}
public static void print(int arr[]) {
System.out.print("Insertion sort result: ");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
public static void main(String[] args) {
int arr[] = {1, 6, 3, 4, 5,2 };
insertion(arr);
print(arr);
}
}