使用Java实现7大排序算法

Java代码

import java.util.Arrays;

public class Sort {

    public Sort() {

    }

    /**
     * 1. 选择排序
     *
     * @param arr 需要排序的数组
     */
    public void selectSort(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            int min = i;
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[min] > arr[j]) {
                    min = j;
                }
            }
            swap(arr, min, i);
        }
    }

    /**
     * 2. 插入排序
     *
     * @param arr 需要排序的数组
     */
    public void insertSort(int[] arr) {
        for (int i = 1; i < arr.length; i++) {
            int cur = arr[i];
            int j = i - 1;
            while (j >= 0 && arr[j] > cur) {
                arr[j + 1] = arr[j];
                j--;
            }
            arr[j + 1] = cur;
        }
    }

    /**
     * 3. 冒泡排序
     *
     * @param arr 需要排序的数组
     */
    public void bubbleSort(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr.length - i - 1; j++) {
                if (arr[j] > arr[j + 1]) {
                    swap(arr, j, j + 1);
                }
            }
        }
    }

    /**
     * 4. 希尔排序
     *
     * @param arr 需要排序的数组
     */
    public void shellSort(int[] arr) {
        int dk = arr.length / 2;
        for (dk = arr.length / 2; dk > 0; dk = dk / 2) {
            for (int i = dk; i < arr.length; i++) {
                if (arr[i] < arr[i - dk]) {
                    int idx = i - dk;
                    int cur = arr[i];
                    arr[i] = arr[i - dk];
                    while (idx >= 0 && arr[idx] > cur) {
                        arr[idx + dk] = arr[idx];
                        idx = idx - dk;
                    }
                    arr[idx + dk] = cur;
                }
            }
        }
    }

    /**
     * 5. 堆排序
     *
     * @param arr 需要排序的数组
     */
    public void heapSort(int[] arr) {
        for (int i = arr.length / 2; i >= 0; i--) {
            percDown(arr, i, arr.length);
        }
        for (int i = arr.length - 1; i > 0; i--) {
            swap(arr, 0, i);
            percDown(arr, 0, i);
        }
    }

    private void percDown(int[] arr, int i, int n) {

        int temp = arr[i];

        for (int child; leftChild(i) < n; i = child) {
            child = leftChild(i);
            if (child != n - 1 && arr[child] < arr[child + 1]) {
                child++;
            }
            if (temp < arr[child]) {
                arr[i] = arr[child];
            } else {
                break;
            }
        }
        arr[i] = temp;
    }

    private int leftChild(int i) {
        return 2 * i + 1;
    }

    /**
     * 6. 归并排序
     *
     * @param arr 需要排序的数组
     */
    public void mergeSort(int[] arr) {
        int[] tmp = new int[arr.length];
        mergeSortHelper(arr, tmp, 0, arr.length - 1);
    }

    private void mergeSortHelper(int[] arr, int[] tmp, int left, int right) {
        if (left < right) {
            int mid = (left + right) / 2;
            mergeSortHelper(arr, tmp, left, mid);
            mergeSortHelper(arr, tmp, mid + 1, right);
            merge(arr, tmp, left, mid + 1, right);
        }
    }

    private void merge(int[] arr, int[] tmp, int left, int right, int rightEnd) {
        int leftEnd = right - 1;
        int tmpPos = left;
        int numElements = rightEnd - left + 1;

        while (left <= leftEnd && right <= rightEnd) {
            if (arr[left] < arr[right]) {
                tmp[tmpPos++] = arr[left++];
            } else {
                tmp[tmpPos++] = arr[right++];
            }
        }
        while (left <= leftEnd) {
            tmp[tmpPos++] = arr[left++];
        }
        while (right <= rightEnd) {
            tmp[tmpPos++] = arr[right++];
        }

        for (int i = 0; i < numElements; i++, rightEnd--) {
            arr[rightEnd] = tmp[rightEnd];
        }
    }

    /**
     * 7. 快速排序
     *
     * @param arr 需要排序的数组
     */
    public void quickSort(int[] arr) {
        qsort(arr, 0, arr.length - 1);
    }

    private void qsort(int[] arr, int low, int high) {
        if (low < high) {
            int pivot = partition(arr, low, high);
            qsort(arr, low, pivot - 1);
            qsort(arr, pivot + 1, high);
        }
    }

    private int partition(int[] arr, int low, int high) {
        int pivot = arr[low];
        while (low < high) {
            while (low < high && arr[high] >= pivot) {
                --high;
            }
            arr[low] = arr[high];
            while (low < high && arr[low] <= pivot) {
                ++low;
            }
            arr[high] = arr[low];
        }
        arr[low] = pivot;
        return low;
    }

    private static void swap(int[] arr, int i, int j) {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }

    public static void printArray(int[] arr) {
        if (arr == null) {
            return;
        }
        System.out.println(Arrays.toString(arr));
    }
}

测试

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

/**
 * Created by Allen on 2016/8/21.
 */
public class SortTest {

    private static int[] arr = {5, 7, 3, 4, 8, 9, 1, 2, 6};
    private static int[] ascArr = {1, 2, 3, 4, 5, 6, 7, 8, 9};
    private static int[] descArr = {9, 8, 7, 6, 5, 4, 3, 2, 1};

    private Sort sort;

    public SortTest() {
        sort = new Sort();
    }

    @Before
    public void init() {

    }

    @Test
    public void selectSortTest() {
        int[] tmp = new int[arr.length];
        System.arraycopy(arr, 0, tmp, 0, arr.length);
        sort.selectSort(tmp);
        Assert.assertArrayEquals(tmp, ascArr);
    }

    @Test
    public void insertSortTest() {
        int[] tmp = new int[arr.length];
        System.arraycopy(arr, 0, tmp, 0, arr.length);
        sort.insertSort(tmp);
        Assert.assertArrayEquals(tmp, ascArr);
    }

    @Test
    public void bubbleSortTest() {
        int[] tmp = new int[arr.length];
        System.arraycopy(arr, 0, tmp, 0, arr.length);
        sort.bubbleSort(tmp);
        Assert.assertArrayEquals(tmp, ascArr);
    }

    @Test
    public void shellSortTest() {
        int[] tmp = new int[arr.length];
        System.arraycopy(arr, 0, tmp, 0, arr.length);
        sort.shellSort(tmp);
        Assert.assertArrayEquals(tmp, ascArr);
    }

    @Test
    public void heapSortTest() {
        int[] tmp = new int[arr.length];
        System.arraycopy(arr, 0, tmp, 0, arr.length);
        sort.heapSort(tmp);
        Assert.assertArrayEquals(tmp, ascArr);
    }

    @Test
    public void mergeSortTest() {
        int[] tmp = new int[arr.length];
        System.arraycopy(arr, 0, tmp, 0, arr.length);
        sort.mergeSort(tmp);
        Assert.assertArrayEquals(tmp, ascArr);
    }

    @Test
    public void quickSortTest() {
        int[] tmp = new int[arr.length];
        System.arraycopy(arr, 0, tmp, 0, arr.length);
        sort.quickSort(tmp);
        Assert.assertArrayEquals(tmp, ascArr);
    }

}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值