编一函数对数组进行排序(用1个参数指定排序的顺序,默认为升序),然后编写主函数对其进行测试。
#include
//对数组进行排序(第3个参数asc指定排序的顺序,默认为升序)void sort(int *a,int n,___) {
}
void showArray(const int a[],int n) { for (int i=0;i<n;i++) std::cout<<"a["<<i<<"]="<<a[i]<<' '; std::cout<<std::endl;}
int main(void) { int a[]={89,12,78,56,23,45,67,34,45}; const int n=____;
sort(a,n); showArray(a,n); sort(a,n,false); showArray(a,n); sort(a,n,true); showArray(a,n);
return 0;}
输入
无
输出
第1行:从小到大排序后输出数组的所有元素 第2行:从大到小排序后输出数组的所有元素 第3行:从小到大排序后输出数组的所有元素
void sort(int *a,int n,___)
填写 int asc = 1
函数里面写
{
for (int i = 0; i < n - 1; i++)
for (int j = 0; j < n - 1 - i; j++)
{
if ((a[j] > a[j + 1] && asc) || (a[j] < a[j + 1] && !asc)) { int t = a[j]; a[j] = a[j + 1]; a[j + 1] = t; }
}
}
const int n=____; 这里写 9