<一>插入排序
1.直接插入排序
无哨兵
#include <stdio.h>
#define MAX 10
void insertSort ( int * a, int n)
{
int buf;
int i, j;
for ( i= 1 ; i< n; i++ )
{
if ( a[ i] < a[ i- 1 ] )
{
buf= a[ i] ;
for ( j= i- 1 ; buf< a[ j] && j>= 0 ; j-- )
a[ j+ 1 ] = a[ j] ;
a[ j+ 1 ] = buf;
}
}
}
int main ( )
{
int a[ MAX] ;
printf ( "Please enter numbers:\n" ) ;
int num;
scanf ( "%d" , & num) ;
int i= 0 ;
for ( i= 0 ; i< num; i++ )
{
printf ( "Please enter a[%d]:\n" , i) ;
scanf ( "%d" , a+ i) ;
}
insertSort ( a, num) ;
for ( i= 0 ; i< num; i++ )
printf ( "%d" , a[ i] ) ;
return 0 ;
}
有哨兵
#include <stdio.h>
#define MAX 10
void insertSort ( int * a, int n)
{
int i, j;
for ( i= 2 ; i< n+ 1 ; i++ )
{
if ( a[ i] < a[ i- 1 ] )
{
a[ 0 ] = a[ i] ;
for ( j= i- 1 ; a[ j] > a[ 0 ] ; j-- )
a[ j+ 1 ] = a[ j] ;
a[ j+ 1 ] = a[ 0 ] ;
}
}
}
int main ( )
{
int num;
int i, j;
int a[ MAX] ;
printf ( "enter num:\n" ) ;
scanf ( "%d" , & num) ;
for ( i= 1 ; i< num+ 1 ; i++ )
{
printf ( "enter a[%d]:\n" , i) ;
scanf ( "%d" , a+ i) ;
}
insertSort ( a, num) ;
printf ( "sorted:\n" ) ;
for ( i= 1 ; i< num+ 1 ; i++ )
{
printf ( "%d " , a[ i] ) ;
}
return 0 ;
}
2.折半排序和希尔排序
#include <stdio.h>
#include <time.h>
#define MAX 10
void insertSort ( int * a, int n)
{
int i, j;
for ( i= 2 ; i< n+ 1 ; i++ )
{
if ( a[ i] < a[ i- 1 ] )
{
a[ 0 ] = a[ i] ;
for ( j= i- 1 ; a[ 0 ] < a[ j] ; j-- )
a[ j+ 1 ] = a[ j] ;
a[ j+ 1 ] = a[ 0 ] ;
}
}
}
void zhebanSort ( int * a, int n)
{
int i, j, high, low, mid;
for ( i= 2 ; i< n+ 1 ; i++ )
{
low= 1 ;
high= i- 1 ;
a[ 0 ] = a[ i] ;
while ( low<= high)
{
mid= ( low+ high) / 2 ;
if ( a[ 0 ] < a[ mid] )
high= mid- 1 ;
else
low= mid+ 1 ;
}
for ( j= i- 1 ; j> high; j-- )
a[ j+ 1 ] = a[ j] ;
a[ high+ 1 ] = a[ 0 ] ;
}
}
void shellPass ( int * a, int n, int step)
{
int i, j;
for ( i= 1 + step; i< n+ 1 ; i++ )
{
a[ 0 ] = a[ i] ;
j= i- step;
while ( j> 0 && a[ 0 ] < a[ j] )
{
a[ j+ step] = a[ j] ;
j= j- step;
}
a[ j+ step] = a[ 0 ] ;
}
}
void shellSort ( int * a, int n)
{
int num= n;
while ( ( n= n/ 2 ) != 0 )
shellPass ( a, num, n) ;
}
int main ( )
{
clock_t start, finish;
start= clock ( ) ;
int i, j;
int num;
int a[ MAX] ;
printf ( "num:\n" ) ;
scanf ( "%d" , & num) ;
for ( i= 1 ; i< num+ 1 ; i++ )
{
printf ( "a[%d]:\n" , i) ;
scanf ( "%d" , a+ i) ;
}
shellSort ( a, num) ;
finish= clock ( ) ;
double time= ( double ) ( finish- start) ;
printf ( "************************\n排序耗时:%f\n" , time) ;
for ( i= 1 ; i< num+ 1 ; i++ )
printf ( "%d " , a[ i] ) ;
return 0 ;
}
<二>交换排序
给爷冒个泡
void bubbleSort1 ( int * a, int n)
{
int i, j;
for ( i= 1 ; i< n; i++ )
for ( j= 1 ; j< n- i+ 2 ; j++ )
if ( a[ j] < a[ j- 1 ] )
{
a[ 0 ] = a[ j] ;
a[ j] = a[ j- 1 ] ;
a[ j- 1 ] = a[ 0 ] ;
}
}
void bubbleSort2 ( int * a, int n)
{
int i= 1 , j, flag;
while ( i< n- 1 )
{
flag= 0 ;
for ( j= 1 ; j< n- i+ 2 ; j++ )
if ( a[ j] < a[ j- 1 ] )
{
a[ 0 ] = a[ j] ;
a[ j] = a[ j- 1 ] ;
a[ j- 1 ] = a[ 0 ] ;
flag= 1 ;
}
if ( flag)
i++ ;
else
break ;
快速排序
#include <stdio.h>
#include <time.h>
#define MAX 10
int partition ( int * a, int i, int j)
{
while ( i< j)
{
while ( i< j && a[ i] <= a[ j] )
j-- ;
a[ 0 ] = a[ i] ;
a[ i] = a[ j] ;
a[ j] = a[ 0 ] ;
while ( i< j && a[ i] <= a[ j] )
i++ ;
a[ 0 ] = a[ i] ;
a[ i] = a[ j] ;
a[ j] = a[ 0 ] ;
}
return i;
}
void qkSort ( int * a, int b, int e)
{
if ( b< e)
{
int k= partition ( a, b, e) ;
qkSort ( a, b, k- 1 ) ;
qkSort ( a, k+ 1 , e) ;
}
}
int main ( )
{
clock_t start, finish;
start= clock ( ) ;
int i, j;
int num;
int a[ MAX] ;
printf ( "num:\n" ) ;
scanf ( "%d" , & num) ;
for ( i= 1 ; i< num+ 1 ; i++ )
{
printf ( "a[%d]:\n" , i) ;
scanf ( "%d" , a+ i) ;
}
qkSort ( a, 1 , num) ;
finish= clock ( ) ;
double time= ( double ) ( finish- start) ;
printf ( "************************\n排序耗时:%f\n" , time) ;
for ( i= 1 ; i< num+ 1 ; i++ )
printf ( "%d " , a[ i] ) ;
return 0 ;
}