java的排序算法,代码详细说明

java的排序算法,代码详细说明

Java中常见的排序算法有以下几种:

  1. 冒泡排序(Bubble Sort):
java 复制代码
public static void bubbleSort(int[] array) {
    int n = array.length;
    for (int i = 0; i < n-1; i++) {
        for (int j = 0; j < n-i-1; j++) {
            if (array[j] > array[j+1]) {
                int temp = array[j];
                array[j] = array[j+1];
                array[j+1] = temp;
            }
        }
    }
}
  1. 选择排序(Selection Sort):
java 复制代码
public static void selectionSort(int[] array) {
    int n = array.length;
    for (int i = 0; i < n-1; i++) {
        int minIndex = i;
        for (int j = i+1; j < n; j++) {
            if (array[j] < array[minIndex]) {
                minIndex = j;
            }
        }
        int temp = array[i];
        array[i] = array[minIndex];
        array[minIndex] = temp;
    }
}
  1. 插入排序(Insertion Sort):
java 复制代码
public static void insertionSort(int[] array) {
    int n = array.length;
    for (int i = 1; i < n; i++) {
        int key = array[i];
        int j = i - 1;
        while (j >= 0 && array[j] > key) {
            array[j+1] = array[j];
            j--;
        }
        array[j+1] = key;
    }
}
  1. 快速排序(Quick Sort):
java 复制代码
public static void quickSort(int[] array, int low, int high) {
    if (low < high) {
        int pivotIndex = partition(array, low, high);
        quickSort(array, low, pivotIndex - 1);
        quickSort(array, pivotIndex + 1, high);
    }
}

public static int partition(int[] array, int low, int high) {
    int pivot = array[high];
    int i = low - 1;

    for (int j = low; j < high; j++) {
        if (array[j] < pivot) {
            i++;
            int temp = array[i];
            array[i] = array[j];
            array[j] = temp;
        }
    }

    int temp = array[i+1];
    array[i+1] = array[high];
    array[high] = temp;
    return i + 1;
}
  1. 归并排序(Merge Sort):
java 复制代码
public static void mergeSort(int[] array, int low, int high) {
    if (low < high) {
        int mid = (low + high) / 2;
        mergeSort(array, low, mid);
        mergeSort(array, mid + 1, high);
        merge(array, low, mid, high);
    }
}

public static void merge(int[] array, int low, int mid, int high) {
    int[] temp = new int[high - low + 1];
    int i = low, j = mid+1, k = 0;

    while (i <= mid && j <= high) {
        if (array[i] <= array[j]) {
            temp[k++] = array[i++];
        } else {
            temp[k++] = array[j++];
        }
    }

    while (i <= mid) {
        temp[k++] = array[i++];
    }

    while (j <= high) {
        temp[k++] = array[j++];
    }

    for (int index = 0; index < temp.length; index++) {
        array[low + index] = temp[index];
    }
}

这些是Java中常见的排序算法,每个算法都有其优缺点,适用于不同的场景和数据规模。例如,冒泡排序和选择排序适用于小数据规模,快速排序和归并排序适用于大数据规模。

相关推荐
扎瓦7 分钟前
ThreadLocal 线程变量
java·后端
BillKu26 分钟前
Java后端检查空条件查询
java·开发语言
jackson凌30 分钟前
【Java学习笔记】String类(重点)
java·笔记·学习
刘白Live1 小时前
【Java】谈一谈浅克隆和深克隆
java
一线大码1 小时前
项目中怎么确定线程池的大小
java·后端
要加油哦~1 小时前
vue · 插槽 | $slots:访问所有命名插槽内容 | 插槽的使用:子组件和父组件如何书写?
java·前端·javascript
Q8137574601 小时前
中阳视角下的资产配置趋势分析与算法支持
算法
crud1 小时前
Spring Boot 3 整合 Swagger:打造现代化 API 文档系统(附完整代码 + 高级配置 + 最佳实践)
java·spring boot·swagger
天天摸鱼的java工程师1 小时前
从被测试小姐姐追着怼到运维小哥点赞:我在项目管理系统的 MySQL 优化实战
java·后端·mysql
yvestine1 小时前
自然语言处理——文本表示
人工智能·python·算法·自然语言处理·文本表示