题解 | #牛牛的排序#
牛牛的排序
https://www.nowcoder.com/practice/26a0c92e9266443887a3bf81aff8e188
#include <stdio.h> void sort(int *arr,int n) {//把最大值依次往右排 for (int i = 0; i < n-1; i++) { //外层循环控制遍历次数n-1; for (int j = 0; j < n-i-1; j++) { //内层循环控制比较次数,多遍历一次,少比较一次 if(*(arr+j)> *(arr+j+1)) { int tmp = *(arr+j); *(arr+j) = *(arr+j+1); *(arr+j+1) = tmp; } } } for (int i = 0; i < n; i++) { printf("%d ",arr[i]); } } int main() { int n; scanf("%d",&n); int arr[n]; for (int i = 0; i < n; i++) { scanf("%d",&arr[i]); } sort(arr,n); return 0; }