C++中用选择法对数组中10个整数由小到大排序
C++中用选择法对数组中10个整数由小到大排序用选择法对数组中10个整数由小到大排序,其中数组名作函数实参和形参。#include <iostream>using namespace std;int main(){void select_sort(int array[], int n);//函数声明int a[10];int i;cout<<"input 10 number
·
C++中用选择法对数组中10个整数由小到大排序
用选择法对数组中10个整数由小到大排序,其中数组名作函数实参和形参。
#include <iostream>
using namespace std;
int main()
{
void select_sort(int array[], int n); //函数声明
int a[10];
int i;
cout<<"input 10 numbers:"<<endl;
for(i=0; i<=9; i++) cin>>a[i]; //输入a[0]到a[9]
cout<<endl;
select_sort(a,10); //函数调用,数组名作函数实参
cout<<"the sorted numbers:"<<endl;
for(i=0; i<=9; i++) cout<<a[i]<<" "; //输出排好的10个数
cout<<endl;
return 0;
}
void select_sort(int array[], int n) //数组名作形参
{
int i,j,t;
for(i=0; i<n-1; i++) //进行n-1轮(0~n-2)挑选小的数
{
for(j=i+1; j<=n-1; j++)
if(array[j]<array[i]) //选出a[i+1]到a[n-1]中最小的数a[j]与a[i]互换
{
t=array[j];
array[j]=array[i];
array[i]=t;
}
}
}

GitCode 天启AI是一款由 GitCode 团队打造的智能助手,基于先进的LLM(大语言模型)与多智能体 Agent 技术构建,致力于为用户提供高效、智能、多模态的创作与开发支持。它不仅支持自然语言对话,还具备处理文件、生成 PPT、撰写分析报告、开发 Web 应用等多项能力,真正做到“一句话,让 Al帮你完成复杂任务”。
更多推荐
所有评论(0)