-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtime_complexity.java
More file actions
32 lines (26 loc) · 1.02 KB
/
time_complexity.java
File metadata and controls
32 lines (26 loc) · 1.02 KB
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
//Write a program to understand Time Complexity.
public class TimeComplexityExample {
public static void main(String[] args) {
int[] arr = new int[10000]; // change size for scaling test
// Fill array with random values
for (int i = 0; i < arr.length; i++)
arr[i] = (int)(Math.random() * 10000);
// Start time
long startTime = System.nanoTime();
// Call the algorithm here
bubbleSort(arr); // Replace with mergeSort, quickSort, etc.
// End time
long endTime = System.nanoTime();
// Total time in milliseconds
System.out.println("Execution Time: " + (endTime - startTime) / 1_000_000.0 + " ms");
}
// Example: Bubble Sort
static void bubbleSort(int[] arr) {
int n = arr.length;
for (int i = 0; i < n - 1; i++)
for (int j = 0; j < n - i - 1; j++)
if (arr[j] > arr[j + 1]) {
int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp;
}
}
}