-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: qwang59 <[email protected]>
- Loading branch information
Showing
2 changed files
with
78 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <sys/time.h> | ||
|
||
#define ARRAY_LEN 3000 | ||
|
||
size_t partition(int *a, size_t low, size_t high); // Move the declaration of the partition function here | ||
|
||
static inline void start(struct timeval *tm) | ||
{ | ||
gettimeofday(tm, NULL); | ||
} | ||
|
||
void swap(int *a, int *b) | ||
{ | ||
int temp = *a; | ||
*a = *b; | ||
*b = temp; | ||
} | ||
|
||
static inline void stop(struct timeval *tm1, struct timeval *tm2) | ||
{ | ||
unsigned long long t = 1000 * (tm2->tv_sec - tm1->tv_sec) + | ||
(tm2->tv_usec - tm1->tv_usec) / 1000; | ||
printf("%llu ms\n", t); | ||
} | ||
|
||
size_t partition(int *a, size_t low, size_t high) | ||
{ | ||
int pivot = a[high]; | ||
size_t i = low - 1; | ||
|
||
for (size_t j = low; j <= high - 1; j++) | ||
{ | ||
if (a[j] < pivot) | ||
{ | ||
i++; | ||
swap(&a[i], &a[j]); | ||
} | ||
} | ||
swap(&a[i + 1], &a[high]); | ||
return (i + 1); | ||
} | ||
|
||
void quick_sort(int *a, size_t low, size_t high) | ||
{ | ||
if (low < high) | ||
{ | ||
size_t pivot = partition(a, low, high); | ||
quick_sort(a, low, pivot - 1); | ||
quick_sort(a, pivot + 1, high); | ||
} | ||
} | ||
|
||
void sort_array() | ||
{ | ||
printf("Quick sorting array of %zu elements\n", ARRAY_LEN); | ||
int data[ARRAY_LEN]; | ||
for (size_t i = 0; i < ARRAY_LEN; ++i) | ||
{ | ||
data[i] = rand(); | ||
} | ||
quick_sort(data, 0, ARRAY_LEN - 1); | ||
} | ||
|
||
int main() | ||
{ | ||
struct timeval tm1, tm2; | ||
start(&tm1); | ||
sort_array(); | ||
gettimeofday(&tm2, NULL); | ||
stop(&tm1, &tm2); | ||
return 0; | ||
} |