一种快速排序的实现

一种快速排序的实现

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;

public class HAH20200710 {
    
    public static void main(String[] args) {
        QSort qSort = new QSort(3,6,2,9,12,-12,11,10,8,20,17);
        qSort.work();
        System.out.println(qSort.toString());
    }
    public static String elementJoinAsStr (CharSequence de, Iterable<ElementInteger> elements) {
        Objects.requireNonNull(de);
        Objects.requireNonNull(elements);

        int needStrLength = 0;
        int size = 0;
        for (ElementInteger e : elements) {
            needStrLength += e.elementSize();
            size++;
        }
        needStrLength += (de.length() * (size - 1));

        StringBuilder buf = new StringBuilder(needStrLength);
        for (ElementInteger e : elements) {
            if (buf.length() != 0) {
                buf.append(de).append(e.getE());
            } else {
                buf.append(e.getE());
            }
        }
        return buf.toString();
    }
}

class QSort {
    private List<ElementInteger> elements;
    public QSort (Integer... integers) {
        Objects.requireNonNull(integers);
        this.elements = new ArrayList<> (integers.length);
        for (int i = 0, len = integers.length; i < len; i++) {
            ElementInteger e = new ElementInteger(integers[i]);
            this.elements.add(e);
        }
    }
    public void work () {
        int leftBorder = 0;
        int rightBorder = this.elements.size() - 1;
        sort(leftBorder, rightBorder);
    }
    public void sort (int leftBorder, int rightBorder) {
        if (rightBorder <= leftBorder) return;

        int benchmark = dividedIntoSmallOnTheLeftAndLargeOnTheRight(leftBorder, rightBorder);
        int theLeftBoundaryOfTheLeftHalf = leftBorder;
        int theRightBoundaryOfTheLeftHalf = benchmark - 1;
        int theLeftBoundaryOfTheRightHalf = benchmark + 1;
        int theRightBoundaryOfTheRightHalf = rightBorder;

        sort(theLeftBoundaryOfTheLeftHalf, theRightBoundaryOfTheLeftHalf);
        sort(theLeftBoundaryOfTheRightHalf, theRightBoundaryOfTheRightHalf);
    }
    public int dividedIntoSmallOnTheLeftAndLargeOnTheRight (int leftBorder, int rightBorder) {
        int defaultBenchmarkIndex = leftBorder;
        ElementInteger benchmarkElement = this.elements.get(defaultBenchmarkIndex);

        int leftCursor = leftBorder;
        int rightCursor = rightBorder + 1;

        while (true) {
            while (this.elements.get(++leftCursor).compareTo(benchmarkElement) >= 0) if (leftCursor == rightBorder) break;
            while (this.elements.get(--rightCursor).compareTo(benchmarkElement) <= 0) if (rightCursor == leftBorder) break;
            if (leftCursor >= rightCursor) break;
            swap(leftCursor, rightCursor);
        }

        int realBenchmarkIndex = rightCursor;
        swap(defaultBenchmarkIndex, realBenchmarkIndex);
        return realBenchmarkIndex;
    }
    public void swap (int a, int b) {
        ElementInteger t = this.elements.get(a);
        this.elements.set(a, this.elements.get(b));
        this.elements.set(b, t);
    }
    @Override
    public String toString() {
        return "[" + HAH20200710.elementJoinAsStr("],[", this.elements) + "]";
    }
}

class ElementInteger implements Comparable<ElementInteger>{
    private Integer e;
    
    public ElementInteger(Integer input) {
        this.e = input;
    }
    @Override
    public int compareTo(ElementInteger o) {
        return this.e >= o.getE() ? (this.e == o.getE() ? 0 : 1) : -1;
    }
    public int getE() {
        return e;
    }
    public void setE(int e) {
        this.e = e;
    }
    public int elementSize () {
        long num = this.e.longValue();
        int size = num < 0 ? 1 : 0;
        num = num > 0 ? num : -num;
        if (num == 0) {
            return 1;
        }
        size += (int) Math.log10(num) + 1;
        return size;
    }
}

你可能感兴趣的:(随笔)