-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQ2.java
37 lines (31 loc) · 910 Bytes
/
Q2.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
import java.util.Arrays;
public class Q2 {
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;
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={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));
}
}
/*
Output :
Original array: [64, 34, 25, 12, 22, 11, 90]
Sorted array: [11, 12, 22, 25, 34, 64, 90]
Time Complexity :
Worst Case: O(n²)
Best Case: O(n) (if the array is already sorted)
Average Case: O(n²)
Space Complexity : O(1)
*/