• 数据结构冒泡排序和直接插入排序


    本文内容

    • 通用数据结构
    • 直接插入排序
    • 冒泡排序
    • Main 函数
    • 运行结果

    本文用 CodeBlock 编写。同时也提供 VS C 和 VS C# 代码。

    通用数据结构

    • MyElement.h
    #ifndef MYELEMENT_H_INCLUDED
    #define MYELEMENT_H_INCLUDED
     
    #define LT(a,b) ((a)<(b))
     
    #define TRUE 1
    #define FALSE 0
    typedef int Status;
     
    typedef int ElementType;
     
    void Print(ElementType e[], int n);
     
    #endif // MYELEMENT_H_INCLUDED

    • MyElement.c
    #include <stdio.h>
    #include "MyElement.h"
     
    void Print(ElementType e[], int n)
    {
        int i;
        for(i=0; i<n; i++)
            printf("%d ",e[i]);
        printf("\n");
    }

    直接插入排序

    • InsertSort.h
    #ifndef INSERTSORT_H_INCLUDED
    #define INSERTSORT_H_INCLUDED
     
    #include "MyElement.h"
     
    void InsertSort(ElementType e[], int n);
    void Main_InsertSort();
     
    #endif // INSERTSORT_H_INCLUDED
    • InsertSort.c
    #include <stdio.h>
    #include "MyElement.h"
    #include "InsertSort.h"
     
    #define N 11
     
    void InsertSort(ElementType a[], int n)
    {
        int i,j;
        for(i=2; i<=n-1; ++i)
            if (LT(a[i],a[i-1]))
            {
                a[0] = a[i];
                for(j=i-1; LT(a[0],a[j]); --j)
                    a[j+1] = a[j];
                a[j+1] = a[0];
            }
    }
     
    void Main_InsertSort()
    {
        ElementType e[N] = {0,49,38,65,97,76,13,27,49,55,10};
        printf("Before InsertSort:\n");
        Print(e,N);
        InsertSort(e,N);
        printf(" After InsertSort:\n");
        Print(e,N);
    }

    冒泡排序

    • BubbleSort.h
    #ifndef BUBBLESORT_H_INCLUDED
    #define BUBBLESORT_H_INCLUDED
     
    #include "MyElement.h"
     
    void BubbleSort(ElementType e[], int n);
    void Main_BubbleSort();
     
    #endif // BUBBLESORT_H_INCLUDED
    • BubbleSort.c
    #include <stdio.h>
    #include "MyElement.h"
    #include "BubbleSort.h"
     
    #define N 10
     
    void BubbleSort(ElementType a[], int n)
    {
        int i,j,t;
        Status change;
        for(i=n-1,change=TRUE; i>0 && change; --i)
        {
            change=FALSE;
            for(j=0; j<i; ++j)
                if(a[j]>a[j+1])
                {
                    t=a[j];
                    a[j]=a[j+1];
                    a[j+1]=t;
                    change=TRUE;
                }
        }
    }
     
    void Main_BubbleSort()
    {
        int d[N]= {50,38,65,97,76,13,27,49,55,4};
        printf("Before BubbleSort:\n");
        Print(d, N);
        BubbleSort(d, N);
        printf(" After BubbleSort:\n");
        Print(d,N);
    }

    参看冒泡排序过程

    Main 函数

    #include "BubbleSort.h"
    #include "InsertSort.h"
     
    int main()
    {
        Main_InsertSort();
        Main_BubbleSort();
    }

    运行结果

    冒泡排序和直接插入排序

     

    下载冒泡排序 Demo

    下载插入排序 Demo

  • 相关阅读:
    wpa_supplicant 和 802.11g WPA 认证的配置
    wpa_supplicant使用笔记
    Linux无线网络设置(wpa_supplicant的使用)
    wpa_supplicant 配置与应用
    wifi测试相关(iwconfig,WPA Supplicant用法)
    wpa_supplicant对wep,wpa,wpa2的psk和隐藏ap的scan_ssid扫描配置
    WPA Supplicant 用法
    svn提交时强制注释
    window 系统下修改`CMD`的编码格式的方法,`CHCP` 的 使用
    windows命令行下redis读取中文字符乱码
  • 原文地址:https://www.cnblogs.com/liuning8023/p/2279898.html
Copyright © 2020-2023  润新知