From 543112a3ed3b7d10462819e5615e1ed6902cce2e Mon Sep 17 00:00:00 2001 From: A-hungry-wolf Date: Thu, 29 Apr 2021 14:35:10 +0800 Subject: [PATCH 1/3] finish QuickSort Algorithms --- week7/quicksort.c | 163 ++++++++++++++++++++++++++++++++++++++++++++++ week7/quicksort.h | 26 ++++++++ 2 files changed, 189 insertions(+) create mode 100644 week7/quicksort.c create mode 100644 week7/quicksort.h diff --git a/week7/quicksort.c b/week7/quicksort.c new file mode 100644 index 0000000..35f2b62 --- /dev/null +++ b/week7/quicksort.c @@ -0,0 +1,163 @@ +#include +#include +#include + +#include "quicksort.h" + +int main() { + /* size of test data */ + int size_1 = 10; + int size_2 = 100000; + + int *A1, *A2; + clock_t start, end; + + printf("Allocating 10 test data space for A1...\n"); + A1 = (int *)malloc(sizeof(int) * size_1); + if (A1 == NULL) { + printf("Fatal: can not allocate A1 space\n"); + exit(1); + } + + printf("Create random integer for A1...\n"); + for (int i = 0; i < size_1; i++) { + A1[i] = rand(); + } + + printf("Testing quick sort...\n"); + start = clock(); + QuickSort(A1, 0, size_1 - 1); + end = clock(); + _PrintClockInterval(start, end); + + if(Check(A1, size_1)){ + printf("Checking result... pass\n"); + } else { + printf("Checking result... fail\n"); + } + + printf("Freeing space of A1\n"); + free(A1); + A1 = NULL; + + printf("Allocating 100000 test data space for A2...\n"); + A2 = (int *)malloc(sizeof(int) * size_2); + if (A2 == NULL) { + printf("Fatal: can not allocate A1 space\n"); + exit(1); + } + + printf("Create random integer for A2...\n"); + for (int i = 0; i < size_2; i++) { + A2[i] = rand(); + } + + printf("Testing merge sort...\n"); + start = clock(); + QuickSort(A2, 0, size_2 - 1); + end = clock(); + PrintClockInterval(start, end); + + if(Check(A2, size_2)){ + printf("Checking result... pass\n"); + } else { + printf("Checking result... fail\n"); + } + + printf("Freeing space of A2\n"); + free(A2); + A2 = NULL; + + return 0; +} + +void QuickSort(int *A, int left, int right) { + int q; + if (left >= right){ + return; + } + /* q is the position of the pivot */ + q = Partition(A, left, right); + QuickSort(A, left, q-1); + QuickSort(A, q+1, right); +} + +int Partition(int *A, int left, int right) { + /* p is the position of the pivot */ + int p; + p = Pivot(A, left, right); + /* use i, j, pivot to locate left, right, and pivot */ + int i, j, pivot; + Swap(A, p, right); + i = left, j = right-1, pivot = A[right]; + while(true){ + while (i=left && A[j]>pivot) + j--; + if (iA[middle]){ + if(A[middle]>A[right]){ + middle = middle; + }else if(A[right]>A[left]) { + middle = left; + }else{ + middle = right; + } + }else{ + if(A[middle] A[i + 1]) { + return false; + } + } + + return true; +} + +void PrintClockInterval(clock_t start, clock_t end) { + printf("the time cost of this algorithms is %ld ms\n", (end - start) / (CLOCKS_PER_SEC / 1000)); +} + +/* use this to check small size of data */ +void _PrintClockInterval(clock_t start, clock_t end) { + printf("the time cost of this algorithms is %ld ms\n", (end - start)); +} + + + diff --git a/week7/quicksort.h b/week7/quicksort.h new file mode 100644 index 0000000..5f77fc8 --- /dev/null +++ b/week7/quicksort.h @@ -0,0 +1,26 @@ +#include +#include + + +/* Sort array A[left..right] using quick sort */ +/* Pivot is picked using median-of-3 */ +void QuickSort(int *A, int left, int right); + +/* Sort the array in order */ +int Partition(int *A, int left, int right); + +/* pick the middle value between A[left], A[middle], A[right] */ +int Pivot(int *A, int left, int right); + +/* Swap A[x], A[y] */ +void Swap(int *A, int x,int y); + +/* create check functions */ +bool Check(int *A, int size); + +/* print the time of small data */ +void PrintClockInterval(clock_t start, clock_t end); + +/* use this to check small size of data */ +void _PrintClockInterval(clock_t start, clock_t end); + From 5fb1ec5cef1918a790ee1fb83aee55dc3c4c5969 Mon Sep 17 00:00:00 2001 From: A-hungry-wolf Date: Thu, 29 Apr 2021 15:02:16 +0800 Subject: [PATCH 2/3] change the date --- {week7 => week8}/quicksort.c | 0 {week7 => week8}/quicksort.h | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename {week7 => week8}/quicksort.c (100%) rename {week7 => week8}/quicksort.h (100%) diff --git a/week7/quicksort.c b/week8/quicksort.c similarity index 100% rename from week7/quicksort.c rename to week8/quicksort.c diff --git a/week7/quicksort.h b/week8/quicksort.h similarity index 100% rename from week7/quicksort.h rename to week8/quicksort.h From 968416e919c86c6e9dd5dc210a77302647d77922 Mon Sep 17 00:00:00 2001 From: heimoshuiyu Date: Thu, 29 Apr 2021 15:24:27 +0800 Subject: [PATCH 3/3] week8 format --- week8/quicksort.c | 62 +++++++++++++++++++++++------------------------ week8/quicksort.h | 8 +++--- 2 files changed, 33 insertions(+), 37 deletions(-) diff --git a/week8/quicksort.c b/week8/quicksort.c index 35f2b62..cf32f11 100644 --- a/week8/quicksort.c +++ b/week8/quicksort.c @@ -30,12 +30,12 @@ int main() { end = clock(); _PrintClockInterval(start, end); - if(Check(A1, size_1)){ + if (Check(A1, size_1)) { printf("Checking result... pass\n"); } else { - printf("Checking result... fail\n"); + printf("Checking result... fail\n"); } - + printf("Freeing space of A1\n"); free(A1); A1 = NULL; @@ -58,12 +58,12 @@ int main() { end = clock(); PrintClockInterval(start, end); - if(Check(A2, size_2)){ + if (Check(A2, size_2)) { printf("Checking result... pass\n"); } else { - printf("Checking result... fail\n"); + printf("Checking result... fail\n"); } - + printf("Freeing space of A2\n"); free(A2); A2 = NULL; @@ -73,29 +73,29 @@ int main() { void QuickSort(int *A, int left, int right) { int q; - if (left >= right){ + if (left >= right) { return; } /* q is the position of the pivot */ q = Partition(A, left, right); - QuickSort(A, left, q-1); - QuickSort(A, q+1, right); + QuickSort(A, left, q - 1); + QuickSort(A, q + 1, right); } int Partition(int *A, int left, int right) { /* p is the position of the pivot */ int p; p = Pivot(A, left, right); - /* use i, j, pivot to locate left, right, and pivot */ + /* use i, j, pivot to locate left, right, and pivot */ int i, j, pivot; Swap(A, p, right); - i = left, j = right-1, pivot = A[right]; - while(true){ - while (i=left && A[j]>pivot) + while (j >= left && A[j] > pivot) j--; - if (iA[middle]){ - if(A[middle]>A[right]){ + if (A[left] > A[middle]) { + if (A[middle] > A[right]) { middle = middle; - }else if(A[right]>A[left]) { + } else if (A[right] > A[left]) { middle = left; - }else{ + } else { middle = right; } - }else{ - if(A[middle] #include - -/* Sort array A[left..right] using quick sort */ -/* Pivot is picked using median-of-3 */ +/* Sort array A[left..right] using quick sort + * Pivot is picked using median-of-3 */ void QuickSort(int *A, int left, int right); /* Sort the array in order */ @@ -13,7 +12,7 @@ int Partition(int *A, int left, int right); int Pivot(int *A, int left, int right); /* Swap A[x], A[y] */ -void Swap(int *A, int x,int y); +void Swap(int *A, int x, int y); /* create check functions */ bool Check(int *A, int size); @@ -23,4 +22,3 @@ void PrintClockInterval(clock_t start, clock_t end); /* use this to check small size of data */ void _PrintClockInterval(clock_t start, clock_t end); -