//
This function is used to sort numbers
#include " stdio.h "
#include " stdlib.h "
#include " conio.h "
#define SIZE 5
#define EQ(a,b) ((a)==(b))
#define LT(a,b) ((a)<(b))
#define LQ(a,b) ((a)>(b))
#define Exchange(a,b) {a = a ^ b ; b = a ^ b ; a = a ^ b;}
// #define DEBUG
void strInsertSort ( int * list) // Straight Insertion Sort
{
for ( int i = 1 ; i < SIZE ; i ++ )
{
if ( LT(list[i],list[i - 1 ] )) // if the number is smaller than the number before it ,it need to insert
{
int temp = list[i] ;
list[i] = list[i - 1 ] ; // let the elem move back
for ( int j = i - 2 ;LT(temp , list[j]) && j >= 0 ; j -- )
list [j + 1 ] = list [j];
list[j + 1 ] = temp;
}
}
}
void EbullitionSort ( int * list) // Ebulliton Sort
{
int flag = 1 ;
for ( int i = 0 ;i < SIZE && flag ; i ++ )
for ( int j = i + 1 ; j < SIZE ; j ++ )
{
flag = 0 ;
if ( list[i] < list[j] )
{
flag = 1 ;
Exchange ( list[i] ,list[j] );
#ifdef DEBUG
printf ( " %d%d " ,list[i] ,list[j]);
getch() ;
#endif
}
}
}
void BInsertSort ( int * list ) // Binary Insert Sort
{
for ( int i = 0 ; i < SIZE ; i ++ )
{
int temp = list [i] ;
int low = 0 ;
int high = i - 1 ;
while ( low <= high )
{
int m = (high + low) >> 1 ;
if ( LT ( temp ,list[m] ))
high = m - 1 ;
else
low = m + 1 ;
}
for ( int j = i - 1 ; j >= high + 1 ; j -- )
list[j + 1 ] = list[j] ;
list[high + 1 ] = temp ;
}
}
void shellInsert ( int * list , int dk ) // ShellInsert in shell Sort
{
for ( int i = dk ; i < SIZE ; i ++ )
{
if ( LT ( list[i],list[i - dk] ) )
{
int temp = list[i] ;
for ( int j = i - dk ; LT(temp , list[j]) && j >= 0 ; j -= dk )
list[j + dk] = list[j] ;
list[j + dk] = temp ;
}
}
}
void shellInsert ( int * list ) // shellInsert
{
int dk[ 3 ] = { 5 , 3 , 1 } ;
for ( int i = 0 ; i < 3 ;i ++ )
shellInsert(list , dk[i]);
}
// Qiuck Sort
int Parttion ( int * list , int low , int high ) // split the list to two part
{
int temp = list[low] ; // let the number in low as pivotkey
int pivotkey = list[low] ;
while ( low < high )
{
while ( low < high && list[high] >= pivotkey ) high -- ;
list[low] = list[high] ;
while ( low < high && list[low] <= pivotkey ) low ++ ;
list[high] = list[low] ;
}
list[low] = temp ;
return low ;
}
void QiuckSort ( int * list , int low , int high ) // qiuck sort
{
if ( low < high )
{
int pivotloc = Parttion ( list , low ,high );
QiuckSort ( list , low ,pivotloc - 1 ) ;
QiuckSort ( list ,pivotloc + 1 ,high) ;
}
}
// Simple Select Sort
int SelectMinKey( int * list , int i ) // Find the MInest number in list begin with i
{
int min = i ;
for ( int j = i + 1 ; j < SIZE ;j ++ )
if ( list[j] < list[j - 1 ] ) min = j;
return min ;
}
void SelectSort ( int * list )
{
for ( int i = 0 ; i < SIZE ; i ++ )
{
int j = SelectMinKey( list ,i );
if ( j != i )
{
// Exchange(list[i],list[j]);
int temp = list[i] ;
list[i] = list[j];
list[j] = temp ;
}
}
}
// Heap Sort
void HeapAdjust ( int * list , int s , int m) // let the number from s to m to be heap
{
int temp = list [s] ;
for ( int j = 2 * s + 1 ; j <= m ;j *= 2 )
{
if ( j < m && LT( list[j],list[j + 1 ] ) )
j ++ ;
if ( LQ(temp , list[j] ) )
break ;
list [s] = list [j] ;
s = j ;
}
list [s] = temp ;
}
// HeapSort
void HeapSort ( int * list )
{
for ( int i = (SIZE / 2 - 1 ); i >= 0 ; i -- )
HeapAdjust ( list , i , SIZE - 1 );
for ( int j = SIZE - 1 ; j > 0 ; j -- )
{
int temp = list [ 0 ] ;
list [ 0 ] = list [j] ;
list [j] = temp ;
HeapAdjust ( list , 0 , j - 1 );
}
}
#include " stdio.h "
#include " stdlib.h "
#include " conio.h "
#define SIZE 5
#define EQ(a,b) ((a)==(b))
#define LT(a,b) ((a)<(b))
#define LQ(a,b) ((a)>(b))
#define Exchange(a,b) {a = a ^ b ; b = a ^ b ; a = a ^ b;}
// #define DEBUG
void strInsertSort ( int * list) // Straight Insertion Sort
{
for ( int i = 1 ; i < SIZE ; i ++ )
{
if ( LT(list[i],list[i - 1 ] )) // if the number is smaller than the number before it ,it need to insert
{
int temp = list[i] ;
list[i] = list[i - 1 ] ; // let the elem move back
for ( int j = i - 2 ;LT(temp , list[j]) && j >= 0 ; j -- )
list [j + 1 ] = list [j];
list[j + 1 ] = temp;
}
}
}
void EbullitionSort ( int * list) // Ebulliton Sort
{
int flag = 1 ;
for ( int i = 0 ;i < SIZE && flag ; i ++ )
for ( int j = i + 1 ; j < SIZE ; j ++ )
{
flag = 0 ;
if ( list[i] < list[j] )
{
flag = 1 ;
Exchange ( list[i] ,list[j] );
#ifdef DEBUG
printf ( " %d%d " ,list[i] ,list[j]);
getch() ;
#endif
}
}
}
void BInsertSort ( int * list ) // Binary Insert Sort
{
for ( int i = 0 ; i < SIZE ; i ++ )
{
int temp = list [i] ;
int low = 0 ;
int high = i - 1 ;
while ( low <= high )
{
int m = (high + low) >> 1 ;
if ( LT ( temp ,list[m] ))
high = m - 1 ;
else
low = m + 1 ;
}
for ( int j = i - 1 ; j >= high + 1 ; j -- )
list[j + 1 ] = list[j] ;
list[high + 1 ] = temp ;
}
}
void shellInsert ( int * list , int dk ) // ShellInsert in shell Sort
{
for ( int i = dk ; i < SIZE ; i ++ )
{
if ( LT ( list[i],list[i - dk] ) )
{
int temp = list[i] ;
for ( int j = i - dk ; LT(temp , list[j]) && j >= 0 ; j -= dk )
list[j + dk] = list[j] ;
list[j + dk] = temp ;
}
}
}
void shellInsert ( int * list ) // shellInsert
{
int dk[ 3 ] = { 5 , 3 , 1 } ;
for ( int i = 0 ; i < 3 ;i ++ )
shellInsert(list , dk[i]);
}
// Qiuck Sort
int Parttion ( int * list , int low , int high ) // split the list to two part
{
int temp = list[low] ; // let the number in low as pivotkey
int pivotkey = list[low] ;
while ( low < high )
{
while ( low < high && list[high] >= pivotkey ) high -- ;
list[low] = list[high] ;
while ( low < high && list[low] <= pivotkey ) low ++ ;
list[high] = list[low] ;
}
list[low] = temp ;
return low ;
}
void QiuckSort ( int * list , int low , int high ) // qiuck sort
{
if ( low < high )
{
int pivotloc = Parttion ( list , low ,high );
QiuckSort ( list , low ,pivotloc - 1 ) ;
QiuckSort ( list ,pivotloc + 1 ,high) ;
}
}
// Simple Select Sort
int SelectMinKey( int * list , int i ) // Find the MInest number in list begin with i
{
int min = i ;
for ( int j = i + 1 ; j < SIZE ;j ++ )
if ( list[j] < list[j - 1 ] ) min = j;
return min ;
}
void SelectSort ( int * list )
{
for ( int i = 0 ; i < SIZE ; i ++ )
{
int j = SelectMinKey( list ,i );
if ( j != i )
{
// Exchange(list[i],list[j]);
int temp = list[i] ;
list[i] = list[j];
list[j] = temp ;
}
}
}
// Heap Sort
void HeapAdjust ( int * list , int s , int m) // let the number from s to m to be heap
{
int temp = list [s] ;
for ( int j = 2 * s + 1 ; j <= m ;j *= 2 )
{
if ( j < m && LT( list[j],list[j + 1 ] ) )
j ++ ;
if ( LQ(temp , list[j] ) )
break ;
list [s] = list [j] ;
s = j ;
}
list [s] = temp ;
}
// HeapSort
void HeapSort ( int * list )
{
for ( int i = (SIZE / 2 - 1 ); i >= 0 ; i -- )
HeapAdjust ( list , i , SIZE - 1 );
for ( int j = SIZE - 1 ; j > 0 ; j -- )
{
int temp = list [ 0 ] ;
list [ 0 ] = list [j] ;
list [j] = temp ;
HeapAdjust ( list , 0 , j - 1 );
}
}