搜尋此網誌

2024年6月30日 星期日

數字排序 由大到小與由小到大 排列

#include <stdio.h>

#define MAX_NUM 10

int main()
{
    int i, j, tmp;
    int *members;
    int test_case0[MAX_NUM] = {13, -5, 0, 7, 79, 213, 5, 132, -53, 71};
    int test_case1[MAX_NUM] = {91, 65, -12, 78, -9, -312, 5, 132, -121, -9};
    printf("\n==================Original===================\n");
    members = test_case0;
    for (i = 0; i < MAX_NUM; i++)
    {
        printf("%d ", members[i]);
    }

    printf("\n========Arrange from large to small=========\n");
    //-------------------------------------
    members = test_case0;
    for (i = 0; i < MAX_NUM; i++)
    {
        for (j = i + 1; j < MAX_NUM; j++)
        {
            if (members[i] < members[j])
            {
                tmp = members[i];
                members[i] = members[j];
                members[j] = tmp;
            }
        }
    }
    //-------------------------------------

    for (i = 0; i < MAX_NUM; i++)
    {
        printf("%d", members[i]);
        if ((i + 1) < MAX_NUM)
            printf(" > ");
    }
    printf("\n==================Original===================\n");
    members = test_case1;
    for (i = 0; i < MAX_NUM; i++)
    {
        printf("%d ", members[i]);
    }
    printf("\n========Arrange from small to large=========\n");
    //-------------------------------------
    members = test_case1;
    for (i = 0; i < MAX_NUM; i++)
    {
        for (j = i + 1; j < MAX_NUM; j++)
        {
            if (members[i] > members[j])
            {
                tmp = members[i];
                members[i] = members[j];
                members[j] = tmp;
            }
        }
    }
    //-------------------------------------

    for (i = 0; i < MAX_NUM; i++)
    {
        printf("%d", members[i]);
        if ((i + 1) < MAX_NUM)
            printf(" < ");
    }
    printf("\n=============================================\n");

    return 0;
}

==================Original=================== 13 -5 0 7 79 213 5 132 -53 71 ========Arrange from large to small========= 213 > 132 > 79 > 71 > 13 > 7 > 5 > 0 > -5 > -53 ==================Original=================== 91 65 -12 78 -9 -312 5 132 -121 -9 ========Arrange from small to large========= -312 < -121 < -12 < -9 < -9 < 5 < 65 < 78 < 91 < 132 =============================================

沒有留言:

張貼留言