数据结构冒泡排序过程

本文内容

  • 冒泡排序
  • 运行过程

冒泡排序

#include <stdio.h>
 
#define N 10
#define LT(a,b) ((a)<(b))
 
#define TRUE 1
#define FALSE 0
typedef int Status;
typedef int ElementType;
 
void MyPrint(ElementType e[], int n)
{
    int i;
    for(i=0; i<n; i++)
        printf("%d ",e[i]);
    printf("\n");
}
 
void BubbleSort(ElementType a[], int n)
{
    int times=1;
    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;
 
                printf("%2d : ", times++);
                MyPrint(a,N);
            }
        }
    }
}
 
void main()
{
    int d[N]= {50,38,65,97,76,13,27,49,55,4};
    printf("Before Sort: \n");
    printf("     ");
    MyPrint(d, N);
    BubbleSort(d, N);
}

运行过程

冒泡排序运行过程

 

下载 Demo

原文地址:https://www.cnblogs.com/liuning8023/p/2280017.html