zoukankan      html  css  js  c++  java
  • 数据结构冒泡排序和直接插入排序

    本文内容

    • 通用数据结构
    • 直接插入排序
    • 冒泡排序
    • 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

  • 相关阅读:
    Modbus RTU与Modbus TCP的区别
    《对比Excel,轻松学习Python数据分析》笔记
    字符串 批量删除
    地图服务在线资源
    Java环境变量配置及maven配置
    Android学习路线
    MySQL备份脚本,应该这么写
    Windows中mysql5.6升级5.7
    SQL Server数据分析面试题(202008)
    PYTHON-未知错误
  • 原文地址:https://www.cnblogs.com/liuning8023/p/2279898.html
Copyright © 2011-2022 走看看