c1; if(argc 0){ int tsize; tsize = atoi(argv [ 1]); if(tsize 0){ size = tsize; }否则{ printf (指定的%d的大小无效。使用10000.\ n, tsize); } } a = malloc(size * sizeof * a); if(a == NULL){ puts(ERROR:Out of memory。); 退出(EXIT_FAILURE); } ap = a; for(x = 0; x< size; x ++){ a [x] = rand()%size; } t0 =时间(NULL); c0 = clock(); printf(" \\\Mergesorting \ n"); mergesort(ap,size); t1 =时间(NULL); c1 = clock(); printf(" \ nMergesort(%d项)\ nTime差异:%f \tClock周期 差异:%f \ n", size,(float)(t1 - t0),(float)(c1 - c0)); for(x = 0; x< size; x ++){ a [x] = rand()%size; } t0 =时间(NULL); c0 = clock(); quicksort(ap,size); t1 =时间(NULL); c1 = clock(); printf(" \\ nQuicksort(%d项)\ nTime差异:%f \ tClock周期 差异:%f \ n", size,(float)(t1 - t0),(浮动)(c1 - c0)); 返回0; } #endif / * C:\ tmp> cl / W4 / Ox / DUNIT_TEST stest.c Microsoft(R)32位C / C ++优化编译器版本14.00 .50727.42 for 80x86 版权所有(C)Microsoft Corporation。保留所有权利。 stest.c stest.c(91):警告C4127:条件表达式是常量 Microsoft (R)增量链接器版本8.00.50727.42 版权所有(C)Microsoft Corporation。保留所有权利。 /out:stest.exe stest.obj C:\ tmp> ; stest -9 指定的-9大小无效。使用10000. Mergesorting Mergesort(100000件) 时差:0.000000时钟周期差:93.000000 Quicksort(100000件) 时差:0.000000时钟周期差:0.000000 C:\ tmp> stest frog 指定的0大小无效。使用10000. Mergesorting Mergesort(100000件) 时差:0.000000时钟周期差:93.000000 Quicksort(100000件) 时差:0.000000时钟周期差异:16.000000 C:\ tmp> stest 0 指定的大小为0。使用10000. Mergesorting Mergesort(100000件) 时差:0.000000时钟周期差:109.000000 Quicksort(100000件) 时差:0.000000时钟周期差异:16.000000 C:\ tmp> stest 1000000 Mergesorting Mergesort(1000000件) 时差:1.000000时钟周期差异: 1032.000000 Quicksort(1000000件) 时差:0.000000时钟周期差:141.000000 C:\\ \\ tmp> stest 10000000 Mergesorting Mergesort(10000000件) 时差:24.000000时钟周期差异:23391.000000 Quicksort(10000000件) 时差:4.000000时钟周期差异:3703.000000 * / /*** There is still lots of room for improvement here. You were missingheader files** and prototypes, and had implicit declaration of function types.** You were getting segfaults because of the size of your automaticvariables.** You should do a test to prove that the data is sorted.** You should try other distributions besides random (your commentsabout** creating a reversed partition were wrong...) but creating a reversedpartition** is a very good idea [hint -- this onerous version of qsort willpositively blow chunks]*/ #include <stdlib.h> extern void merge(int *, int *, int *, int);extern void mergesort(int *, int);extern int partition(int *, int, int);extern void q_sort(int *, int, int);extern void quicksort(int *, int);extern void swap(int *, int, int); void mergesort(int *a, int size){int *b;int *c;int *bp;int *cp;int x;int y; b = malloc((size / 2) * sizeof *b);c = malloc((size - (size / 2)) * sizeof *c);bp = b;cp = c; if (size 1) {for (x = 0; x < (size / 2); x++) {b[x] = *(a + x);}for (y = 0; y < (size - (size / 2)); y++) {c[y] = *(a + x);x++;}mergesort(bp, (size / 2));mergesort(cp, (size - (size / 2)));merge(a, bp, cp, size);}} void merge(int *a, int *b, int *c, int size){int i = 0,j = 0,k = 0;while (i < (size / 2) && j < (size - (size / 2))) {if (*(b + i) < *(c + j)) {*(a + k) = *(b + i);i++;} else {*(a + k) = *(c + j);j++;}k++;}if (i == (size / 2)) {while (j < (size - (size / 2))) {*(a + k) = *(c + j);k++;j++;}} else {while (i < (size / 2)) {*(a + k) = *(b + i);k++;i++;}}} void quicksort(int *a, int size){q_sort(a, 0, size - 1);} void q_sort(int *a, int l, int r){int s;if (l < r) {s = partition(a, l, r);q_sort(a, l, s - 1);q_sort(a, s + 1, r);}} int partition(int *a, int l, int r){int i,j,p;p = *(a + l);i = l;j = r + 1;while (1 < 2) {do++i;while (*(a + i) <= p && i <= r);do--j;while (*(a + j) p && j >= l);if (i >= j)break;swap(a, i, j);}swap(a, l, j);return j;} void swap(int *a, int b, int c){int temp = *(a + b);*(a + b) = *(a + c);*(a + c) = temp;} #ifdef UNIT_TEST #include <stdio.h>#include <time.h> int main(int argc, char **argv){int size = 100000;int *a;int *ap,x;time_t t0,t1;clock_t c0,c1; if (argc 0) {int tsize;tsize = atoi(argv[1]);if (tsize 0) {size = tsize;} else {printf("Invalid size of %d specified. Using 10000.\n",tsize);}}a = malloc(size * sizeof *a);if (a == NULL) {puts("ERROR: Out of memory.");exit(EXIT_FAILURE);}ap = a;for (x = 0; x < size; x++) {a[x] = rand() % size;}t0 = time(NULL);c0 = clock();printf("\nMergesorting\n");mergesort(ap, size);t1 = time(NULL);c1 = clock();printf("\nMergesort(%d items)\nTime Difference: %f\tClock CycleDifference: %f\n",size, (float) (t1 - t0), (float) (c1 - c0)); for (x = 0; x < size; x++) {a[x] = rand() % size;} t0 = time(NULL);c0 = clock();quicksort(ap, size);t1 = time(NULL);c1 = clock();printf("\nQuicksort(%d items)\nTime Difference: %f\tClock CycleDifference: %f\n",size, (float) (t1 - t0), (float) (c1 - c0));return 0;}#endif/*C:\tmp>cl /W4 /Ox /DUNIT_TEST stest.cMicrosoft (R) 32-bit C/C++ Optimizing Compiler Version 14.00.50727.42for 80x86Copyright (C) Microsoft Corporation. All rights reserved. stest.cstest.c(91) : warning C4127: conditional expression is constantMicrosoft (R) Incremental Linker Version 8.00.50727.42Copyright (C) Microsoft Corporation. All rights reserved. /out:stest.exestest.obj C:\tmp>stest -9Invalid size of -9 specified. Using 10000. Mergesorting Mergesort(100000 items)Time Difference: 0.000000 Clock Cycle Difference: 93.000000 Quicksort(100000 items)Time Difference: 0.000000 Clock Cycle Difference: 0.000000 C:\tmp>stest frogInvalid size of 0 specified. Using 10000. Mergesorting Mergesort(100000 items)Time Difference: 0.000000 Clock Cycle Difference: 93.000000 Quicksort(100000 items)Time Difference: 0.000000 Clock Cycle Difference: 16.000000 C:\tmp>stest 0Invalid size of 0 specified. Using 10000. Mergesorting Mergesort(100000 items)Time Difference: 0.000000 Clock Cycle Difference: 109.000000 Quicksort(100000 items)Time Difference: 0.000000 Clock Cycle Difference: 16.000000 C:\tmp>stest 1000000 Mergesorting Mergesort(1000000 items)Time Difference: 1.000000 Clock Cycle Difference: 1032.000000 Quicksort(1000000 items)Time Difference: 0.000000 Clock Cycle Difference: 141.000000 C:\tmp>stest 10000000 Mergesorting Mergesort(10000000 items)Time Difference: 24.000000 Clock Cycle Difference: 23391.000000 Quicksort(10000000 items)Time Difference: 4.000000 Clock Cycle Difference: 3703.000000 */ T. hanks for all the help ...还有一个问题 试图找出一种更好的排序时间,但是没有 gethrtime电脑(运行MAC操作系统) 有什么建议吗?(对于小数字进行多次排序和 取平均值的唯一方法,或者有什么东西否则我可以使用 吗?) Thanks for all the help...one more question Trying to figure out a better way to time the sorting, but there is nogethrtime on this computer(running MAC OS) Any suggestions?(is running sorts for small numbers several times andtaking the average the only way, or is there something else I may beable to use?) 这篇关于合并中的段错误和快速排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-30 17:34