c++冒泡排序

#include <iostream>

int main() {
    int arr[]={4,8,2,3,1,0,9,7,6};
    std::cout<<"排序前的数组:"<<arr<<std::endl;
    int arr_length = sizeof(arr)/ sizeof(arr[0]);
    std::cout<<"数组的元素个数:"<<arr_length<<std::endl;

    for (int i=0;i<arr_length-1;i++){
        for(int j=0;j<arr_length-i-1;j++){
            if(arr[j]>arr[j+1]){
                int temp = arr[j];
                arr[j] =arr[j+1];
                arr[j+1] = temp;
            }

        }
    }

    for (int i = 0; i < arr_length; i++)
    {
        std::cout << arr[i] << std::endl;
    }
    return 0;
}

posted @ 2021-07-02 11:09  公众号python学习开发  阅读(51)  评论(0编辑  收藏  举报