几种排序算法的实现

主要有:冒泡排序,简单选择排序,直接插入排序,还有快速排序的实现

不多说,直接贴代码:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

const int N = 10; 
double t1, t2, t3, t4;

void Maopao(int* a,int n)// 冒泡排序 
{
     printf("\n----------------------冒泡排序------------------------\n");
     printf("数据交换:\n");
     
     int x[n];
     int i, j, temp;
     for(int k = 0; k < n; k++)
     x[k] = a[k];
     
     int time = 0;
     for (i = 0; i < n; i++)
         for(j = 0; j < n- i - 1; j++)
         if(x[j] > x[j + 1])
         {
                 time++;
                 printf("                       %d  %d\n", x[j], x[j+1]);
                 temp = x[j];
                 x[j] = x[j+1];
                 x[j+1] = temp;
         }
     printf("\nAfter sorting:\n");
     for(int k = 0; k < n; k++)
     printf("%d\t", x[k]);
     t1 = (double)clock()/CLOCKS_PER_SEC;
     printf("\n交换次数:%d(一次交换,三次移动)\n", time);
     printf("\nTime used: %.6lf secs.\n", t1); 
     printf("\n     ----------------冒泡结束-----------------\n");
}
     
void SelectSort(int* a,int n) //简单选择排序 
{
     printf("\n----------------------简单选择排序------------------------\n");
     printf("数据交换:\n");
     int x[n], temp, i, j, k;
     
     for(int m = 0; m < n; m++)
     x[m] = a[m];
     
     int time = 0; 
     for (i = 0; i < n; i++)
     {
             for (k = i+1, j = i; k <= n; k++)
                 if(x[k] < x[j])
                         j = k;
             if(j != i)
             {
                  time++;
                  temp = x[i];
                  x[i] = x[j];
                  x[j] = temp;
                  printf("                       %d  %d\n", x[i], x[j]); 
             }
     }
     printf("\nAfter sorting:\n"); 
     for(int m = 0; m < n; m++)
     printf("%d\t", x[m]);
     printf("\n交换次数:%d(一次交换,三次移动)\n", time);
     t2 = (double)clock()/CLOCKS_PER_SEC;
     printf("\nTime used: %.6lf secs.\n", t2-t1); 
     printf("\n----------------简单选择排序结束-----------------\n");
}

void Insertsort(int* a,int n)
{
     printf("\n----------------------直接插入排序------------------------\n");
     int x[n], i, j;
     for(int k = 0; k < n; k++)
     x[k] = a[k];
     
     int time = 0;
     for(i = 2; i <= n; i++)
             if(x[i] < x[i-1])
             {
                     time += 2;
                     x[0] = x[i];
                     x[i] = x[i-1];
                     
                     for (j = i-2; x[0] < x[j]; j--)
                     {
                         x[j+1] = x[j];
                         time += 1;
                     } 
                     
                     x[j+1] = x[0];
                     time += 1;
             
             }
             
     for (int k = 0; k < n; k++)
     printf("%d\t", x[k]);
     printf("\n移动次数:%d\n", time);
     t3 = (double)clock()/CLOCKS_PER_SEC;
     printf("\nTime used: %.6lf secs.\n", t3-t2); 
     printf("\n----------------直接插入排序结束-----------------\n");  
}

int Partition(int *num, int low, int high)
{
    num[0] = num[low];
    int key = num[low];
    
    while(low < high)
    {
              while(low < high && num[high] >= key)
                        --high;
              num[low] = num[high];
              
              while(low < high && num[low] <= key)
                        ++low;
              num[high] = num[low];
    }
    
    num[low] = num[0];
    return low;
}

void Qsort(int* num, int low, int high)
{
     int Division;
     
     if(low < high)
     {
            Division = Partition(num, low, high);
            Qsort(num, low, Division-1);
            Qsort(num, Division+1, high);
     }
}      
                     
int main()
{
    time_t t;
    int i, n, a[N+1];
    
    srand((unsigned)time(&t));
    for(int i = 0; i < N; i++)
    {
            n = rand() % 100;
            a[i] = n;
    }
    
    FILE *fp;
    fp = fopen("epx4.txt","wb");
    if(fp == NULL)
    {
          printf("Error in open your file\n");
          exit(0);
    }
    
    fwrite(a, sizeof(int), N, fp);
    fclose(fp);
    
    fp = fopen("epx4.txt","rb");
    if(fp == NULL)
    {
          printf("Error in open your file\n");
          exit(0);
    }
    int b[N+1];
    fread(b, sizeof(int), N, fp);
    printf("Data before sorting:\n");
    for(int i = 0; i < N; i++)
    printf("%d\t", b[i]);
    
    Maopao(b, N);
    SelectSort(b, N);
    Insertsort(b, N);
    
    
    printf("\n----------------------快速排序----------------------------\n"); // 快速排序
    int c[N+1]; // 准备工作 
    for (int k = 1; k <= N; k++)
    c[k] = b[k-1];
     
    int low, high;
    low = 1;
    high = N;
    
    Partition(c, low, high);
    Qsort(c, low, high); 
    
    for(int k = 1; k <= N; k++)
    printf("%d\t", c[k]);
    t4 = (double)clock()/CLOCKS_PER_SEC;
    printf("\nTime used: %.6lf secs.\n", t4-t3); 
    printf("\n----------------快速排序结束-----------------\n");
    
    system("pause");
    return 0;
}
    
    
            


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值