用选择法对数组中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”变量,将这个函数改了一下,这样做有没有问题?