-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquick.c
More file actions
186 lines (157 loc) · 4.42 KB
/
quick.c
File metadata and controls
186 lines (157 loc) · 4.42 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include "quick.h"
#include "queue.h"
#include "sorting_statistics.h"
#include "stack.h"
#include <stdint.h>
// Description:
// Sets the max_size if current_size is larger than the current max_size.
//
// Parameters:
// uint32_t current_size - The current size.
// uint32_t *max_size - The pointer to the current max size.
//
// Returns:
// Nothing.
static void set_max_size( uint32_t current_size, uint32_t *max_size ) {
if ( current_size > *max_size ) {
*max_size = current_size;
}
}
// Description:
// Places elements less than the pivot to the left side of the array and elements
// greater than or equal to the pivot onto the right side of the array.
//
// Parameters:
// uint32_t *arr - The array to sort.
// int64_t lo - Starting point.
// int64_t hi - Ending point.
// SortingStatistics *stats - A pointer to a SortingStatistics struct that will hold the stats of the sort.
//
// Returns:
// int64_t - The division of the array.
static int64_t partition( uint32_t *arr, int64_t lo, int64_t hi, SortingStatistics *stats ) {
uint32_t pivot = arr[ lo + ( ( hi - lo ) / 2 ) ];
int64_t i = lo - 1;
int64_t j = hi + 1;
while ( i < j ) {
i += 1;
while ( ++stats->compares && arr[ i ] < pivot ) {
i += 1;
}
j -= 1;
while ( ++stats->compares && arr[ j ] > pivot ) {
j -= 1;
}
if ( i < j ) {
// Swap arr[i] and arr[j].
uint32_t old_arr_i = arr[ i ];
arr[ i ] = arr[ j ];
arr[ j ] = old_arr_i;
stats->moves += 3;
}
}
return j;
}
// Description:
// Helper function for recursive quicksort.
//
// Parameters:
// uint32_t *arr - The array to sort.
// uint32_t len - The length of the array to sort.
// int64_t lo - Starting point.
// int64_t hi - Ending point.
// SortingStatistics *stats - A pointer to the SortingStatistics struct that holds the sorting statistics.
//
// Returns:
// Nothing.
static void quicksort_recursive_internal( uint32_t *arr, uint32_t len, int64_t lo, int64_t hi, SortingStatistics *stats ) {
int64_t p = partition( arr, lo, hi, stats );
if ( lo < p ) {
quicksort_recursive_internal( arr, len, lo, p, stats );
}
if ( hi > p + 1 ) {
quicksort_recursive_internal( arr, len, p + 1, hi, stats );
}
}
// Description:
// Uses quicksort to sort an array recursively.
//
// Parameters:
// uint32_t *arr - The array to sort.
// uint32_t len - The length of the array to sort.
//
// Returns:
// SortingStatistics - The statistics for the sort.
SortingStatistics quicksort_recursive( uint32_t *arr, uint32_t len ) {
SortingStatistics stats = sorting_statistics_create( len );
quicksort_recursive_internal( arr, len, 0, len - 1, &stats );
return stats;
}
// Description:
// Uses quicksort to sort an array using a stack.
//
// Parameters:
// uint32_t *arr - The array to sort.
// uint32_t len - The length of the array to sort.
//
// Returns:
// SortingStatistics - The statistics for the sort.
SortingStatistics quicksort_stack( uint32_t *arr, uint32_t len ) {
SortingStatistics stats = sorting_statistics_create( len );
int64_t lo = 0;
int64_t hi = len - 1;
Stack *stack = stack_create( len );
stack_push( stack, lo );
stack_push( stack, hi );
set_max_size( stack_size( stack ), &stats.max_ds_size );
while ( !stack_empty( stack ) ) {
stack_pop( stack, &hi );
stack_pop( stack, &lo );
int64_t p = partition( arr, lo, hi, &stats );
if ( lo < p ) {
stack_push( stack, lo );
stack_push( stack, p );
}
if ( hi > p + 1 ) {
stack_push( stack, p + 1 );
stack_push( stack, hi );
}
set_max_size( stack_size( stack ), &stats.max_ds_size );
}
stack_delete( &stack );
return stats;
}
// Description:
// Uses quicksort to sort an array using a queue.
//
// Parameters:
// uint32_t *arr - The array to sort.
// uint32_t len - The length of the array to sort.
//
// Returns:
// Nothing.
SortingStatistics quicksort_queue( uint32_t *arr, uint32_t len ) {
SortingStatistics stats = sorting_statistics_create( len );
int64_t lo = 0;
int64_t hi = len - 1;
Queue *queue = queue_create( len );
queue_add( queue, lo );
queue_add( queue, hi );
set_max_size( queue_size( queue ), &stats.max_ds_size );
while ( !queue_empty( queue ) ) {
queue_remove( queue, &lo );
queue_remove( queue, &hi );
int64_t p = partition( arr, lo, hi, &stats );
if ( lo < p ) {
queue_add( queue, lo );
queue_add( queue, p );
}
if ( hi > p + 1 ) {
queue_add( queue, p + 1 );
queue_add( queue, hi );
}
set_max_size( queue_size( queue ), &stats.max_ds_size );
}
queue_delete( &queue );
return stats;
}