Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1607094146 赵励志 #23

Open
ZhaoLizz opened this issue Nov 24, 2017 · 0 comments
Open

1607094146 赵励志 #23

ZhaoLizz opened this issue Nov 24, 2017 · 0 comments

Comments

@ZhaoLizz
Copy link

作业

BaseSort

package com.company.sort;

public abstract class BaseSort {
    public abstract void sort(Comparable[] a);

    /**
     * 前者小于后者时返回true
     */
    boolean less(Comparable v, Comparable w) {
        return v.compareTo(w) < 0;
    }

    /**
     * 交换Comparable数组下标为 i 和 j 的两个元素的值
     */
    void exch(Comparable[] a, int i, int j) {
        Comparable t = a[i];
        a[i] = a[j];
        a[j] = t;
    }

}

QuickSort

package com.company.sort;

public class QuickSort extends BaseSort {
    @Override
    public void sort(Comparable[] a) {
        sort(a, 0, a.length - 1);
    }

    public void sort(Comparable[] a, int lo, int hi) {
        if (hi <= lo)
            return;

        int j = partition(a, lo, hi);
        sort(a, lo, j - 1);
        sort(a, j + 1, hi);
    }

    /**
     * 获取数组在当前范围内的切分元素的下标
     */
    public int partition(Comparable[] a, int lo, int hi) {
        int i = lo;
        int j = hi + 1;
        Comparable v = a[lo];

        while (true) {
            while (less(a[++i], v))
                if (i == hi)
                    break;

            while (less(v, a[--j]))
                if (j == lo)
                    break;

            //i j 相遇
            if (i >= j)
                break;
            exch(a, i, j);
        }

        exch(a, lo, j);
        return j;
    }
}

InsertSort

package com.company.sort;

public class InsertSort extends BaseSort {

    /**
     * 基于插入排序的希尔排序
     * @param a
     */
    @Override
    public void sort(Comparable[] a) {
        int N = a.length;
        int h = 1;

        while (h < N / 3) {
            h = 3 * h + 1;
        }

        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 /= 3;
        }
    }
}

SelectSort

package com.company.sort;

public class SelectSort extends BaseSort {

    @Override
    public void sort(Comparable[] a) {
        int N = a.length;
        for (int i = 0; i < N; i++) {
            int min = i;
            for (int j = i + 1; j < N; j++) {
                if (less(a[j],a[min]))
                    min = j;
                exch(a, i, min);
            }
        }
    }
}

Factory

package com.company.sort;

import java.util.Arrays;

public class Factory {
    private BaseSort baseSort;

    public void setBaseSort(BaseSort baseSort) {
        this.baseSort = baseSort;
    }

    public void doSort(Comparable[] a) {
        baseSort.sort(a);
        System.out.println(Arrays.toString(a));
    }
}

Test

package com.company.sort;

import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        Factory factory = new Factory();
        Scanner in = new Scanner(System.in);
        Comparable[] a = new Comparable[10];

        for (int i = 0; i < 10; i++) {
            a[i] = in.nextInt();
        }

        factory.setBaseSort(new QuickSort());
//        factory.doSort(a);

        factory.setBaseSort(new SelectSort());
//        factory.doSort(a);

        factory.setBaseSort(new InsertSort());
//        factory.doSort(a);

    }
}
@ZhaoLizz ZhaoLizz changed the title 1607094146 1607094146 赵励志 Nov 24, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant