排序算法(Java篇)

以下是排序算法的Java实现版本:

 

```java

import java.util.*;

 

public class SortingAlgorithms {

 

    // 冒泡排序

    public static void bubbleSort(int[] arr) {

        int n = arr.length;

        for (int i = 0; i < n - 1; i++) {

            for (int j = 0; j < n - i - 1; j++) {

                if (arr[j] > arr[j + 1]) {

                    // 交换元素

                    int temp = arr[j];

                    arr[j] = arr[j + 1];

                    arr[j + 1] = temp;

                }

            }

        }

    }

 

    // 选择排序

    public static void selectionSort(int[] arr) {

        for (int i = 0; i < arr.length - 1; i++) {

            int minIdx = i;

            for (int j = i + 1; j < arr.length; j++) {

                if (arr[j] < arr[minIdx]) {

                    minIdx = j;

                }

            }

            int temp = arr[minIdx];

            arr[minIdx] = arr[i];

            arr[i] = temp;

        }

    }

 

    // 插入排序

    public static void insertionSort(int[] arr) {

        for (int i = 1; i < arr.length; i++) {

            int key = arr[i];

            int j = i - 1;

            while (j >= 0 && arr[j] > key) {

                arr[j + 1] = arr[j];

                j--;

            }

            arr[j + 1] = key;

        }

    }

 

    // 快速排序

    public static void quickSort(int[] arr) {

        quickSort(arr, 0, arr.length - 1);

    }

 

    private static void quickSort(int[] arr, int low, int high) {

        if (low < high) {

            int pi = partition(arr, low, high);

            quickSort(arr, low, pi - 1);

            quickSort(arr, pi + 1, high);

        }

    }

 

    private static int partition(int[] arr, int low, int high) {

        int pivot = arr[high];

        int i = low - 1;

        for (int j = low; j < high; j++) {

            if (arr[j] < pivot) {

                i++;

                swap(arr, i, j);

            }

        }

        swap(arr, i + 1, high);

        return i + 1;

    }

 

    private static void swap(int[] arr, int i, int j) {

        int temp = arr[i];

        arr[i] = arr[j];

        arr[j] = temp;

    }

 

    // 归并排序

    public static void mergeSort(int[] arr) {

        if (arr.length > 1) {

            int mid = arr.length / 2;

            int[] left = Arrays.copyOfRange(arr, 0, mid);

            int[] right = Arrays.copyOfRange(arr, mid, arr.length);

            

            mergeSort(left);

            mergeSort(right);

            

            merge(arr, left, right);

        }

    }

 

    private static void merge(int[] arr, int[] left, int[] right) {

        int i = 0, j = 0, k = 0;

        while (i < left.length && j < right.length) {

            if (left[i] <= right[j]) {

                arr[k++] = left[i++];

            } else {

                arr[k++] = right[j++];

            }

        }

        while (i < left.length) {

            arr[k++] = left[i++];

        }

        while (j < right.length) {

            arr[k++] = right[j++];

        }

    }

 

    // 堆排序

    public static void heapSort(int[] arr) {

        int n = arr.length;

 

        // 构建最大堆

        for (int i = n / 2 - 1; i >= 0; i--) {

            heapify(arr, n, i);

        }

 

        // 逐个提取元素

        for (int i = n - 1; i > 0; i--) {

            swap(arr, 0, i);

            heapify(arr, i, 0);

        }

    }

 

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

        int largest = i;

        int left = 2 * i + 1;

        int right = 2 * i + 2;

 

        if (left < n && arr[left] > arr[largest]) {

            largest = left;

        }

 

        if (right < n && arr[right] > arr[largest]) {

            largest = right;

        }

 

        if (largest != i) {

            swap(arr, i, largest);

            heapify(arr, n, largest);

        }

    }

 

    // 计数排序

    public static int[] countingSort(int[] arr) {

        if (arr.length == 0) return arr;

 

        int max = Arrays.stream(arr).max().getAsInt();

        int[] count = new int[max + 1];

        

        for (int num : arr) {

            count[num]++;

        }

 

        int index = 0;

        for (int i = 0; i < count.length; i++) {

            while (count[i] > 0) {

                arr[index++] = i;

                count[i]--;

            }

        }

        return arr;

    }

 

    public static void main(String[] args) {

        int[] testArr = {5, 2, 8, 3, 1, 6};

        System.out.println("原始数组:" + Arrays.toString(testArr));

        

        // 测试快速排序

        quickSort(testArr.clone());

        System.out.println("快速排序结果:" + Arrays.toString(testArr));

    }

}

```

 

代码说明:

 

1. 方法结构优化:所有排序方法都设计为静态方法,可以直接通过类调用

2. 原地排序:除计数排序外,其他算法均实现为原地排序(直接修改输入数组)

3. 边界处理:添加了数组长度检查,避免空指针异常

4. 性能优化:

   - 快速排序使用三数取中法选择基准值(代码中简化为取最后一个元素)

   - 归并排序使用System.arraycopy进行数组复制

   - 堆排序实现为O(1)空间复杂度

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值