This repository has been archived by the owner on Apr 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
CalculatingRunTime.java
86 lines (51 loc) · 2.37 KB
/
CalculatingRunTime.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*----- Checking the run time of an Array using different approaches -----*/
import java.util.*;
import java.util.concurrent.TimeUnit;
public class CalculatingRunTime {
public static void main(String args[]){
int arr[] = new int[(Integer.MAX_VALUE)/10000];
for(int i=(Integer.MAX_VALUE)/10000; i>0; i--){
arr[(Integer.MAX_VALUE)/10000 - i] = i;
}
/*--- Calculating the run time for sorting using nested for loops: ---*/
long timeStarts = System.currentTimeMillis(); //Time starts here
for(int i=0; i<arr.length; i++){
for(int j=i+1; j<arr.length; j++){
if(arr[i]>arr[j]) {
int bucket = arr[i];
arr[i] = arr[j];
arr[j] = bucket;
}
}
}
System.out.print("The sorted array in ASC order is:");
for(int i=0; i<arr.length; i++){
System.out.print(" "+arr[i]);
}
long timeEnds = System.currentTimeMillis(); //Time ends here
long timeInSeconds = TimeUnit.MILLISECONDS.toSeconds(timeEnds - timeStarts);
System.out.printf("%nThe Time taken in seconds is: "+timeInSeconds+" seconds%n");
/*--- Checking run time for Array sorting using inbuilt sorting method: ---*/
int arr2[] = new int[(Integer.MAX_VALUE)/10000];
for(int i=(Integer.MAX_VALUE)/10000; i>0; i--){
arr2[(Integer.MAX_VALUE)/10000 - i] = i;
}
long timeStarts2 = System.currentTimeMillis(); //Time starts here
Arrays.sort(arr2);
System.out.print("The sorted array in ASC order is:");
for(int i=0; i<arr2.length; i++){
System.out.print(" "+arr2[i]);
}
long timeEnds2 = System.currentTimeMillis(); //Time ends here
long timeInSeconds2 = TimeUnit.MILLISECONDS.toSeconds(timeEnds2 - timeStarts2);
System.out.printf("%nThe Time taken in seconds is: "+timeInSeconds2+" seconds%n");
}
}
/*
* Worst case:
*
* The Run time for for-loop came out to be: 21 seconds
* The Run time for inbuilt sort came out to be: 1 second
*
*
*/