0
点赞
收藏
分享

微信扫一扫

【自学C】用选择法对10个整数排列

谁知我新 2022-02-21 阅读 40
c语言

从待排序数列中选出最大的放在首位,两数交换位置

#include<stdio.h>

int main(void)
{
    int max,buf;
    int num=0;                        
    int a[10] = { 3,18,32,76,89,102,23,43,78,99 };
    for (int i = 0; i < 10; i++)
    {
        max = a[i];
        for (int j = i; j < 10; j++)
        {
            if (a[j] > max)
            {
                max = a[j];
                num = j;                        //num用来存储该轮循环出现最大值的位置
            }
        }
        a[num] = a[i];
        a[i] = max;
    }

    for (int j = 0; j < 10; j++)
    {
        printf("%d\n", a[j]);
    }
}

举报

相关推荐

0 条评论