排序算法
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
using namespace std;
typedef int ElementType;
void baboSort ( ElementType A[ ] , int N) {
for ( int i = 0 ; i < N; i++ ) {
int flag = 0 ;
for ( int j = i; j < N; j++ ) {
if ( A[ i] > A[ j] ) {
swap ( A[ i] , A[ j] ) ;
flag = 1 ;
}
}
if ( flag == 0 ) break ;
}
}
void InsertionSort ( ElementType A[ ] , int N) {
int P, i;
ElementType Tmp;
for ( P = 1 ; P < N; P++ ) {
Tmp = A[ P] ;
for ( i = P; i > 0 && A[ i - 1 ] > Tmp; i-- )
A[ i] = A[ i - 1 ] ;
A[ i] = Tmp;
}
}
void ShellSort ( ElementType A[ ] , int N)
{
int Si, D, P, i;
ElementType Tmp;
int Sedgewick[ ] = { 929 , 505 , 209 , 109 , 41 , 19 , 5 , 1 , 0 } ;
for ( Si = 0 ; Sedgewick[ Si] >= N; Si++ ) ;
for ( D = Sedgewick[ Si] ; D > 0 ; D = Sedgewick[ ++ Si] )
for ( P = D; P < N; P++ ) {
Tmp = A[ P] ;
for ( i = P; i >= D && A[ i - D] > Tmp; i - = D)
A[ i] = A[ i - D] ;
A[ i] = Tmp;
}
}
void Swap ( ElementType* a, ElementType* b)
{
ElementType t = * a; * a = * b; * b = t;
}
void PercDown ( ElementType A[ ] , int p, int N)
{
int Parent, Child;
ElementType X;
X = A[ p] ;
for ( Parent = p; ( Parent * 2 + 1 ) < N; Parent = Child) {
Child = Parent * 2 + 1 ;
if ( ( Child != N - 1 ) && ( A[ Child] < A[ Child + 1 ] ) )
Child++ ;
if ( X >= A[ Child] ) break ;
else
A[ Parent] = A[ Child] ;
}
A[ Parent] = X;
}
void HeapSort ( ElementType A[ ] , int N)
{
int i;
for ( i = N / 2 - 1 ; i >= 0 ; i-- )
PercDown ( A, i, N) ;
for ( i = N - 1 ; i > 0 ; i-- ) {
Swap ( & A[ 0 ] , & A[ i] ) ;
PercDown ( A, 0 , i) ;
}
}
void Merge ( ElementType A[ ] , ElementType TmpA[ ] , int L, int R, int RightEnd)
{
int LeftEnd, NumElements, Tmp;
int i;
LeftEnd = R - 1 ;
Tmp = L;
NumElements = RightEnd - L + 1 ;
while ( L <= LeftEnd && R <= RightEnd) {
if ( A[ L] <= A[ R] )
TmpA[ Tmp++ ] = A[ L++ ] ;
else
TmpA[ Tmp++ ] = A[ R++ ] ;
}
while ( L <= LeftEnd)
TmpA[ Tmp++ ] = A[ L++ ] ;
while ( R <= RightEnd)
TmpA[ Tmp++ ] = A[ R++ ] ;
for ( i = 0 ; i < NumElements; i++ , RightEnd-- )
A[ RightEnd] = TmpA[ RightEnd] ;
}
void Msort ( ElementType A[ ] , ElementType TmpA[ ] , int L, int RightEnd)
{
int Center;
if ( L < RightEnd) {
Center = ( L + RightEnd) / 2 ;
Msort ( A, TmpA, L, Center) ;
Msort ( A, TmpA, Center + 1 , RightEnd) ;
Merge ( A, TmpA, L, Center + 1 , RightEnd) ;
}
}
void MergeSort ( ElementType A[ ] , int N)
{
ElementType* TmpA;
TmpA = ( ElementType* ) malloc ( N * sizeof ( ElementType) ) ;
if ( TmpA != NULL ) {
Msort ( A, TmpA, 0 , N - 1 ) ;
free ( TmpA) ;
}
else printf ( "空间不足" ) ;
}
void Merge_pass ( ElementType A[ ] , ElementType TmpA[ ] , int N, int length)
{
int i, j;
for ( i = 0 ; i <= N - 2 * length; i + = 2 * length)
Merge ( A, TmpA, i, i + length, i + 2 * length - 1 ) ;
if ( i + length < N)
Merge ( A, TmpA, i, i + length, N - 1 ) ;
else
for ( j = i; j < N; j++ ) TmpA[ j] = A[ j] ;
}
void Merge_Sort ( ElementType A[ ] , int N)
{
int length;
ElementType* TmpA;
length = 1 ;
TmpA = ( ElementType* ) malloc ( N * sizeof ( ElementType) ) ;
if ( TmpA != NULL ) {
while ( length < N) {
Merge_pass ( A, TmpA, N, length) ;
length * = 2 ;
Merge_pass ( TmpA, A, N, length) ;
length * = 2 ;
}
free ( TmpA) ;
}
else printf ( "Space is not enough" ) ;
}
int main ( ) {
int N= 5 ;
ElementType A[ 100 ] = { 10 , 8 , 5 , 9 , 2 } ;
cout<< "冒泡排序结果为:" << endl;
baboSort ( A, N) ;
for ( int i= 0 ; i< N; i++ ) {
cout<< A[ i] << ' ' ;
}
cout<< endl;
cout<< "插入排序结果为:" << endl;
InsertionSort ( A, N) ;
for ( int i= 0 ; i< N; i++ ) {
cout<< A[ i] << ' ' ;
}
cout<< endl;
cout<< "希尔排序结果为:" << endl;
ShellSort ( A, N) ;
for ( int i= 0 ; i< N; i++ ) {
cout<< A[ i] << ' ' ;
}
cout<< endl;
cout<< "堆排序结果为:" << endl;
HeapSort ( A, N) ;
for ( int i= 0 ; i< N; i++ ) {
cout<< A[ i] << ' ' ;
}
cout<< endl;
cout<< "递归实现归并排序排序结果为:" << endl;
MergeSort ( A, N) ;
for ( int i= 0 ; i< N; i++ ) {
cout<< A[ i] << ' ' ;
}
cout<< endl;
cout<< "循环实现归并排序排序结果为:" << endl;
Merge_Sort ( A, N) ;
for ( int i= 0 ; i< N; i++ ) {
cout<< A[ i] << ' ' ;
}
cout<< endl;
return 0 ;
}