Giter Club home page Giter Club logo

algorithms's Issues

Part1Basic/JAVA/src/main/java/Chapter2SortingBasic/Section6InsertionSortOptimize/InsertionSort.java,发现一个bug

public static void sort(Comparable[] arr) {
        int n = arr.length;
        // 注意下标从1开始,不停往前比较,下标为0的元素默认有效
        for (int i = 1; i < n; ++i) {
            // 先把起始位置的元素暂存
            Comparable tmp = arr[i];
            // 从位置i开始,从前挨个比较(前面已经是有序地了,这个是前提),一直比较到前面的某个元素比自己小就停下
            for (int j = i; j > 0; j--) {
                if (tmp.compareTo(arr[j - 1]) < 0) {
                    // 起始位置元素比前面的小,就把前面的值附到后面来元素上
                    arr[j] = arr[j - 1];
                } else {
                    // 一直到j前面的元素j-1小于tmp了,说明升序排列完成,把之前存的起始位置元素tmp插入到此处即可
                    arr[j] = tmp;
                    break;
                }
            }

        }

当j=0时,不会进入for循环。arr[j] = tmp;应该写在内部循环的外面。

public static void sort(Comparable[] arr) {
        int n = arr.length;
        // 注意下标从1开始,不停往前比较,下标为0的元素默认有效
        for (int i = 1; i < n; ++i) {
            // 先把起始位置的元素暂存
            Comparable tmp = arr[i];
            int j = i;
            // 从位置i开始,从前挨个比较(前面已经是有序地了,这个是前提),一直比较到前面的某个元素比自己小就停下
            for (; j > 0; j--) {
                if (tmp.compareTo(arr[j - 1]) < 0) {
                    // 起始位置元素比前面的小,就把前面的值附到后面来元素上
                    arr[j] = arr[j - 1];
                } else {
                    // 一直到j前面的元素j-1小于tmp了,说明升序排列完成,把之前存的起始位置元素tmp插入到此处即可
                    break;
                }
            }
            arr[j] = tmp;

        }
    }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.