• 内部排序->交换排序->起泡排序


    文字描述

      首先将第一个记录的关键字和第二个记录的关键字进行比较,若为逆序(L.r[1].key>L.r[2].key),则将两个记录交换位置,然后比较第二个记录和第三个记录的关键字。依次类推,直至第n-1个记录和第n个记录的关键字进行过比较为止。上述过程称为第一趟起泡排序,其结果使得关键字最大的记录被安置到最后一个记录的位置上。

      整个起泡排序过程需进行k(k大于等于1且小于n)趟起泡排序,显然判别起泡排序结束的条件应该是排序过程中没有进行过交换记录的操作。

    示意图

    算法分析

      如果待排序序列为“正序”序列,只需进行一趟排序,在排序过程中进行n-1次关键字间的比较,且不移动记录;

      如果待排序序列为“逆序”序列,则需进行n-1趟排序,需进行n(n-1)/2次比较,并作等数量级的记录移动。

      因此,总的时间复杂度为n*n, 辅助空间为1,是稳定的排序方法。

    代码实现

     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 
     4 #define DEBUG
     5 
     6 #define EQ(a, b) ((a) == (b))
     7 #define LT(a, b) ((a) <  (b))
     8 #define LQ(a, b) ((a) <= (b))
     9 
    10 #define DEBUG
    11 
    12 #define MAXSIZE 20
    13 typedef int KeyType;
    14 typedef char InfoType;
    15 typedef struct{
    16     KeyType key;
    17     InfoType otherinfo;
    18 }RedType;
    19 
    20 typedef struct{
    21     RedType r[MAXSIZE+1];
    22     int length;
    23 }SqList;
    24 
    25 void PrintList(SqList L){
    26     int i = 0;
    27     printf("下标值:");
    28     for(i=0; i<=L.length; i++){
    29         printf("[%d] ", i);
    30     }
    31     printf("
    关键字:");
    32     for(i=0; i<=L.length; i++){
    33         printf(" %-3d", L.r[i].key);
    34     }
    35     printf("
    其他值:");
    36     for(i=0; i<=L.length; i++){
    37         printf(" %-3c", L.r[i].otherinfo);
    38     }
    39     printf("
    
    ");
    40     return ;
    41 }
    42 
    43 /*起泡排序*/
    44 void BubbleSort(SqList *L)
    45 {
    46     int i = 0, j = 0, isswap = 0;
    47     RedType tmp;
    48     for(i=1; i<=L->length; i++){
    49         isswap = 0;
    50         for(j=1; j<(L->length-i+1); j++){
    51             if(!LQ(L->r[j].key, L->r[j+1].key)){
    52                 tmp = L->r[j];
    53                 L->r[j] = L->r[j+1];
    54                 L->r[j+1] = tmp;
    55                 isswap = 1;
    56             }
    57         }
    58 #ifdef DEBUG
    59         printf("起泡排序第%d趟:
    ", i);
    60         PrintList(*L);
    61 #endif
    62         if(!isswap)
    63             break;
    64     }
    65 }
    66 
    67 int  main(int argc, char *argv[])
    68 {
    69     if(argc < 2){
    70         return -1;
    71     }
    72     SqList L;
    73     int i = 0;
    74     for(i=1; i<argc; i++){
    75         if(i>MAXSIZE)
    76             break;
    77         L.r[i].key = atoi(argv[i]);
    78         L.r[i].otherinfo = 'a'+i-1;
    79     }
    80     L.length = (i-1);
    81     L.r[0].key = 0;
    82     L.r[0].otherinfo = '0';
    83     printf("输入数据:
    ");
    84     PrintList(L);
    85     BubbleSort(&L);
    86     return 0;
    87 }
    起泡排序

    运行

  • 相关阅读:
    html URLRewriter生成静态页不能访问
    sql server 2008 不允许保存更改,您所做的更改要求删除并重新创建以下表
    IIS7.0 伪静态页配置
    hubbledotnet 定时更新索引
    今天开通了这个BLOG。
    ASP.NET公有六种验证控件 功能描叙
    Recommend of the Day:Orkut社区和明星推荐
    每日英语:Why You Need a Dictator in a Marriage
    每日英语:An Unhappy Middle in the Middle Kingdom
    每日英语:Web Browsers Are Reinvented
  • 原文地址:https://www.cnblogs.com/aimmiao/p/9367633.html
Copyright © 2020-2023  润新知