冒泡排序,bubbleSort

// 冒泡排序,bubbleSort
// O(n^2),稳定
#include <cstdio>

void bubbleSort(int array[], int length)
{
    for(int i = 0; i < length-1; i++)
    {
        for(int j = i+1; j < length; j++)
        {
            if(array[i] > array[j])
            {
                int temp = array[i];
                array[i] = array[j];
                array[j] = temp;
            }
        }
    }
}

int main()
{
    int array[] = {1,3,4,5,2,6,9,7,8,0};
    int n = sizeof(array)/sizeof(array[0]);

    bubbleSort(array, n);
    for(int i = 0; i < n; i++)
        printf("%d ",array[i]);
}
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页