基础排序算法演示

满脑子都是阶段/状态/决策……学无止境呀

发一点基本的排序算法,分成很多个文件写的,每个文件前面带了注释,一起发出来,结构如下:

基础排序算法文件结构

 Sort.h

头文件 

 main.c

主演示程序 

 printkeys.c

 数据输出

 ifcontinue.c

 演示控制

random.c 

 随机初始化数据

bubblesort.c 

 冒泡排序算法

quicksort.c 

 快速排序算法

 insort.c

 插入排序算法

 shellsort.c

 希尔排序算法

演示按 随机化数据->输出->排序->输出 的流程进行,算法很简单不想多说。

 

/* sort.h: 排序算法演示头文件 */  
#include 
< stdio.h >

#define  MAXN 100

typedef 
struct  
{
        
int key;
        
int data;
}
 Record;


/*

几种排序算法演示
by DaNmarner
Email: DaNmarner@gmail.com 

*/


#include 
" sort.h "

int  Random(Record r[], int  n);
int  printkeys(Record r[], int  n);
int  ifcontinue( void );
void  InsSort (Record r[], int  n);
void  ShellSort (Record[], int  n);
void  BubbleSort(Record r[], int  n);
void  QuickSort (Record r[], int  s, int  t);

int  main( void )
{
    Record r[MAXN];
    
int n=10;
    
    printf (
"Show sorts  by DaNmarner 2006.4 ");
    printf (
"How much record to be sorted ( n<100 ) ?");
    scanf (
"%d",&n);
    puts(
"");
       
    
//演示插入排序 
    puts("InsSort ready! ");
    ifcontinue();
    Random(r,n);
    puts(
"Before InsSort:");
    printkeys(r,n);
    InsSort(r,n);
    puts(
"After InsSort:");
    printkeys(r,n);
    puts(
"");
    
//插入排序结束
    
    
//演示希尔排序
    puts("ShellSort ready! ");
    ifcontinue(); 
    Random(r,n);
    puts(
"Before ShellSort:");
    printkeys(r,n);
    ShellSort(r,n);
    puts(
"After ShellSort:");
    printkeys(r,n);
    puts(
"");
    
//希尔排序结束 
    
    
//演示冒泡排序
    puts("BubbleSort ready! ");
    ifcontinue();   
    Random(r,n);
    puts(
"Before BubbleSort:");
    printkeys(r,n);
    BubbleSort(r,n);
    puts(
"After BubbleSort:");
    printkeys(r,n);
    puts(
"");
    
//冒泡排序结束 
    
    
//演示快速排序
    puts("QuickSort ready! ");
    ifcontinue();   
    Random(r,n);
    puts(
"Before QuickSort:");
    printkeys(r,n);
    QuickSort(r,
0,n-1);
    puts(
"After QuickSort:");
    printkeys(r,n);
    puts(
"");
    
//演示快速结束 
    
    
//演示堆排序
    puts("HeapSort ready! ");
    ifcontinue();   
    Random(r,n);
    puts(
"Before HeapSort:");
    printkeys(r,n);
    QuickSort(r,
0,n-1);
    puts(
"After HeapSort:");
    printkeys(r,n);
    puts(
"");
    
//演示堆结束 
     
    system(
"PAUSE");
    
return 0;
}



/* printkeys.c: 输出排序关键字 */  
#include 
" sort.h "

int  printkeys (Record r[], int  n)
{
    
int i;
    
for (i=0;i<n;i++)
        printf(
"%d ",r[i].key);
    printf (
" ");
    
return i;
}



/* ifcontinue.c: 控制是否继续演示 */
#include 
< stdio.h >
#include 
< stdlib.h >

int  ifcontinue( void )
{
    
int c;
    puts(
"Press any key to continue, q to quit... ");
    
if ((c=getchar())=='q') exit(1);
}



/* random.c: 用随机数给r[0].key-r[n-1].key赋值 */
#include 
< stdlib.h >
#include 
< time.h >

#include 
" sort.h "

int  Random (Record r[], int  n)
{
    
int i;
    srand(time(NULL));
    
for (i=0;i<n;i++)
        r[i].key
=rand();
    
return n;
}


 

/* bubblesort.c: 演示冒泡排序 */
#include 
" sort.h "
void  BubbleSort(Record r[], int  n)
{
     
int i,j,done=0;
     Record tmp;
     
     
for (i=0!done && i<n; i++){
         done
=1;
         
for (j=1;j<n-i;j++)
             
if (r[j-1].key>r[j].key){
                 tmp
=r[j-1];
                 r[j
-1]=r[j];
                 r[j]
=tmp;
                 done
=0;
             }

     }

}
        


/* quicksort.c 快速排序算法 */
#include 
" sort.h "
void  QuickSort (Record r[], int  s, int  t)
{
     
int low=s;
     
int high=t;
     Record pivot;
     
     pivot
=r[s];
     
while (low<high){
           
while (low<high && r[high].key>pivot.key)
                 high
--;
           
if (low<high) r[low++]=r[high];
           
while (low<high && r[low].key<pivot.key)
                 low
++;
           
if (low<high) r[high--]=r[low];
     }

     r[low]
=pivot;
     
if (s<low) QuickSort(r,s,low-1);
     
if (t>high) QuickSort(r,high+1,t);
}

           
/* inssort.c: 插入排序算法 */
#include 
" sort.h "

void  InsSort (Record r[], int  n)
{
     
int i,j;
     Record tmp;
     
for (i=1;i<n;i++{
         tmp
=r[i];
         
for (j=i-1; j>=0 && tmp.key < r[j].key; j--)
             r[j
+1]=r[j];
         r[j
+1]=tmp;
     }

}



/* shellsort.c: 希尔排序算法*/  
#include 
" sort.h "

void  ShellSort (Record r[], int  n)
{
     
int i,j,dk=n/2;
     Record tmp;
     
     
while (dk > 0{
           
for (i=dk;i<n;i++{
               tmp
=r[i];
               
for (j=i-dk;j>=0 && tmp.key<r[j].key;j-=dk)
                   r[j
+dk]=r[j];
               r[j
+dk]=tmp;
           }

           dk
/=2;
     }

}

               


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值