【最终修改版】冒泡排序
如果你发现任何bug,请告知。话说手机写代码真累。
程序代码:
#include <stdio.h> #include <time.h> #include <stdlib.h> typedef int E_T; void sort( E_T *array, int l, int r ); int main( void ) { E_T a[1000]={590,96,132,319,726,598,695,996,979,533,656,264,903,431,256,10,542,42,504,867,694,729,42,40,920,774,81,183,366,52,166,605,518,986,108,23,382,221,206,848,752,780,952,146,392,66,580,336,379,551,494,612,379,272,697,981,473,73,883,859,842,624,454,334,28,836,993,787,594,662,351,392,975,398,742,563,752,889,475,629,741,753,364,53,273,926,509,755,151,660,527,201,816,677,374,329,722,808,266,739,403,65,961,325,215,644,682,57,166,700,269,633,818,121,514,414,735,338,727,41,803,486,45,465,712,129,517,471,224,757,15,895,721,351,932,616,297,932,253,828,763,612,586,828,898,358,560,734,766,700,661,561,997,199,989,795,596,982,843,608,645,468,384,622,635,752,527,431,836,608,773,198,311,512,187,197,222,96,334,711,237,284,760,2,628,376,824,383,937,315,376,324,658,883,429,177,570,42,76,211,473,480,851,70,217,369,662,926,358,359,420,623,197,139,1,19,81,752,996,148,82,449,507,707,191,645,422,632,429,79,506,667,872,979,312,771,297,157,133,5,361,404,778,407,102,392,494,309,531,760,171,523,781,319,158,580,963,319,619,251,221,494,41,409,952,769,122,678,8,238,622,91,848,110,656,815,279,715,649,536,179,990,691,364,962,869,385,273,182,403,189,346,593,232,577,155,426,674,771,591,446,676,451,415,155,822,982,790,696,325,472,331,481,268,654,647,426,659,306,848,386,320,50,885,138,547,234,522,261,447,425,894,114,685,538,875,944,42,133,558,267,749,805,368,684,274,834,932,613,722,642,338,828,422,339,955,15,241,573,398,218,998,6,858,925,491,637,173,370,740,467,533,773,595,855,285,293,918,225,666,138,705,945,814,559,758,641,229,633,194,912,711,312,706,113,341,431,93,99,214,375,44,794,716,641,711,126,459,603,274,802,418,257,801,831,550,648,535,218,35,736,829,476,52,120,104,25,853,591,876,108,406,928,240,136,459,700,98,721,538,935,109,220,109,100,976,517,320,730,657,88,285,707,578,482,822,938,426,544,491,469,695,144,661,252,314,735,2,425,460,578,602,96,584,353,776,737,48,817,406,619,189,714,546,107,257,584,621,853,63,917,7,610,254,591,859,596,178,277,704,462,115,232,538,455,858,824,118,112,609,381,54,763,122,543,204,903,691,200,785,514,104,304,504,126,801,593,383,999,438,436,278,563,161,283,436,403,179,401,646,42,329,687,552,27,912,937,297,936,247,880,458,56,796,153,672,304,665,966,122,526,864,795,325,426,541,699,886,821,795,604,991,54,122,281,390,907,176,239,986,453,373,474,680,872,83,288,853,377,181,518,932,496,151,458,26,848,439,977,753,917,115,169,442,13,283,74,790,82,920,777,247,111,830,429,66,681,837,589,744,144,766,729,941,22,692,441,992,575,656,515,333,618,706,281,871,727,885,273,250,793,155,559,16,206,152,560,201,924,91,312,161,118,914,607,245,704,806,512,878,455,999,10,961,413,601,570,38,643,775,992,0,71,278,809,740,673,190,213,869,601,108,466,39,915,363,529,675,469,973,887,995,280,357,759,943,45,222,653,286,314,649,870,241,437,851,967,60,857,869,99,894,199,777,505,436,94,233,933,105,965,601,230,776,713,928,888,853,283,459,595,385,3,980,147,444,203,665,457,200,741,895,530,836,534,680,871,758,87,883,861,837,402,331,653,5,58,690,358,468,918,360,442,149,79,318,895,819,936,136,383,504,486,283,882,222,143,331,450,302,968,753,183,216,242,658,20,586,28,931,962,604,535,667,155,819,86,994,786,540,764,377,297,204,355,362,363,853,354,706,811,716,982,827,605,785,973,311,644,311,743,17,91,508,205,518,447,987,439,645,324,288,304,731,504,884,557,770,781,498,286,403,43,0,484,937,661,362,876,176,283,555,962,94,968,470,562,281,245,829,498,225,77,783,964,399,754,479,403,158,8,426,548,488,377,810,768,156,257,497,718,71,146,351,86,385,651,512,491,714,388,944,960,765,750,409,407,43,481,814,918,555,821,648,308,720,564,153,770,636,824,479,596,740,426,548,690,891,795,627,138,275,238,749,697,537,573,821,858,257,635,65,926,467,218,443,221,716,38,390,299,375,698,332,558,389,529,647,223,691,584,847,207,580,834,548,524,662,915,717,261,561,314,64,498,13,640,522,503,859,214,769,570,847,743,352,979,852,81,275,772,409,642,636,889,707,432,533,58,339,673,455,119,401,364,690,811,321,56,406,859,925,420,122,977,907,423,803,492,425,617}; //time_t tm1,tm2; /* srand( ( unsigned long )time( NULL ) ); for( int i = 0; i < 10; ++i ) { a[ i ] = rand() % 1000; printf( "%d ",a[ i ] ); } printf( "\n" );*/ sort( a, 0, 1000 ); for( int i = 0; i < 1000; ++i ) printf( "%d ", a[ i ] ); printf( "\n" ); printf( "\n" ); printf( "\n" ); return 0; } void sort( E_T *array, int l, int r ) { int minix, maxix; int R; int c; // int f; int s; s = 0; if( l >= r ) return; for( int ix = l; ix < r -1; ix++ ) { if( array[ ix ] <= array[ ix + 1 ] ) { s++; if( s < 0 ) break; } else if( array[ ix ] > array[ix + 1 ] ) { s--; if( s > 0 ) break; } else break; } c=0; if( s == r - l - 1 ) { printf("jiao huan %d ci\n",c); return; } else if( s == l - r + 1) { R = r - 1; for(; l < R; l++,R-- ) { c++; E_T temp = array[ l ]; array[ l ] = array[ R ]; array[ R ] = temp; } } else { for( ; l < r; ++l, --r ) { //f = 0; for( minix = l + 1, maxix = r - 2, R = r - 1; minix <= R; ++minix, --maxix ) { if( array[ l ] > array[ minix ] ) { c++; // f++; E_T temp = array[ l ]; array[ l ] = array[ minix ]; array[ minix ] = temp; } if( array[ R ] < array[ maxix ] ) { c++; //f++; E_T temp = array[ R ]; array[ R ] = array[ maxix ]; array[ maxix ] = temp; } } /* if( f == 0 ) sort( array, l + 1, r - 1 );*/ } } printf("jiao huan %d ci\n",c); }
[此贴子已经被作者于2018-5-25 20:09编辑过]