zoukankan      html  css  js  c++  java
  • 冒泡排序

    严格按照定义写的代码:

    void bubbleSort(int array[], int length)

    {

        for (int i = 0; i < length; i++) {

            for (int j = 1; j < length - i; j++) {

                if (array[j-1] > array[j]) {

                    swap(array[j-1], array[j]);

                }

            }

        }

    }

    void swap(int &a, int &b) {

        int temp = a;

        a = b;

        b = temp;

    }

    网络优化版

    void bubbleSort(int array[], int length) {

        bool flag = true;//判断是否发生交换

        while (flag) {

            flag = false;

            for (int j = 1; j < length; j++) {

                if (array[j-1] > array[j]) {

                    swap(array[j-1], array[j]);

                    flag = true;

                }

            }

            length --;

        }

    }

  • 相关阅读:
    PatentTips
    PatentTips
    PatentTips
    PatentTips
    PatentTips
    PatentTips
    PatentTips
    PatentTips
    How to build and run ARM Linux on QEMU from scratch
    How to debug Android Native Application with eclipse
  • 原文地址:https://www.cnblogs.com/huigee/p/9836676.html
Copyright © 2011-2022 走看看