标题:用选择法对数组中10个整数从小到大排列
取消只看楼主
GELUCk
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2018-1-11
结帖率:0
已结贴  问题点数:10 回复次数:1 
用选择法对数组中10个整数从小到大排列
书中给出的答案:
#include <iostream>
using namespace std;
int main()
{
    void select_sort(int array[], int n);
    int a[10], i;
    cout << "enter the originl arry:" << endl;
    for (i = 0; i < 10; i++)
        cin >> a[i];
    cout << endl;
    select_sort(a,10);
    cout << "the ssorted array:" << endl;
    for (i = 0; i < 10; i++)
        cout << a[i] << " ";
    cout << endl;
    system("pause");
}
void select_sort(int array[], int n)
{
    int i, j, k, t;

    for (i = 0; i < n - 1; i++)
    {
        k = i;
        for (j = i + 1; j < n; j++)
            if (array[j] < array[k])        
                k = j;
        t = array[k]; array[k] = array[i]; array[i] = t;
    }
}

我自己写的:

#include <iostream>
using namespace std;
void select_sort(int array[], int n);
int main()
{
    void select_sort(int array[], int n);
    int a[10], i;
    cout << "enter the originl arry:" << endl;
    for (i = 0; i < 10; i++)
        cin >> a[i];
    cout << endl;
    select_sort(a, 10);
    cout << "the ssorted array:" << endl;
    for (i = 0; i < 10; i++)
        cout << a[i] << " ";
    cout << endl;
    system("pause");
}
void select_sort(int array[], int n)
{
    int i, j, t;
    for (i = 0; i < n - 1; i++)
    {
        for (j = i + 1; j < n; j++)
            if (array[j] < array[i])
            {
                t = array[j]; array[j] = array[i]; array[i] = t;
                cout << array[i] << " " << array[j] << endl;
            }
    }
}
主要是我的select_sort函数中省去了“k”变量,将这个函数改了一下,这样做有没有问题?
搜索更多相关主题的帖子: int array cout for i++ 
2018-02-21 18:06
GELUCk
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2018-1-11
得分:0 
哦,原来如此,谢了。
2018-03-04 17:22



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-484810-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.287301 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved