c++版:冒泡排序

冒泡排序

void bubbleSort(int a[],int length)
{
    int temp;
    for (int i = 0; i < length - 1; i ++) {
        for (int j = 0; j < length - i -1; j++) {
            if (a[j] > a[j+1])//升序
            {
                temp = a[j];
                a[j] = a[j + 1];
                a[j + 1] = temp;
                //交换变量
            }
        }
    }
}
int main(int argc, const char * argv[])
{
    int s[] = {23,42,323,34,2,3,1324,23,23,43,3};
    bubbleSort(s, 11);
    std::cout <<"从小到大的排序为:";
    for (int i = 0; i < 11; i ++) {
        std::cout << s[i];
        if (i != 10) {
            std::cout<<",";
        }
    }
    return 0;
}


输出结果为:

从小到大的排序为:2,3,3,23,23,23,34,42,43,323,1324


你可能感兴趣的:(C++,冒泡排序)