# 对九种排序算法的实现

195人阅读 评论(1)
关键是对算法的实现,写的不是特别严谨,有错误望指出.
<pre name="code" class="cpp">#include <stdio.h>
#include <memory.h>
#include <math.h>

#define INF 99999999//无穷大

void swap(int *a, int *b);

void Insert_sort(int *a, int count)//插入排序
{
int k;
int i, j;
for(i = 1;i < count;i++ )
{
k = a[i];
j = i - 1;
while( j >= 0 && a[j] > k )
{
a[j + 1] = a[j];
j--;
}
a[j + 1] = k;
}
}

void Bubble_sort( int *a, int count )//冒泡排序
{
int i, j;
for( i = 0;i < count;i++ )
for( j = count - 1;j > i;j-- )
if( a[j - 1] > a[j] )
{
if( a[ j - 1 ] != a[j] )
swap( &a[ j - 1 ], &a[j] );
}

}

void Select_sort( int *a, int count)//选择排序
{
int i, j;
int min;
for( i = 0;i < count - 1;i++ )
{
min = i;
for( j = i + 1;j < count;j++ )
if( a[min] > a[j] )
{
min = j;
}
if( a[min] != a[i] )
swap( &a[min], &a[i] );
}
}

void Recursive_mergesort( int *a, int p, int q );
void Merge( int *a, int p, int m, int q );

void Merge_sort( int *a, int count )//归并排序
{
Recursive_mergesort( a, 0, count - 1 );
}

void Recursive_mergesort( int *a, int p, int q )
{
if( p < q )
{
int m = ( p + q ) / 2;
Recursive_mergesort( a, p, m );
Recursive_mergesort( a, m + 1, q );
Merge( a, p, m, q );
}
}

void Merge( int *a, int p, int m, int q )
{
int Llength = m - p + 1;
int Rlength = q - m;
int *L = ( int * )malloc( ( Llength + 1 ) * sizeof( int ) );
int *R = ( int * )malloc( ( Rlength + 1 ) * sizeof( int ) );
int i, j;
for( i = 0;i < Llength;i++ )
{
L[i] = a[ p + i ];
}
for( j = 0;j < Rlength;j++ )
{
R[j] = a[ m + j + 1 ];
}
L[Llength] = INF;
R[Rlength] = INF;
i = j = 0;
int k;
for( k = p;k <= q;k++ )
{
if( L[i] > R[j] )
{
a[k] = R[j];
j++;
}
else
{
a[k] = L[i];
i++;
}
}
free( L );
free( R );
}

void Recursive_quicksort( int *a, int p, int q );
int Partition( int *a, int p, int q );

void Quick_sort( int *a, int count )//快速排序
{
Recursive_quicksort( a, 0, count - 1 );
}

void Recursive_quicksort( int *a, int p, int q )
{
if( p < q )
{
int r = Partition( a, p, q );
Recursive_quicksort( a, p, r - 1 );
Recursive_quicksort( a, r + 1, q );
}
}

int Partition( int *a, int p, int q )
{
int i = p - 1, j;
int pivot = a[q];
for( j = p;j < q;j++ )
{
if( a[j] <= pivot )
{
i++;
if( a[i] != a[j] )
swap( &a[i], &a[j] );
}
}
if( a[ i + 1 ] != a[q] )
swap( &a[ i + 1 ], &a[q] );
return i + 1;
}

void Build_heap( int *a, int count );

void Heap_sort( int *a, int count )//堆排序
{
int headsize = count - 1, i;
for( i = headsize;i >= 0;i-- )
{
swap( &a[0], &a[i] );
Heap_adjust( a, 0, i -1 );
}
}

void Build_heap( int *a, int headsize )
{
int i;
for( i = ( headsize / 2 );i >= 0;i-- )
{
}
}

{
int lchild = 2 * i + 1;
int rchild = 2 * i + 2;
int max = i;
if( lchild <= headsize && a[lchild] > a[max] )
{
max = lchild;
}
if( rchild <= headsize && a[rchild] > a[max] )
{
max = rchild;
}
if( max != i )
{
swap( &a[max], &a[i] );
}
}

int range = 1000;//计数排序范围

void Count_sort( int *a, int count )//计数排序
{
int *b = ( int * )malloc( count * sizeof( int ) );
int *c = ( int * )malloc( ( range + 1 ) * sizeof( int ) );
memset( b, 0, sizeof(b) * sizeof( int ) );
memset( c, 0, sizeof(c) * sizeof( int ) );
int i;
for( i = 0;i < count;i++ )
c[a[i]]++;
for( i = 1;i < range + 1;i++ )
c[i] += c[ i - 1 ];
for( i = 0;i < count;i++ )
b[--c[a[i]]] = a[i];
for( i = 0;i < count;i++ )
a[i] = b[i];
free( b );
free( c );
}

{
return ( ( a / radix ) % 10 );
}

void Radix_sort( int *a, int count )//基数排序
{
}

{
int count[10] = {0};
int i, j;
int *bucket = ( int * ) malloc( ( end - begin + 1 ) * sizeof( int ) );
for( i = begin;i <= end;i++ )
{
count[ Get_digit( a[i], radix ) ]++;
}
for( i = 1;i < 10;i++ )
{
count[i] += count[ i - 1 ];
}
for( i = begin;i <= end;i++ )
{
j = Get_digit( a[i], radix );
bucket[ count[j] - 1 ] = a[i];
--count[j];
}
for( i = begin, j = 0;i <= end;i++, j++ )
a[i] = bucket[j];
free( bucket );
int p, q;
for( i = 0;i < 10;i++ )
{
if( i == 0 )
{
p = begin;
q = begin + count[i] - 1;
}
else
{
p = begin + count[ i - 1 ];
q = begin + count[i] - 1;
}
if( p < q && radix / 10 > 0 )
}
}

int MAX_LENGTH;
void Bucket_count( float *a, int begin, int end, int radix );

int Get_MAX_LENGTH( double a )
{
int i = 0;
while( a < 1 )
{
a *= 10;
i++;
}
return i;
}

{
int i = 10;
while( a < 1 && i <= radix)
{
a *= 10;
i *= 10;
}
return a;
}

void Bucket_sort( float *a, int count )//桶排序
{
Bucket_count( a, 0, count - 1, radix );
}

void Bucket_count( float *a, int begin, int end, int radix )
{
int count[10] = {0};
int i, j;
float *bucket = ( float * )malloc( ( end - begin + 1 ) * sizeof( float ) );
for( i = begin;i <= end;i++ )
{
count[ Get_decimal( a[i], radix ) ]++;
}
for( i = 1;i < 10;i++ )
{
count[i] += count[ i - 1 ];
}
for( i = begin;i <= end;i++ )
{
j = Get_decimal( a[i], radix );
bucket[ count[j] - 1 ] = a[i];
count[j]--;
}
for( i = begin,j = 0;i <= end;i++, j++ )
a[i] = bucket[j];
free( bucket );
int p, q;
for( i = 0;i < 10;i++ )
{
if( i == 0 )
{
p = begin;
q = begin + count[i] - 1;
}
else
{
p = begin + count[ i - 1 ];
q = begin + count[i] - 1;
}
if( p < q && radix <= pow ( 10, Get_MAX_LENGTH( Min_radix ) ) )
Bucket_count( a, p, q, radix * 10 );
}
}

void swap(int *a, int *b)
{
*a = *a + *b;
*b = *a - *b;
*a = *a - *b;
}

void Print_array(int *a, int count)
{
int i;
for( i = 0;i < count;i++ )
{
printf( "%d ", a[i] );
}
printf( "\n" );
}

void Print_float_array(float *a, int count)
{
int i;
for( i = 0;i < count;i++ )
{
printf( "%g ", a[i] );
}
printf( "\n" );
}

int main(int argc, char** argv) {
int a[] = {5, 8, 1, 3, 9, 0, 2, 19, 76, 43, 54, 78, 3, 5, 82, 13};
float b[] = {0.05, 0.08, 0.01, 0.03, 0.09, 0, 0.02, 0.19, 0.76, 0.43, 0.54, 0.78, 0.03, 0.05, 0.82, 0.13};
int sum = sizeof(a) / sizeof(int);
int float_sum = sizeof(b) / sizeof(float);
int i = 0;
printf( "请输入排序方式：\n1.插入排序 2.冒泡排序 3.选择排序 4.归并排序 5.快速排序\n6.堆排序 7.计数排序 8.基数排序 9.桶排序\n" );
scanf( "%d", &i );
switch(i)
{
case 1:
printf( "原始数据：\n" ); Print_array( a, sum ); printf( "排序后数据：\n" );
Insert_sort( a, sum); Print_array( a, sum ); break;
case 2:
printf( "原始数据：\n" ); Print_array( a, sum ); printf( "排序后数据：\n" );
Bubble_sort( a, sum); Print_array( a, sum ); break;
case 3:
printf( "原始数据：\n" ); Print_array( a, sum ); printf( "排序后数据：\n" );
Select_sort( a, sum); Print_array( a, sum ); break;
case 4:
printf( "原始数据：\n" ); Print_array( a, sum ); printf( "排序后数据：\n" );
Merge_sort( a, sum); Print_array( a, sum ); break;
case 5:
printf( "原始数据：\n" ); Print_array( a, sum ); printf( "排序后数据：\n" );
Quick_sort( a, sum); Print_array( a, sum ); break;
case 6:
printf( "原始数据：\n" ); Print_array( a, sum ); printf( "排序后数据：\n" );
Heap_sort( a, sum); Print_array( a, sum ); break;
case 7:
printf( "原始数据：\n" ); Print_array( a, sum ); printf( "排序后数据：\n" );
Count_sort( a, sum); Print_array( a, sum ); break;
case 8:
printf( "原始数据：\n" ); Print_array( a, sum ); printf( "排序后数据：\n" );
Radix_sort( a, sum); Print_array( a, sum ); break;
case 9:
printf( "原始数据：\n" ); Print_float_array( b, float_sum ); printf( "排序后数据：\n" );
Bucket_sort( b, float_sum ); Print_float_array( b, float_sum ); break;
default:
{
printf( "输入错误" );
exit(0);
}
}
return 0;
}

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：1766次
• 积分：96
• 等级：
• 排名：千里之外
• 原创：8篇
• 转载：0篇
• 译文：0篇
• 评论：1条
文章分类
文章存档
评论排行