排序算法

1 篇文章 0 订阅

排序算法

/**
 * @author admin
 */
public abstract class Sort<T extends Comparable<T>> {

    protected String name;

    public Sort(String name) {
        this.name = name;
    }

    protected abstract void sort(T[] nums);

    protected boolean less(T x, T y) {
        return x.compareTo(y) < 0;
    }

    protected void swap(T[] nums, int i, int j) {
        T temp = nums[i];
        nums[i] = nums[j];
        nums[j] = temp;
    }

    public void sortAndPrint(T[] nums) {
        sort(nums);
        System.out.println(name + ":");
        for (T num : nums) {
            System.out.print(num + " ");
        }
        System.out.println();
    }
}

选择排序

/**
 * 不稳定
 * @param <T>
 */
public class Selection<T extends Comparable<T>> extends Sort<T> {

    public Selection(String name) {
        super(name);
    }

    @Override
    protected void sort(T[] nums) {
        for (int i = 0; i < nums.length - 1; i++) {
            int min = i;
            for (int j = i + 1; j < nums.length; j++) {
                if (less(nums[j], nums[min])) {
                    min = j;
                }
            }
            swap(nums, i, min);
        }
    }
}

冒泡排序

/**
 * 稳定
 * @param <T>
 */
public class Bubble<T extends Comparable<T>> extends Sort<T> {
    public Bubble(String name) {
        super(name);
    }

    @Override
    protected void sort(T[] nums) {
        boolean isSorted = true;
        for (int i = nums.length - 1; i > 0 && isSorted; i--) {
            isSorted = false;
            for (int j = 0; j < i; j++) {
                if (less(nums[j+1], nums[j])) {
                    isSorted = true;
                    swap(nums, j, j+1);
                }
            }
        }
    }
}

插入排序

/**
 * 稳定
 */
public class Insertion<T extends Comparable<T>> extends Sort<T> {

    public Insertion(String name) {
        super(name);
    }

    @Override
    protected void sort(T[] nums) {
        for (int i = 0; i < nums.length; i++) {
            for (int j = i; j > 0 && less(nums[j], nums[j - 1]); j--) {
                swap(nums, j, j - 1);
            }
        }
  

希尔排序

/**
 * 希尔排序:
 * 插入排序 + 间隔h
 * 不稳定
 */
public class Shell<T extends Comparable<T>> extends Sort<T> {
    public Shell(String name) {
        super(name);
    }

    @Override
    protected void sort(T[] nums) {

        int n = nums.length;
        int h = 1;
        while (h < n / 3) {
            h = h * 3 + 1;
            // 1, 4, 13, 40, 121, 364, 1092,...
        }

        while (h >= 1) {
            for (int i = h; i < n; i++) {
                for (int j = i; j >= h && less(nums[j], nums[j - h]); j -= h) {
                    swap(nums, j, j - h);
                }
            }
            h = h / 3;
        }
    }
}

归并排序

/**
 * 归并
 * 稳定
 * O(nlogn)
 * @param <T>
 */
public abstract class MergeSort<T extends Comparable<T>> extends Sort<T> {
    protected T[] aux;

    public MergeSort(String name) {
        super(name);
    }

    protected void merge(T[] nums, int left, int m, int right) {
        // merge [left, m][m+1, right]
        int i = left;
        int j = m + 1;
        for (int k = left; k <= right ; k++) {
            aux[k] = nums[k];
        }

        for (int k = left; k <= right ; k++) {
            if (i > m) {
                nums[k] = aux[j++];
            } else if (j > right) {
                nums[k] = aux[i++];
            } else if (aux[i].compareTo(aux[j]) <= 0) {
                nums[k] = aux[i++];
            } else {
                nums[k] = aux[j++];
            }
        }

    }

}
/**
 * @author admin
 */
public class Up2DownMergeSort<T extends Comparable<T>> extends MergeSort<T> {
    public Up2DownMergeSort(String name) {
        super(name);
    }

    @Override
    protected void sort(T[] nums) {
        this.aux = (T[]) new Comparable[nums.length];
        sort(nums, 0, nums.length - 1);
    }

    private void sort(T[] nums, int left, int right) {

        if (right - left < 1) {
            return;
        }
        int mid = left + (right - left) / 2;
        sort(nums, left, mid);
        sort(nums, mid + 1, right);
        merge(nums, left, mid, right);
    }
}
/**
 * @author admin
 */
public class Down2UpMergeSort<T extends Comparable<T>> extends MergeSort<T> {

    public Down2UpMergeSort(String name) {
        super(name);
    }

    @Override
    protected void sort(T[] nums) {
        int N = nums.length;
        aux = (T[]) new Comparable[N];

        for (int i = 1; i < N; i = i*2) {
            for (int j = 0; j < N - i; j += i*2) {
                merge(nums, j, j + i - 1, Math.min(j + i + i - 1, N - 1));
            }
        }
    }
}

快速排序

public class Quick<T extends Comparable<T>> extends Sort<T> {

    public Quick(String name) {
        super(name);
    }

    @Override
    protected void sort(T[] nums) {
        int N = nums.length;
        sort(nums, 0, N - 1);
    }

    protected void sort(T[] nums, int left, int end) {
        if (end - left < 1) {
            return;
        }
        int partition = quick(nums, left, end);
        sort(nums, left, partition - 1);
        sort(nums, partition + 1, end);
    }

    private int quick(T[] nums, int left, int end) {
        int i = left;
        int j = end + 1;
        T temp = nums[left];

        while (true) {
            while (less(temp, nums[--j]) && j > i) {
                continue;
            }
            while (less(nums[++i], temp) && j > i) {
                continue;
            }
            if (i >= j) {
                break;
            }
            swap(nums, i, j);
        }
        swap(nums, left, j);
        return j;
    }
}

三向切分快速排序

/**
 * 三向切分快速排序
 * 适用于大量重复元素
 * @author admin
 */
public class ThreeWayQuick<T extends Comparable<T>> extends Quick<T> {

    public ThreeWayQuick(String name) {
        super(name);
    }

    @Override
    protected void sort(T[] nums, int left, int end) {
        if (end - left < 1) {
            return;
        }
        int lt = left, i = left + 1, gt = end;
        T temp = nums[left];
        while (i <= gt) {
            int compare = nums[i].compareTo(temp);
            if (compare < 0) {
                swap(nums, i, lt);
                i++;
                lt++;
            } else if (compare > 0) {
                swap(nums, i, gt);
                gt--;
            } else {
                i++;
            }
        }

        sort(nums, left, lt - 1);
        sort(nums, gt + 1, end);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值