当前位置 : 主页 > 编程语言 > c++ >

数据结构 C++冒泡排序 数组当参数传递

来源:互联网 收集:自由互联 发布时间:2021-06-23
冒泡排序 #include iostream using namespace std; void bubblesort1A(int A[],int n); int main() { int A[10]={0},n=0,i=0; cinn; for( i=0;in;i++) cinA[i]; bubblesort1A( A , n); for(int i=0;in;i++) coutA[i]endl; return 0; } void bubblesort1A(
冒泡排序
#include <iostream>

using namespace std;
void bubblesort1A(int A[],int n);
int main() {
int A[10]={0},n=0,i=0;
cin>>n;
for( i=0;i<n;i++)
cin>>A[i];
bubblesort1A( A , n);
for(int i=0;i<n;i++)
cout<<A[i]<<endl;
return 0;
}
void bubblesort1A(int A[],int n)
{
bool sorted = false;
while (!sorted)
{
sorted = true;
for (int i = 1; i < n; i++)
{
if (A[i - 1] > A[i])
{
swap(A[i - 1], A[i]);
sorted = false;
}
}n--;
}
}

数组当参数传递
#include <iostream>
using namespace std;
int test2(int a[]){
for(int i=0;i<5;i++){
cout<<a[i]<<endl;
}
}
int main(){
int a[5] = {1,2,3,4,5};
test2(a);
}
网友评论