week9 heapsort and quicksort compare

This commit is contained in:
2021-04-28 22:02:17 +08:00
parent 9ed5742b89
commit d9790182c8
2 changed files with 129 additions and 0 deletions

113
week9/T8.c Normal file
View File

@@ -0,0 +1,113 @@
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "T8.h"
int main() {
int size = 5000000;
clock_t start_time, end_time;
int *A1 = (int *)malloc(sizeof(int) * size);
if (A1 == NULL) {
printf("Fatal: Can not allocate memory of A1 with size=%d\n", size);
exit(1);
}
int *A2 = (int *)malloc(sizeof(int) * size);
if (A2 == NULL) {
printf("Fatal: Can not allocate memory of A2 with size=%d\n", size);
exit(1);
}
/* generate random number */
for (int i = 0; i < size; i++) {
A1[i] = A2[i] = rand();
}
printf("Testing HeapSort function ... ");
start_time = clock();
HeapSort(A1, size);
end_time = clock();
printf("%s\n", Check(A1, size) ? "pass" : "failed");
PrintTimeDuration(start_time, end_time);
printf("Testing QuickSort function ... ");
start_time = clock();
QuickSort(A2, 0, size - 1);
end_time = clock();
printf("%s\n", Check(A1, size) ? "pass" : "failed");
PrintTimeDuration(start_time, end_time);
return 0;
}
bool Check(int *A, int size) {
for (int i = 0; i < size - 1; i++) {
if (A[i] > A[i + 1]) {
return false;
}
}
return true;
}
void QuickSort(int *A, int left, int right) {
if (left >= right) {
return;
}
int pi = Partition(A, left, right);
QuickSort(A, left, pi - 1);
QuickSort(A, pi + 1, right);
}
int Partition(int *A, int left, int right) {
int pivot = A[right];
int i = left - 1;
for (int j = left; j < right; j++) {
if (A[j] < pivot) {
Swap(&A[++i], &A[j]);
}
}
Swap(&A[++i], &A[right]);
return i;
}
void HeapSort(int *A, int n) {
int i;
for (i = n / 2; i >= 0; i--) {
PercDown(A, i, n);
}
for (i = n - 1; i > 0; i--) {
Swap(&A[0], &A[i]);
PercDown(A, 0, i);
}
}
void PercDown(int *A, int i, int n) {
int child, tmp;
for (tmp = A[i]; (2 * i + 1) < n; i = child) {
child = 2 * i + 1;
if (child != n - 1 && A[child + 1] > A[child]) {
child++;
}
if (tmp < A[child]) {
A[i] = A[child];
} else {
break;
}
}
A[i] = tmp;
}
void Swap(int *a, int *b) {
int tmp = *a;
*a = *b;
*b = tmp;
}
void PrintTimeDuration(clock_t start_time, clock_t end_time) {
printf("Time cost: %ld ms\n", (end_time - start_time) / (CLOCKS_PER_SEC / 1000));
}

16
week9/T8.h Normal file
View File

@@ -0,0 +1,16 @@
#include <time.h>
void QuickSort(int *A, int left, int right);
int Partition(int *A, int left, int right);
void HeapSort(int *A, int n);
void PercDown(int *A, int i, int n);
bool Check(int *A, int size);
void Swap(int *a, int *b);
void PrintTimeDuration(clock_t start_time, clock_t end_time);