求解C语言题目

定义函数void sort(int a[],int n),用选择法对数组a中的元素升序排列。自定义main 函数,并在其中调用 sort函数.

#include <iostream>
using namespace std;

void sort(int s[], int n){
for(int i=0; i<n; i++){
for(int j=i; j<n; j++) {
if(s[i]>s[j]) swap(s[i], s[j]);
}
}
}

int main(){
int s[1000];
int n;
while(cin>>n){
for(int i=0; i<n; i++) cin>>s[i];
sort(s, n);
for(int i=0; i<n; i++) cout<<s[i]<<' ';
cout<<endl;
}
return 0;
}
温馨提示:答案为网友推荐,仅供参考