-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path4-QuickSort.java
More file actions
68 lines (63 loc) · 2.25 KB
/
4-QuickSort.java
File metadata and controls
68 lines (63 loc) · 2.25 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
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
/*Program 4.
Sort a given set of n integer elements using Quick Sort method and compute its time complexity. Run the program for varied values of n > 5000 and record the time taken to sort. Plot a graph of the time taken versus n on graph sheet. The elements can be read from a file or can be generated using the random number generator. Demonstrate using Java how the divide-and-conquer method works along with its time complexity analysis: worst case, average case and best case.
*/
import java.util.Scanner;
import java.util.Arrays;
import java.util.Random;
public class QuickSortComplexity {
static final int MAX = 10005;
static int[] a = new int[MAX];
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter Max array size: ");
int n = input.nextInt();
Random random = new Random();
System.out.println("Enter the array elements: ");
for (int i = 0; i < n; i++)
a[i] = input.nextInt(); // for keyboard entry
// a[i] = random.nextInt(1000); // generate
// random numbers � uniform distribution
// a = Arrays.copyOf(a, n); // keep only non zero elements
// Arrays.sort(a); // for worst-case time complexity
System.out.println("Input Array:");
for (int i = 0; i < n; i++)
System.out.print(a[i] + " ");
// set start time
long startTime = System.nanoTime();
QuickSortAlgorithm(0, n - 1);
long stopTime = System.nanoTime();
long elapsedTime = stopTime - startTime;
System.out.println("\nSorted Array:");
for (int i = 0; i < n; i++)
System.out.print(a[i] + " ");
System.out.println();
System.out.println("Time Complexity in ms for
n=" + n + " is: " + (double) elapsedTime / 1000000);
}
public static void QuickSortAlgorithm(int p, int r) {
int i, j, temp, pivot;
if (p < r) {
i = p;
j = r + 1;
pivot = a[p]; // mark first element as pivot
while (true) {
i++;
while (a[i] < pivot && i < r)
i++;
j--;
while (a[j] > pivot)
j--;
if (i < j) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
} else
break; // partition is over
}
a[p] = a[j];
a[j] = pivot;
QuickSortAlgorithm(p, j - 1);
QuickSortAlgorithm(j + 1, r);
}
}
}