Java排序算法-持续更新中

一、比较排序

1.1 交换排序

数组两元素交换位置

java 复制代码
public class ArrayUtil {

    /**
     * 交换数组中的两个元素
     * @param array 数组
     * @param ele1Idx 元素1的索引下标
     * @param ele2Idx 元素1的索引下表
     */
    public static void swap(int[] array, int ele1Idx, int ele2Idx) {
        int tmp = array[ele1Idx];
        array[ele1Idx] = array[ele2Idx];
        array[ele2Idx] = tmp;
    }
}

1.1.1 冒泡排序

java 复制代码
public class BubbleSort {

    public static void main(String[] args) {
        int[] nums = {12, 34, 11, 98, 56, 49, 21, 29, 19};
        System.out.println("排序前: " + Arrays.toString(nums));
        new BubbleSort().bubbleSort(nums);
        System.out.println("排序后: " + Arrays.toString(nums));
    }

    private void bubbleSort(int[] nums) {
        for (int i = 0; i < nums.length - 1; i++) {
            for (int j = 0; j < nums.length - 1 - i; j++) {
                if (nums[j] > nums[j + 1]) {
                    ArrayUtil.swap(nums, j, j + 1);
                }
            }
        }
    }
}

1.1.2 快速排序

java 复制代码
public class QuickSort {

    public static void main(String[] args) {
        int[] nums = {12, 34, 11, 98, 56, 49, 21, 29, 19};
        System.out.println("排序前: " + Arrays.toString(nums));
        new QuickSort().quickSort(nums, 0, nums.length - 1);
        System.out.println("排序后: " + Arrays.toString(nums));
    }

    private void quickSort(int[] nums, int start, int end) {
        if (start > end) return;
        int i = start, j = end, base = nums[start];
        while (i < j) {
            while (j >= 0 && nums[j] >= base) j--;
            while (i < j && nums[i] <= base) i++;
            if (i < j) {
                ArrayUtil.swap(nums, i, j);
            }
        }
        ArrayUtil.swap(nums, start, i);
        quickSort(nums, start, i - 1);
        quickSort(nums, i + 1, end);
    }
}

1.2 插入排序

1.2.1 简单插入排序

java 复制代码
public class InsertionSort {

    public static void main(String[] args) {
        int[] nums = {12, 34, 11, 98, 56, 49, 21, 29, 19};
        System.out.println("排序前: " + Arrays.toString(nums));
        new InsertionSort().insertionSort(nums);
        System.out.println("排序后: " + Arrays.toString(nums));
    }

    private void insertionSort(int[] nums) {
        for (int i = 1; i < nums.length; i++) {
            int j, temp = nums[i];
            for (j = i; j > 0 && nums[j - 1] > temp; j--) {
                nums[j] = nums[j - 1];
            }
            nums[j] = temp;
        }
    }
}

1.2.2 希尔排序

java 复制代码
public class ShellSort {

    public static void main(String[] args) {
        int[] nums = {12, 34, 11, 98, 56, 49, 21, 29, 19};
        System.out.println("排序前: " + Arrays.toString(nums));
        new ShellSort().shellSort(nums);
        System.out.println("排序后: " + Arrays.toString(nums));
    }

    private void shellSort(int[] nums) {
        // 缩小增量排序
        for (int gap = nums.length / 2; gap >= 1; gap /= 2) {
            for (int i = gap; i < nums.length; i++) {
                int j, temp = nums[i];
                for (j = i; j >= gap && nums[j - gap] > temp; j-=gap) {
                    nums[j] = nums[j - gap];
                }
                nums[j] = temp;
            }
        }
    }
}

1.3 选择排序

1.3.1 简单选择排序

java 复制代码
public class SelectionSort {

    public static void main(String[] args) {
        int[] nums = {12, 34, 11, 98, 56, 49, 21, 29, 19};
        System.out.println("排序前: " + Arrays.toString(nums));
        new SelectionSort().selectionSort(nums);
        System.out.println("排序后: " + Arrays.toString(nums));
    }

    private void selectionSort(int[] nums) {
        for (int i = 0; i < nums.length - 1; i++) {
            for (int j = i + 1; j < nums.length; j++) {
                if (nums[i] > nums[j]) {
                    ArrayUtil.swap(nums, i, j);
                }
            }
        }
    }
}

1.3.2 堆排序

java 复制代码
public class HeapSort {

    public static void main(String[] args) {
        int[] nums = {12, 34, 11, 98, 56, 49, 21, 29, 19};
        System.out.println("排序前: " + Arrays.toString(nums));
        new HeapSort().headSort(nums);
        System.out.println("排序后: " + Arrays.toString(nums));
    }

    private void headSort(int[] nums) {
        int n = nums.length;
        buildHeap(nums, n);
        for (int i = n - 1; i >= 0 ; i--) {
            swap(nums, i, 0);
            heapify(nums, i, 0);
        }
    }

    private void buildHeap(int[] tree, int n) {
        int lastNodeIndex = n - 1;
        int parentNodeIndex = (lastNodeIndex - 1) / 2;
        for (int i = parentNodeIndex; i >= 0; i--) {
            heapify(tree, n, i);
        }
    }

    private void heapify(int[] tree, int n, int i) {
        if (i > n) {
            return;
        }
        int l = 2 * i + 1;
        int r = 2 * i + 2;
        int max = i;
        if (l < n && tree[l] > tree[max]) {
            max = l;
        }
        if (r < n && tree[r] > tree[max]) {
            max = r;
        }
        if (max != i) {
            swap(tree, max, i);
            heapify(tree, n, max);
        }
    }

    private void swap(int[] nums, int left, int right) {
        int temp = nums[left];
        nums[left] = nums[right];
        nums[right] = temp;
    }
}

1.4 归并排序

1.4.1 二路归并排序

java 复制代码
public class MergeSort {

    public static void main(String[] args) {
        int[] nums = {6, 8, 10, 9, 4, 5, 2, 7};
        new MergeSort().mergeSort(nums, 0, nums.length - 1);
        System.out.println(Arrays.toString(nums));
    }

    private void mergeSort(int[] arr, int l, int r) {
        if (l == r) return;
        int m = (l + r) / 2;
        mergeSort(arr, l, m);
        mergeSort(arr, m + 1, r);
        merge(arr, l, m + 1, r);
    }

    private void merge(int[] arr, int l, int m, int r) {
        int lSize = m - l;
        int rSize = r - m + 1;
        int[] lArr = new int[lSize];
        int[] rArr = new int[rSize];
        // 1.fill in the left sub array
        if (m - l >= 0) System.arraycopy(arr, l, lArr, 0, lSize);
        // 2.fill in the right sub array
        if (r + 1 - m >= 0) System.arraycopy(arr, m, rArr, 0, rSize);
        // 3.merge into the original array
        int i = 0, j = 0, k = l;
        while (i < lSize && j < rSize) {
            if (lArr[i] < rArr[j]) {
                arr[k++] = lArr[i++];
            } else {
                arr[k++] = rArr[j++];
            }
        }
        while (i < lSize) {
            arr[k++] = lArr[i++];
        }
        while (j < rSize) {
            arr[k++] = rArr[j++];
        }
    }
}

1.4.2 多路归并排序

java 复制代码
待更新...

二、非比较排序

2.1 计数排序

java 复制代码
public class CountingSort {

    public static void main(String[] args) {
        int[] nums = {12, 34, 11, 98, 56, 49, 21, 29, 19};
        System.out.println("排序前: " + Arrays.toString(nums));
        new CountingSort().countingSort(nums);
        System.out.println("排序后: " + Arrays.toString(nums));
    }

    public void countingSort(int[] nums) {
        int[] mm = getMaxMin(nums);
        int[] cnt = new int[mm[1] - mm[0] + 1];
        for (int num : nums) {
            cnt[num - mm[0]] += 1;
        }
        int idx = 0;
        for (int i = 0; i < cnt.length; i++) {
            for (int j = 0; j < cnt[i]; j++) {
                nums[idx++] = i + mm[0];
            }
        }
    }

    private static int[] getMaxMin(int[] nums) {
        int max = nums[0], min = nums[0];
        for (int i = 1; i < nums.length; i++) {
            if (nums[i] > max) max = nums[i];
            if (nums[i] < min) min = nums[i];
        }
        return new int[]{min, max};
    }
}

2.2 桶排序

java 复制代码
public class BucketSort {

    public static void main(String[] args) {
        int[] nums = {12, 34, 11, 98, 56, 49, 21, 29, 19};
        System.out.println("排序前: " + Arrays.toString(nums));
        new BucketSort().bucketSort(nums);
        System.out.println("排序后: " + Arrays.toString(nums));
    }

    public void bucketSort(int[] nums) {
        if (nums == null || nums.length == 0) {
            return;
        }

        // 找出数组中的最大值和最小值
        int[] mm = getMaxMin(nums);

        // 初始化桶数组
        int listSize = mm[1] - mm[0] + 1;
        List<List<Integer>> buckets = new ArrayList<>(listSize);
        for (int i = 0; i < listSize; i++) {
            buckets.add(new ArrayList<>());
        }

        // 将数据分配到桶中
        for (int num : nums) {
            buckets.get(num - mm[0]).add(num);
        }

        // 对每个桶进行排序,这里使用了简单的Insertion Sort
        for (List<Integer> numList : buckets) {
            if (!numList.isEmpty()) {
                Collections.sort(numList);
            }
        }

        // 从桶中收集已排序的数据
        int k = 0;
        for (List<Integer> bucket : buckets) {
            for (Integer value : bucket) {
                nums[k++] = value;
            }
        }
    }

    private static int[] getMaxMin(int[] nums) {
        int max = nums[0], min = nums[0];
        for (int i = 1; i < nums.length; i++) {
            if (nums[i] > max) max = nums[i];
            if (nums[i] < min) min = nums[i];
        }
        return new int[]{min, max};
    }
}

2.3 基数排序

java 复制代码
public class RadixSort {

    public static void main(String[] args) {
        int[] nums = {12, 34, 11, 98, 56, 49, 21, 29, 19};
        System.out.println("排序前: " + Arrays.toString(nums));
        new RadixSort().radixSort(nums);
        System.out.println("排序后: " + Arrays.toString(nums));
    }

    public void radixSort(int[] nums) {
        int[] mm = getMaxMin(nums);
        int len = String.valueOf(mm[1]).length();
        @SuppressWarnings("unchecked")
        LinkedList<Integer>[] digits = new LinkedList[19];
        for (int powCount = 0; powCount < len; powCount++) {
            for (int num : nums) {
                int idx = (int) Math.abs(num / Math.pow(10, powCount) % 10);
                idx = num >= 0 ? 9 + idx : 9 - idx;
                if (digits[idx] == null) digits[idx] = new LinkedList<>();
                digits[idx].add(num);
            }
            for (int i = 0, j = 0; j < digits.length; j++) {
                while (digits[j] != null && !digits[j].isEmpty()) {
                    nums[i++] = digits[j].removeFirst();
                }
            }
        }
    }

    private static int[] getMaxMin(int[] nums) {
        int max = nums[0], min = nums[0];
        for (int i = 1; i < nums.length; i++) {
            if (nums[i] > max) max = nums[i];
            if (nums[i] < min) min = nums[i];
        }
        return new int[]{min, max};
    }
}
相关推荐
小李不想输啦2 小时前
什么是微服务、微服务如何实现Eureka,网关是什么,nacos是什么
java·spring boot·微服务·eureka·架构
张铁铁是个小胖子2 小时前
微服务学习
java·学习·微服务
ggs_and_ddu2 小时前
Android--java实现手机亮度控制
android·java·智能手机
敲代码娶不了六花4 小时前
jsp | servlet | spring forEach读取不了对象List
java·spring·servlet·tomcat·list·jsp
Yhame.4 小时前
深入理解 Java 中的 ArrayList 和 List:泛型与动态数组
java·开发语言
是小崔啊5 小时前
开源轮子 - EasyExcel02(深入实践)
java·开源·excel
myNameGL6 小时前
linux安装idea
java·ide·intellij-idea
青春男大6 小时前
java栈--数据结构
java·开发语言·数据结构·学习·eclipse
HaiFan.6 小时前
SpringBoot 事务
java·数据库·spring boot·sql·mysql
我要学编程(ಥ_ಥ)6 小时前
一文详解“二叉树中的深搜“在算法中的应用
java·数据结构·算法·leetcode·深度优先