-
Notifications
You must be signed in to change notification settings - Fork 0
/
randomized_quick_sort.c
82 lines (73 loc) · 2.01 KB
/
randomized_quick_sort.c
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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define INPUT_SIZE 1000000
void randomized_quick_sort(int[], int, int);
int randomized_partition(int[], int, int);
int partition(int[], int, int);
void swap(int[], int, int);
int main(void){
int arr[INPUT_SIZE];
int i;
for(i = 0; i < INPUT_SIZE; i++){
arr[i] = random();
}
clock_t start_time = clock();
randomized_quick_sort(arr, 0, INPUT_SIZE - 1);
clock_t end_time = clock();
double execution_time = (double)(end_time - start_time) / CLOCKS_PER_SEC;
/*for(i = 0; i < INPUT_SIZE; i++){
printf("%i\n", arr[i]);
}*/
printf("\nExecution Time = %f\n", execution_time);
return 0;
}
/*
** A is the array to be sorted
** p is the start index of the portion of A to be sorted in the current call
** r is the end index of the portion of A to be sorted in the current call
*/
void randomized_quick_sort(int A[], int p, int r){
if(p < r){
int q = randomized_partition(A, p, r);
randomized_quick_sort(A, p, q - 1);
randomized_quick_sort(A, q + 1, r);
}
}
/*
** A is the array with a portion to be partitioned
** p is the start index of the portion of A to be partitioned in the current call
** r is the end index of the portion of A to be partitioned in the current call
*/
int randomized_partition(int A[], int p, int r){
swap(A, random() % INPUT_SIZE, r);
return partition(A, p, r);
}
/*
** A is the array with a portion to be partitioned
** p is the start index of the portion of A to be partitioned in the current call
** r is the end index of the portion of A to be partitioned in the current call
*/
int partition(int A[], int p, int r){
int x = A[r];
int i = p - 1;
int j;
for(j = p; j < r ; j++){
if(A[j] <= x){
i++;
swap(A, i, j);
}
}
swap(A, i + 1, r);
return i + 1;
}
/*
** A is the array that holds items to be swapped
** i is the index of the item to be swapped
** j is the index of the other item to be swapped
*/
void swap(int A[], int i, int j){
int temp = A[i];
A[i] = A[j];
A[j] = temp;
}