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_adjust( int *a, int i, int headsize );

void Heap_sort( int *a, int count )//堆排序 
{
	int headsize = count - 1, i;
	Build_heap( a, headsize );
	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-- )
	{
		Heap_adjust( a, i, headsize );
	}
}

void Heap_adjust( int *a, int i, int headsize )
{
	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] );
		Heap_adjust( a, max, headsize );
	}
}

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 ); 
}  

int Max_radix = 1000;//設元素最大為1000; 

void Radix_count( int *a, int begin, int end, int radix );

int Get_digit( int a, int radix )//radix為基數,當從個位開始排時為1, 10, 100...到最高位,當從最高位開始時相反 
{
	return ( ( a / radix ) % 10 );
}

void Radix_sort( int *a, int count )//基數排序 
{
	int radix = Max_radix;
	Radix_count( a, 0, count - 1, radix );
}

void Radix_count( int *a, int begin, int end, int radix )
{
	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 )
			Radix_count( a, p, q, radix / 10 );
	}
}

double Min_radix = 0.00001;//設桶排序最小為0.0001 
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 Get_decimal( float a, int radix )//radix為基數,當從個位開始排時為1, 10, 100...到最高位,當從最高位開始時相反 
{
	int i = 10;
	while( a < 1 && i <= radix)
	{
		a *= 10;
		i *= 10;
	}
	return a;
}

void Bucket_sort( float *a, int count )//桶排序 
{
	int radix = 10;
	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;
}