排序完成。
3> 快速排序该位置后面的序列。
3. 程序实现
/*
* 五种排序思维基本实现
* */
# include
# define LEN 10
/* 冒泡排序
* */
void bubble_sort( int *base, int len );
/* 插入排序
* */
void insert_sort( int *base, int len );
/* 选择排序
* */
void select_sort( int *base, int len );
/* 归并排序
* */
void merger_sort( int *base, int start, int end );
/* 快速排序
* */
void quick_sort( int *base, int start, int end );
int
main( void )
{
void print( int *base, int len );
int tmp[LEN] = { 55, 32, -1, -12, 15, 4, 7, 0, 15, 40 };
//bubble_sort( tmp, LEN );
//insert_sort( tmp, LEN );
//select_sort( tmp, LEN );
//merger_sort( tmp, 0, LEN - 1 );
quick_sort( tmp, 0, LEN - 1 );
print( tmp, LEN );
return 0;
}
/*
* function: print the valuse arr
* */
void
print( int *base, int len )
{
while (len-- > 0)
printf( "%5d", *base++ );
printf( "\n" );
return;
}
/* function bubble
* */
void
bubble_sort( int *base, int len )
{
int i, j, tmp;
/* 1. Find the current bubble point
* first: len - 1 seat
* last: 1 seat
* 0 seat cannot bubble
*
* 2. Bubble by some method
* */
for (i = len -1; i > 0; i--)
for (j = 0; j < i; j++)
if (base[j] > base[j+1]){
tmp = base[j];
base[j] = base[j+1];
base[j+1] = tmp;
}
return;
}
/* function insert;
* */
void
insert_sort( int *base, int len )
{
int i, j, tmp;
for (i = 1; i < len; i++){
j = i - 1;
tmp = base[i];
/*
* Find insert point and move the array values
* Base[j+1] are always a logical empty element
* */
while (j >= 0 && tmp < base[j])
base[j+1] = base[j--];
base[j+1] = tmp;
}
return;
}
/* function select
* */
void
select_sort( int *base, int len ){
int i, j, tmp;
/* Used current element compare's with others
* Select the min value write's in the current element
* */
for (i = 0; i < len - 1; i++){
for (j = i + 1; j < len; j++)
if (base[i] > base[j]){
tmp = base[i];
base[i] = base[j];
base[j] = tmp;
}
//printf( "NO.%d select the min value is %3d , arr is ", i, base[i] );
//print( base, len );
}
return;
}
/* function merger
* */
void
merger_sort( int *base, int start, int end )
{
void merger( int *base, int start, int mid, int end );
void print_sub( int *base, int start, int end );
if (start < end){
/* Divide */
int mid = ( start + end ) / 2;
//printf( "Sort: ( %d - %d ) AND ( %d - %d )", start, mid, mid+1, end );
//print_sub( base, start, end );
/* Conguer */
merger_sort( base, start, mid );
merger_sort( base, mid + 1, end );
/* combine */
merger( base, start, mid, end);
//printf( "merger: ( %d - %d ) AND ( %d - %d )", start, mid, mid+1, end );
//print_sub( base, start, end );
}
}
/* sub function to merger_sort
* */
void
merger( int *base, int start, int mid, int end ){
/* cnt1 is left lenght, cnt2 is right length */
int cnt1 = mid - start + 1;
int cnt2 = end - mid;
int left[LEN], right[LEN];
int i,j,k;
/* Copy data from base array to left and right array
* */
for ( i = 0, k = start; i < cnt1; i++, k++ )
left[i] = base[k];
for ( i = 0, k = mid + 1; i < cnt2; i++, k++ )
right[i] = base[k];
/* Compare left and right array's data, the smaller insert to base array
* */
for (i = 0, j = 0, k = start; i < cnt1 && j < cnt2; k++)
if (left[i] <= right[j])
base[k] = left[i++];
else
base[k] = right[j++];
/* left[] is not exhausted*/
while (i < cnt1)
base[k++] = left[i++];
/* right[] is not exhausted*/
while (j < cnt2)
base[k++] = right[j++];
return;
}
/* function: used start and end columns, printf a sub array from a array.
* */
void print_sub( int *base, int start, int end )
{
int i;
for (i = start; i != end + 1; i++)
printf( "%3d", base[i]);
printf( "\n" );
return;
}
/* function quick
* */
void
quick_sort( int *base, int start, int end )
{
void print_sub( int *base, int start, int end );
if (end > start){
int partition( int *base, int start, int end );
/* After find partition point, partition base array */
int pos = partition( base, start, end );
//printf( "the partition value is : ( %d ) ", base[pos] );
//printf( "sort(%3d,%3d) AND (%3d,%3d) \n", start, pos-1, pos+1, end );
quick_sort( base, start, pos - 1 );
quick_sort( base, pos + 1, end );
}
}
/* sub function to quick_sort
* */
int
partition( int *base, int start, int end )
{
int tmp = base[start];
while (end > start){
/* first right */
while (end > start)
if( base[end] < tmp ) {
base[start++] = base[end];
break;
} else
end--;
/*second left */
while (end > start)
if (base[start] > tmp) {
base[end--] = base[start];
break;
}else
start++;
}
base[end] = tmp;
return end;
}