Title Set basic programming bubble sort method 7-27 (20 minutes)

Here Insert Picture Description

#include <stdio.h>
int main()
{
    int n, k;
    int a[100];
    scanf("%d %d", &n, &k);
    for (int i = 0; i < n; i++)
    {
        scanf("%d", &a[i]);
    }
    for (int i = 0; i < k; i++)
    {
        for (int j = 0; j < n - 1; j++)
        {
            if (a[j] > a[j + 1])
            {
                int t = a[j];
                a[j] = a[j + 1];
                a[j + 1] = t;
            }
        }
    }
    for (int i = 0; i < n; i++)
    {
        printf("%d", a[i]);
        if (i != n - 1)
            printf(" ");
    }
    return 0;
}
Published 287 original articles · won praise 117 · Views 8922

Guess you like

Origin blog.csdn.net/qq_44458489/article/details/105400405