C语言第十四课:冒泡排序

案例

#include "pch.h"
#include <iostream>

/*用冒泡排序写一个小程序进行排序:
1.78, 1.77, 1.82, 1.79, 1.85, 1.75, 1.86, 1.77, 1.81, 1.80*/
int main()
{
    int i, j;
    double arr[] = { 1.78, 1.77, 1.82, 1.79, 1.85, 1.75, 1.86, 1.77, 1.81, 1.80 };
    //输出排序前
    printf("\n************排队前*************\n");
    for ( i = 0; i < 10; i++)
    {
        if (i!=9)
        {
            printf("%.2lf,", arr[i]);
        }
        else
        {
            printf("%.2lf", arr[i]);
        }
    }
    printf("\n************排队后*************\n");
    for ( i = 8; i >= 0; i--)
    {
        for (j = 0; j <= i; j++)
        {
            double temp;
            if (arr[j]>arr[j+1])
            {
                temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }
    for ( i = 0; i < 10; i++)
    {
        if(i!=9)
        {
            printf("%.2lf,", arr[i]);
        }
        else
        {
            printf("%.2lf", arr[i]);
        }
    }
}

本文链接:

https://www.zaigie.com/archives/119/