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 --;

        }

    }

  • 相关阅读:
    leetcode931
    leetcode1289
    leetcode1286
    poj Meteor Shower
    noip杂题题解
    noip2007部分题
    NOIP Mayan游戏
    某模拟题题解
    codevs 1423 骑士
    noip 邮票面值设计
  • 原文地址:https://www.cnblogs.com/huigee/p/9836676.html
Copyright © 2011-2022 走看看