-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtim-sort.java
More file actions
28 lines (21 loc) · 868 Bytes
/
tim-sort.java
File metadata and controls
28 lines (21 loc) · 868 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
import java.util.Arrays;
import java.util.Random;
public class TimSortExample {
public static void main(String[] args) {
// Create an array with 1 million Integer elements
Integer[] array = new Integer[1_000_000];
// Fill the array with random values
Random random = new Random();
for (int i = 0; i < array.length; i++) {
array[i] = random.nextInt();
}
// Record the start time
long startTime = System.currentTimeMillis();
// Sort the array using TimSort (implicitly used by Arrays.sort for objects)
Arrays.sort(array);
// Record the end time
long endTime = System.currentTimeMillis();
// Calculate and print the sorting time
System.out.println("Sorting 1 million integers took " + (endTime - startTime) + " milliseconds.");
}
}