Skip to content

Instantly share code, notes, and snippets.

@L8RFN
Last active July 19, 2023 03:11
Show Gist options
  • Save L8RFN/b0e8bc870d25c34d8ebd05d846b6052e to your computer and use it in GitHub Desktop.
Save L8RFN/b0e8bc870d25c34d8ebd05d846b6052e to your computer and use it in GitHub Desktop.
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the size of the array to sort: ");
int size = sc.nextInt();
sc.close();
int[] arr = new int[size];
Random rand = new Random();
for (int i = 0; i < size; i++) {
arr[i] = rand.nextInt(10000);
}
long startTime = System.currentTimeMillis();
Arrays.sort(arr);
long endTime = System.currentTimeMillis();
long elapsedTime = endTime - startTime;
System.out.println("Sorted " + size + " elements in " + elapsedTime + " ms");
}
}
//There is a direct relation between the size of the array and time it takes to sort it.
//test 1:
Enter the size of the array to sort: 100000
Sorted 100000 elements in 17 ms
//test 2:
Enter the size of the array to sort: 99
Sorted 99 elements in 0 ms
//test 3:
Enter the size of the array to sort: 10000
Sorted 10000 elements in 2 ms
//O(n log n) -------- from the internet(it is hard to find it manually until now).
//question: 10000 took 2 ms , then how much time will 100000 take?
answer: 1000 log 1000 ========== 2 ms
100000 log 100000 ====== X ms , find X...
(1000 log 1000) * X = 200000 log 100000
X = 200 (log (100000) / log (1000))
X = 200 (5/3)
X = 333
well, it is not 17 ms cause the running time is dependent on many external factors.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment