指针,数组,函数,冒泡排序升序案例
#include<iostream>
using namespace std;
void bubblesort(int*arr,int len) {
for (int i = 0; i < len - 1; i++) {
for (int j = 0; j < len - i - 1; j++) {
if (arr[j] >arr[j + 1]) {
int temp = arr[j];
arr[j ] = arr[j+1];
arr[j + 1] = temp;
}
}
}
}
void printarray(int*arr,int len) {
for (int i = 0; i < len; i++) {
cout << arr[i] << " ";
}
}
int main() {
int arr[10] = { 2,4,6,3,5,7,1,9,10,8 };
int len = sizeof(arr) / sizeof(arr[0]);
bubblesort(arr, len);
printarray(arr, len);
return 0;
}