-
Notifications
You must be signed in to change notification settings - Fork 368
/
randomized_quicksort.java
47 lines (42 loc) · 1.3 KB
/
randomized_quicksort.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
import java.util.Random;
public class randomized_quicksort {
public static int partition(int[] arr, int p, int r) {
int x = arr[r];
int i = p - 1;
for (int j = p; j <= r - 1; j++) {
if (arr[j] <= x) {
i++;
swap(arr, i, j);
}
}
swap(arr, i + 1, r);
return i + 1;
}
public static int randomizedPartition(int[] arr, int p, int r) {
int i = p + new Random().nextInt(r - p + 1);
swap(arr, i, r);
return partition(arr, p, r);
}
public static void randomizedQuicksort(int[] arr, int p, int r) {
if (p < r) {
int q = randomizedPartition(arr, p, r);
randomizedQuicksort(arr, p, q - 1);
randomizedQuicksort(arr, q + 1, r);
}
}
public static void swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
public static void main(String[] args) {
int[] arr = {78, 9, 10, -1, 2, -45, 91};
int n = arr.length;
randomizedQuicksort(arr, 0, n - 1);
System.out.println("Sorted array using Randomized quicksort is:");
for (int i = 0; i < n; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
}