常用经典排序算法

本文介绍了七种经典的排序算法:快速排序、希尔排序、归并排序、冒泡排序、选择排序、插入排序,并提供了一组用于生成随机数组及校验数组是否有序的辅助函数。

经典排序算法

1.快速排序

package com.hooware.sorttest.quick;

import com.hooware.RandomNumber;

import java.util.Arrays;

@SuppressWarnings("all")
public class Quickly {
    public static void main(String[] args) {
        int[] array = RandomNumber.getRandomArray(10000000);
        //System.out.println(Arrays.toString(array));
        Quickly quickly = new Quickly();
        long start = System.currentTimeMillis();
        quickly.quickSort(array,0,array.length-1);
        long end = System.currentTimeMillis();
        System.out.println("================= \n The array is ordered? : " + RandomNumber.isOrderedArray(array) + "! consume " + (end-start)/1000.0 + " s!");
//        System.out.println(Arrays.toString(array));
    }

    /**
     *
     * @param array
     * @param l 数组左边界
     * @param r 数组右边界
     */
    public void quickSort(int[] array, int l, int r) {
        if (l < r) {
            int mid = partition(array,l,r);
            quickSort(array,l,mid-1);
            quickSort(array,mid+1,r);
        }
    }

    /**
     *
     * @param array
     * @param l 数组左边界
     * @param r 数组右边界
     * @return 返回pivot基准值索引
     */
    private int partition(int[] array, int l, int r) {
        int pivot = l;
        int index = pivot + 1;
        for (int i = index; i <= r; i++) {
            if (array[i] < array[pivot]){
                swap(array,i,index);
                index++;
            }
        }
        swap(array,pivot,index-1);
        return index-1;
    }

    /**
     * @param array
     * @param i
     * @param j
     */
    public void swap(int[] array, int i, int j) {
        int temp = array[i];
        array[i] = array[j];
        array[j] = temp;
    }
}

2.希尔排序

package com.hooware.sorttest.shell;

import com.hooware.RandomNumber;
import com.hooware.sorttest.merge.MergeSort;

public class ShellSort {
    public static void main(String[] args) {
        int[] array = RandomNumber.getRandomArray(10000000);
        //System.out.println(Arrays.toString(array));
        ShellSort shellSort = new ShellSort();
        long start = System.currentTimeMillis();
        shellSort.shellSort(array);
        long end = System.currentTimeMillis();
        System.out.println("================= \n The array is ordered? : " + RandomNumber.isOrderedArray(array) + "! consume " + (end - start) / 1000.0 + " s!");
        //System.out.println(Arrays.toString(sortArray));
    }

    private void shellSort(int[] array) {
        for (int gap = array.length / 2; gap > 0; gap /= 2) {
            for (int i = gap; i < array.length; i++) {
                int j = i;
                while (j - gap >= 0 && array[j] < array[j - gap]) {
                    swap(array, j - gap, j);
                    j -= gap;
                }
            }
        }
    }

    /**
     * @param array
     * @param i
     * @param j
     */
    public void swap(int[] array, int i, int j) {
        int temp = array[i];
        array[i] = array[j];
        array[j] = temp;
    }
}

3.归并排序

package com.hooware.sorttest.merge;

import com.hooware.RandomNumber;

import java.util.Arrays;

public class MergeSort {

    public static void main(String[] args) {
        int[] array = RandomNumber.getRandomArray(100000);
        //System.out.println(Arrays.toString(array));
        MergeSort mergeSort = new MergeSort();
        long start = System.currentTimeMillis();
        int[] sortArray = mergeSort.mergeSort(array);
        long end = System.currentTimeMillis();
        System.out.println("================= \n The array is ordered? : " + RandomNumber.isOrderedArray(sortArray) + "! consume " + (end-start)/1000.0 + " s!");
        //System.out.println(Arrays.toString(sortArray));
    }

    /**
     *
     * @param array
     * @return
     */
    private int[] mergeSort(int[] array) {
        int[] arr = Arrays.copyOf(array,array.length);

        if (array.length < 2) {
            return array;
        }

        int mid = array.length/2;
        int[] left = Arrays.copyOfRange(array,0,mid);
        int[] right = Arrays.copyOfRange(array,mid,array.length);
        return merge(mergeSort(left),mergeSort(right));
    }

    /**
     *
     * @param left
     * @param right
     * @return
     */
    private int[] merge(int[] left, int[] right) {
        int[] result = new int[left.length+right.length];
        int i = 0;
        while (left.length > 0 && right.length > 0) {
            if (left[0] < right[0]) {
                result[i] = left[0];
                i++;
                left = Arrays.copyOfRange(left,1,left.length);
            } else {
                result[i] = right[0];
                i++;
                right = Arrays.copyOfRange(right,1,right.length);
            }
        }
        while (left.length > 0 ) {
            result[i] = left[0];
            i++;
            left = Arrays.copyOfRange(left,1,left.length);
        }
        while (right.length > 0 ) {
            result[i] = right[0];
            i++;
            right = Arrays.copyOfRange(right,1,right.length);
        }
        return result;
    }

}

4.冒泡排序

package com.mysort;

import java.util.Arrays;

public class BubbleSort {
    private int[] array = {23, 11, 7, 29, 33, 59, 8, 20, 9, 3, 2, 6, 10, 44, 83, 28, 5, 1, 0};

    public static void main(String[] args) {
        BubbleSort bubbleSort = new BubbleSort();
        System.out.println("Initial Array : " + Arrays.toString(bubbleSort.array));
        bubbleSort.bubble(bubbleSort.array);
        System.out.println("Sorted Array : " + Arrays.toString(bubbleSort.array));
        System.out.println("-----------------------");
        int[] arr1 = SortAlgsCheck.getRandomArray(10000);
        bubbleSort.bubble(arr1);
        boolean flag = SortAlgsCheck.testIsPass(arr1);
        System.out.println("test is success? " + flag);
    }

    public void bubble(int[] array) {
        int len = array.length;
        for (int i = 0; i < len - 1; i++) {
            for (int j = 0; j < len - 1 - i; j++) {
                if (array[j] > array[j+1]) {
                    int temp = array[j];
                    array[j] = array[j+1];
                    array[j+1] = temp;
                }
            }
        }
    }
}

5.选择排序

package com.mysort;

import java.util.Arrays;

public class SelectSort {
    private int[] array = {23, 11, 7, 29, 33, 59, 8, 20, 9, 3, 2, 6, 10, 44, 83, 28, 5, 1, 0};

    public static void main(String[] args) {
        SelectSort selectSort = new SelectSort();
        System.out.println("Initial Array : " + Arrays.toString(selectSort.array));
        selectSort.select(selectSort.array);
        System.out.println("Sorted Array : " + Arrays.toString(selectSort.array));
        System.out.println("----------------------");
        int[] arr1 = SortAlgsCheck.getRandomArray(10000);
//        System.out.println("Initial Array : " + Arrays.toString(arr1));
        selectSort.select(arr1);
        boolean flag = SortAlgsCheck.testIsPass(arr1);
        System.out.println("test is success? " + flag);
    }

    public void select(int[] array) {
        int len = array.length;
        for (int i = 0; i < len - 1; i++) {
            for (int j = i + 1; j < len; j++) {
                if(array[i] > array[j]){
                    int temp = array[i];
                    array[i] = array[j];
                    array[j] = temp;
                }
            }
        }
    }
}

6.插入排序

package com.mysort;

import java.util.Arrays;

public class InsertSort {

    private int[] array = {23, 11, 7, 29, 33, 59, 8, 20, 9, 3, 2, 6, 10, 44, 83, 28, 5, 1, 0, 36};

    public static void main(String[] args) {
        InsertSort insert = new InsertSort();
        System.out.println("Initial Array : " + Arrays.toString(insert.array));
        insert.insertSort(insert.array);
        System.out.println("Sorted Array : " + Arrays.toString(insert.array));
        System.out.println("--------------");
        int[] arr1 = SortAlgsCheck.getRandomArray(10000);
//        insert.insertSort(arr1);
        insert.insert(arr1);
        boolean flag = SortAlgsCheck.testIsPass(arr1);
        System.out.println("test is success? " + flag);
    }

    public void insertSort(int[] arr) {
        int temp;   // 保存临时变量
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = i + 1; j > 0; j--) {
                if (arr[j] < arr[j - 1]) {    // 交换两者位置
                    temp = arr[j];
                    arr[j] = arr[j - 1];
                    arr[j - 1] = temp;
                }
            }
        }
    }

    public void insert(int[] array) {
        for (int i = 1; i < array.length; i++) {
            for (int j = i; j > 0; j--) {
                if(array[j-1] > array[j]){
                    int temp = array[j-1];
                    array[j-1] = array[j];
                    array[j] = temp;
                }
            }
        }
    }
}

7.随机数组生成及校验数组有序函数

package com.mysort;

import java.util.Random;

public class SortAlgsCheck {

    public static int[] getRandomArray(int len) {
        int[] array = new int[len];
        Random random = new Random();
        for (int i = 0; i < array.length; i++) {
            array[i] = random.nextInt(Integer.MAX_VALUE);
        }
        return array;
    }

    public static boolean testIsPass(int[] array) {
        int len = array.length;
        for (int i = 1; i < len; i++) {
            if (array[i-1] > array[i]) {
                return false;
            }
        }
        return true;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值