题解 | #C++冒泡排序#

C++冒泡排序

https://www.nowcoder.com/practice/eb72dada09de43ceacf93f9a143ee321

#include <iostream>
using namespace std;

int main() {

    int arr[6] = { 0 };
    int len = sizeof(arr) / sizeof(int);
    
    for (int i = 0; i < len; i++) {
        cin >> arr[i];
    }
    
    // write your code here......
    for (int i = 0 ; i < len - 1 ; i++)
    {
        for (int j = i + 1; j < len ; j++)
        {
            if (arr[i] > arr[j])
            {
                int tmp = arr[i];
                arr[i] = arr[j];
                arr[j] = tmp;
            }
        }
    }

    for (int i = 0; i < len; i++)
    {
        cout << arr[i] << " " ;
    }

    return 0;
}

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务