Coder Social home page Coder Social logo

shell排序算法写错了 about cs-notes HOT 5 CLOSED

cyc2018 avatar cyc2018 commented on May 18, 2024
shell排序算法写错了

from cs-notes.

Comments (5)

CyC2018 avatar CyC2018 commented on May 18, 2024

@keqizwl 没有错的

from cs-notes.

CyC2018 avatar CyC2018 commented on May 18, 2024

@keqizwl

import java.util.Arrays;

public class Shell {
    public static void sort(Comparable[] a) {
        int N = a.length;
        int h = 1;
        while (h < N / 3) {
            h = 3 * h + 1; // 1, 4, 13, 40, ...
        }
        while (h >= 1) {
            for (int i = h; i < N; i++) {
                for (int j = i; j >= h && less(a[j], a[j - h]); j -= h) {
                    exch(a, j, j - h);
                }
            }
            h = h / 3;
        }
    }

    private static void exch(Comparable[] a, int i, int j) {
        Comparable t = a[i];
        a[i] = a[j];
        a[j] = t;
    }

    private static boolean less(Comparable x, Comparable y) {
        return x.compareTo(y) < 0;
    }

    public static void main(String[] args) {
        Integer[] a = {1, 3, 2, 4, 8, 7, 0};
        sort(a);
        System.out.println(Arrays.toString(a));
    }
}
[0, 1, 2, 3, 4, 7, 8]

from cs-notes.

keqizwl avatar keqizwl commented on May 18, 2024

排序没错,因为当h为1的时候就是插入排序,肯定能排正确,但是算法不是正确的shell算法,你可以搜索下shell算法实现,对比下。

from cs-notes.

CyC2018 avatar CyC2018 commented on May 18, 2024

@keqizwl 麻烦给以下比较权威的资料,我找到的方法都是这么实现的。

from cs-notes.

keqizwl avatar keqizwl commented on May 18, 2024

不好意思,是我弄错了,你实现是对的

from cs-notes.

Related Issues (20)

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.