There is currently 1 post.
Font size: Small - 100% (Default)  Content converter: No conversion
 
Clicks Replies
1204 0
【程式】選擇排序法C語言程式
巨大八爪鱼
武林盟主 二十一级
Reply
Floor 1 Posted at: 1/9/16 17:32
#include <stdio.h>
#define COMPARE <

int main()
{
    int nums[] = {303, 558, 314, 117, 205, 48, 96, 118, 653};
    int count = sizeof(nums) / sizeof(int);
    int i, j, temp, max_j;
    
    // 按從大到小排序
    for (i = 0; i < count - 1; i++) // 循環共進行count - 1輪
    {
        // 每輪循環除第一個外,比較餘下的數,找出最大的數的下標
        max_j = i + 1;
        for (j = i + 2; j < count; j++)
        {
            if (nums[max_j] COMPARE nums[j])
                max_j = j;
        }
        
        // 如果該輪循環第一個數比最大的數小,就交換
        if (nums[i] COMPARE nums[max_j])
        {
            temp = nums[i];
            nums[i] = nums[max_j];
            nums[max_j] = temp;
        }
    }
    for (i = 0; i < count; i++)
        printf("%d ", nums[i]);
    putchar('\n');
    return 0;
}
Reply the post
Content:
User: You are currently anonymous.
Captcha:
Unclear? Try another one.
(Shortcut key: Ctrl+Enter)
Post Information
Clicks: 1204 Replies: 0
Author: 巨大八爪鱼
Last reply: 巨大八爪鱼
Last reply time: 1/9/16 17:32
Announcements