最近公司组织了一次算法题,其中用到了排序,看了一下,有很多同事的排序写得还是很见功底的,至于为什么要写这篇博客,很多人要说,网上排序算法的博客一大把,各式各样,我是搜了一些排序算法的博客,不过基本都是相互抄的或者是转载的,而且很多看起来很高深,但真正当你没有网络,没有任何资料,不能用lib库时,我不知道还有多少人能写出最简单的排序算法,这是我写的目的,所以我找了一些经过测试是正确结果的同事的排序题.
1.首先是最简单的,就是两个for循环,依次比较,进行到底,不用管i和j之间的关系.
2.接下来是冒泡排序,这位资深的pl加了一个变量count,感觉有点意思,贴下代码:
void Bubble(int *p){ int count=0; int i,j,temp; for(i=0;ip[j+1]) { count++; // exchange temp = p[j+1]; p[j+1] = p[j]; p[j] = temp; } } if(count==0) { break; } }}
3.插入排序
void insertsort(int *a){ int i = 0; int temp = 0; int k = 0; for(i = 1; i < N; i++) { temp = a[i]; k = i; while(k>0&&a[k-1]>temp) { a[k]=a[k-1]; k--; } a[k]=temp; }}
4.快速排序
用的是递归,不推荐用,递归开销非常大,做过acm的就知道,用递归,时间经常会超时,当然递归的好处就是代码量少,比较容易看懂.
void quick_sort(struct Pair *p, int left, int right){ int m; if (left < right) { m = quickpass(p, left, right); quick_sort(p, left, m-1); quick_sort(p, m+1, right); }}
标准代码:效率非常高,我们组的,从中兴跳过来的,实力确实厉害,里面还有windows下和linux下时间函数的应用,而且从规范性来说,只在提示你
加代码的地方写代码,很多人没有注意这一条,写了很多其他独立的函数,还加了很多全局变量之类的,如果严格地来说,这就破坏了算法题的规则.
#include#include #include #define N 30int a1[30]={ 10,15,24,78,80,26,36,39,47,49,58,61,62,63,77,110,111,119,129,139,149,3,4,5,159,169,179,189,199,209};int b1[30]={ 240,0,9,12,20,23,30,42,55,58,66,70,205,215,75,79,82,86,105,115,125,135,145,155,165,175,185,195,225,235};int c1[30]={ 5,18,22,30,50,60,63,65,93,94,95,96,97,70,74,83,84,85,88,90,91,92,98,99,100,101,102,103,104,106};int a2[30]={ 31,40,53,169,179,189,199,58,61,62,119,129,139,149,63,64,65,71,14,16,27,30,110,111,159,209,219,229,239,249};int b2[30]={ 12,24,35,66,70,75,79,105,115,125,135,145,155,165,175,185,195,205,215,39,43,44,45,46,47,48,50,55,225,235};int c2[30]={ 131,141,151,13,15,17,20,22,32,33,34,36,37,49,60,68,77,100,120,121,161,171,181,191,201,211,221,231,241,251};int a3[30]={ 4,16,27,30,31,40,53,58,61,62,63,64,65,71,110,111,119,129,139,149,159,169,179,189,199,209,219,229,239,249};int b3[30]={ 12,24,35,39,43,44,45,46,47,48,50,55,66,70,75,79,105,115,125,135,145,155,165,175,185,195,205,215,225,250};int c3[30]={ 251,241,231,221,211,201,191,181,171,161,151,141,131,121,120,100,77,68,60,49,37,36,34,33,32,22,20,17,15,3};int a4[30]={ 26,27,28,29,19,3,4,5,6,7,13,14,15,8,9,10,11,12,16,20,21,22,23,24,25,0,1,2,17,18};int b4[30]={ 38,39,40,41,42,43,44,45,54,56,57,58,46,47,48,49,50,51,52,53,59,60,61,62,63,63,65,66,67,68};int c4[30]={ 16,27,104,105,106,107,108,109,110,111,35,45,58,100,101,102,103,112,1,4,7,8,14,113,114,115,116,117,118,120};int a5[30]={ 1,3,5,16,17,18,25,27,34,41,42,43,44,45,46,47,48,49,56,78,79,80,81,82,83,84,85,86,87,88};int b5[30]={ 9,12,20,22,70,80,91,92,30,31,32,33,34,35,36,37,38,39,40,42,59,93,94,95,96,97,98,99,100,101};int c5[30]={ 54,55,58,2,4,7,8,14,68,69,16,27,45,46,47,48,49,50,51,52,53,60,61,62,63,64,65,66,67,82};int a6[30]={ 4,10,13,18,19,20,21,22,23,24,25,26,27,28,29,34,40,51,52,53,54,55,56,57,58,59,60,61,62,63};int b6[30]={ 6,13,14,15,16,17,18,31,32,33,34,35,36,38,50,58,70,71,72,73,74,75,76,77,78,79,80,81,85,100};int c6[30]={ 2,5,9,23,39,40,49,58,70,71,72,73,74,75,76,77,78,79,80,90,91,92,93,94,95,96,97,98,99,100};struct Range{ int begin; int end;};struct Range test(int* a,int* b,int* c){ struct Range r; //Please write you code here int minDistance = -1; int rangBegin = -1, rangEnd = -1; int min = -1, max = -1; int i = 0, j = 0, k = 0; int preI = 0, preJ = 0, preK = 0; //sort a b c 升序 int temp; for (i = 1; i < N; i++) { temp = a[i]; for (j = i - 1;j>-1&&a[j] > temp; j--) { a[j + 1] = a[j]; a[j] = temp; } temp = b[i]; for (j = i - 1;j>-1&&b[j] > temp ; j--) { b[j + 1] = b[j]; b[j] = temp; } temp = c[i]; for (j = i - 1;j>-1&&c[j] > temp ; j--) { c[j + 1] = c[j]; c[j] = temp; } } //遍历a, 从b c中找到 >=当前值的,算距离,如果最小则替代。 preJ = 0; preK = 0; for ( i = 0; i < N; ++i ) { for ( j = preJ; j < N; ++j ) { if ( b[j] >= a[i] ) { break; } } if ( j == N ) { break; } preJ = j; for ( k = preK; k < N; ++k ) { if ( c[k] >= a[i] ) { break; } } if ( k == N ) { break; } preK = k; min = a[i]; max = b[j] > c[k] ? b[j]:c[k]; if (minDistance == -1 || max - min < minDistance || (max - min == minDistance && min < rangBegin)) { minDistance = max - min; rangBegin = min; rangEnd = max; } } preJ = 0; preK = 0; for ( i = 0; i < N; ++i ) { for ( j = preJ; j < N; ++j ) { if ( a[j] >= b[i] ) { break; } } if ( j == N ) { break; } preJ = j; for ( k = preK; k < N; ++k ) { if ( c[k] >= b[i] ) { break; } } if ( k == N ) { break; } preK = k; min = b[i]; max = a[j] < c[k] ? c[k]:a[j]; if (minDistance == -1 || max - min < minDistance || (max - min == minDistance && min < rangBegin)) { minDistance = max - min; rangBegin = min; rangEnd = max; } } preJ = 0; preK = 0; for ( i = 0; i < N; ++i ) { for ( j = preJ; j < N; ++j ) { if ( b[j] >= c[i] ) { break; } } if ( j == N ) { break; } preJ = j; for ( k = preK; k < N; ++k ) { if ( a[k] >= c[i] ) { break; } } if ( k == N ) { break; } preK = k; min = c[i]; max = b[j] < a[k] ? a[k]:b[j]; if (minDistance == -1 || max - min < minDistance || (max - min == minDistance && min < rangBegin)) { minDistance = max - min; rangBegin = min; rangEnd = max; } } r.begin = rangBegin; r.end = rangEnd; return r;}int main(void){ struct Range ran;#ifdef _WIN32 long start, end;#else struct timeval start, end;#endif int i=0; ran=test(a1,b1,c1); printf("The min range is : [%d,%d]\n",ran.begin,ran.end); ran=test(a2,b2,c2); printf("The min range is : [%d,%d]\n",ran.begin,ran.end); ran=test(a3,b3,c3); printf("The min range is : [%d,%d]\n",ran.begin,ran.end); ran=test(a4,b4,c4); printf("The min range is : [%d,%d]\n",ran.begin,ran.end); ran=test(a5,b5,c5); printf("The min range is : [%d,%d]\n",ran.begin,ran.end); ran=test(a6,b6,c6); printf("The min range is : [%d,%d]\n",ran.begin,ran.end);#ifdef _WIN32 start = clock();#else gettimeofday( &start, NULL );#endif for (i = 0; i < 500000; i++) { test(a1,b1,c1); test(a2,b2,c2); test(a3,b3,c3); }#ifdef WIN32 end = clock(); printf("time = %d ms\n", end - start);#else gettimeofday( &end, NULL ); printf("run time is :%f S \n",(1000000 * ( end.tv_sec - start.tv_sec ) + end.tv_usec - start.tv_usec)/1000000.0f); #endif return 0;}