week8 format
This commit is contained in:
@@ -30,7 +30,7 @@ int main() {
|
|||||||
end = clock();
|
end = clock();
|
||||||
_PrintClockInterval(start, end);
|
_PrintClockInterval(start, end);
|
||||||
|
|
||||||
if(Check(A1, size_1)){
|
if (Check(A1, size_1)) {
|
||||||
printf("Checking result... pass\n");
|
printf("Checking result... pass\n");
|
||||||
} else {
|
} else {
|
||||||
printf("Checking result... fail\n");
|
printf("Checking result... fail\n");
|
||||||
@@ -58,7 +58,7 @@ int main() {
|
|||||||
end = clock();
|
end = clock();
|
||||||
PrintClockInterval(start, end);
|
PrintClockInterval(start, end);
|
||||||
|
|
||||||
if(Check(A2, size_2)){
|
if (Check(A2, size_2)) {
|
||||||
printf("Checking result... pass\n");
|
printf("Checking result... pass\n");
|
||||||
} else {
|
} else {
|
||||||
printf("Checking result... fail\n");
|
printf("Checking result... fail\n");
|
||||||
@@ -73,13 +73,13 @@ int main() {
|
|||||||
|
|
||||||
void QuickSort(int *A, int left, int right) {
|
void QuickSort(int *A, int left, int right) {
|
||||||
int q;
|
int q;
|
||||||
if (left >= right){
|
if (left >= right) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
/* q is the position of the pivot */
|
/* q is the position of the pivot */
|
||||||
q = Partition(A, left, right);
|
q = Partition(A, left, right);
|
||||||
QuickSort(A, left, q-1);
|
QuickSort(A, left, q - 1);
|
||||||
QuickSort(A, q+1, right);
|
QuickSort(A, q + 1, right);
|
||||||
}
|
}
|
||||||
|
|
||||||
int Partition(int *A, int left, int right) {
|
int Partition(int *A, int left, int right) {
|
||||||
@@ -89,13 +89,13 @@ int Partition(int *A, int left, int 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;
|
int i, j, pivot;
|
||||||
Swap(A, p, right);
|
Swap(A, p, right);
|
||||||
i = left, j = right-1, pivot = A[right];
|
i = left, j = right - 1, pivot = A[right];
|
||||||
while(true){
|
while (true) {
|
||||||
while (i<right && A[i]<pivot)
|
while (i < right && A[i] < pivot)
|
||||||
i++;
|
i++;
|
||||||
while (j>=left && A[j]>pivot)
|
while (j >= left && A[j] > pivot)
|
||||||
j--;
|
j--;
|
||||||
if (i<j)
|
if (i < j)
|
||||||
Swap(A, i++, j--);
|
Swap(A, i++, j--);
|
||||||
else
|
else
|
||||||
break;
|
break;
|
||||||
@@ -106,29 +106,29 @@ int Partition(int *A, int left, int right) {
|
|||||||
|
|
||||||
int Pivot(int *A, int left, int right) {
|
int Pivot(int *A, int left, int right) {
|
||||||
int middle = 0;
|
int middle = 0;
|
||||||
middle = (left + right)/2;
|
middle = (left + right) / 2;
|
||||||
|
|
||||||
if(A[left]>A[middle]){
|
if (A[left] > A[middle]) {
|
||||||
if(A[middle]>A[right]){
|
if (A[middle] > A[right]) {
|
||||||
middle = middle;
|
middle = middle;
|
||||||
}else if(A[right]>A[left]) {
|
} else if (A[right] > A[left]) {
|
||||||
middle = left;
|
middle = left;
|
||||||
}else{
|
} else {
|
||||||
middle = right;
|
middle = right;
|
||||||
}
|
}
|
||||||
}else{
|
} else {
|
||||||
if(A[middle]<A[right]){
|
if (A[middle] < A[right]) {
|
||||||
middle = middle;
|
middle = middle;
|
||||||
}else if(A[right]<A[left]){
|
} else if (A[right] < A[left]) {
|
||||||
middle = left;
|
middle = left;
|
||||||
}else{
|
} else {
|
||||||
middle = right;
|
middle = right;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return middle;
|
return middle;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Swap(int *A, int x,int y) {
|
void Swap(int *A, int x, int y) {
|
||||||
int tmp;
|
int tmp;
|
||||||
tmp = A[x];
|
tmp = A[x];
|
||||||
A[x] = A[y];
|
A[x] = A[y];
|
||||||
@@ -151,13 +151,11 @@ bool Check(int *A, int size) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void PrintClockInterval(clock_t start, clock_t end) {
|
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));
|
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 */
|
/* use this to check small size of data */
|
||||||
void _PrintClockInterval(clock_t start, clock_t end) {
|
void _PrintClockInterval(clock_t start, clock_t end) {
|
||||||
printf("the time cost of this algorithms is %ld ms\n", (end - start));
|
printf("the time cost of this algorithms is %ld ms\n", (end - start));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +1,8 @@
|
|||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
|
||||||
|
/* Sort array A[left..right] using quick sort
|
||||||
/* Sort array A[left..right] using quick sort */
|
* Pivot is picked using median-of-3 */
|
||||||
/* Pivot is picked using median-of-3 */
|
|
||||||
void QuickSort(int *A, int left, int right);
|
void QuickSort(int *A, int left, int right);
|
||||||
|
|
||||||
/* Sort the array in order */
|
/* 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);
|
int Pivot(int *A, int left, int right);
|
||||||
|
|
||||||
/* Swap A[x], A[y] */
|
/* Swap A[x], A[y] */
|
||||||
void Swap(int *A, int x,int y);
|
void Swap(int *A, int x, int y);
|
||||||
|
|
||||||
/* create check functions */
|
/* create check functions */
|
||||||
bool Check(int *A, int size);
|
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 */
|
/* use this to check small size of data */
|
||||||
void _PrintClockInterval(clock_t start, clock_t end);
|
void _PrintClockInterval(clock_t start, clock_t end);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user