十大排序算法

十大排序算法

十大排序算法 冒泡排序、选择排序、插入排序、希尔排序、归并排序、快速排序、堆排序、计数排序、桶排序、基数排序。


字典:

  • n:数据规模
  • k:桶的大小(整数范围)
  • 稳定性:排序后两个相等元素的顺序和排序之前是否相同
java 复制代码
// 交换元素
public static void swap(int[] ints, int m, int n) {
    int temp = ints[m];
    ints[m] = ints[n];
    ints[n] = temp;
}

1、冒泡排序

java 复制代码
// 冒泡排序
public static int[] bubbleSort(int[] ints) {
    for (int i = 0; i < ints.length - 1; i++) {
        for (int j = 0; j < ints.length - 1 - i; j++) {
            if (ints[j] > ints[j + 1]) {
                swap(ints, j, j + 1);
            }
        }
    }
    return ints;
}

// 鸡尾酒排序(冒泡的升级版)
public static int[] cocktailSort(int[] ints) {
    for (int i = 0; i < ints.length / 2; i++) {

        for (int j = i; j < ints.length - 1 - i; j++) {
            if (ints[j] > ints[j + 1]) {
                swap(ints, j, j + 1);
            }
        }

        for (int j = ints.length - 1 - (i + 1); j > i; j--) {
            if (ints[j] < ints[j - 1]) {
                swap(ints, j, j - 1);
            }
        }
    }
    return ints;
}

2、选择排序

java 复制代码
// 选择排序
public static int[] selectionSort(int[] ints) {
    for (int i = 0; i < ints.length - 1; i++) {

        int minIndex = i;

        for (int j = i; j < ints.length - 1; j++) {
            if (ints[j + 1] < ints[minIndex]) {
                minIndex = j + 1;
            }
        }

        if (minIndex != i) {
            swap(ints, i, minIndex);
        }
    }
    return ints;
}

3、插入排序

java 复制代码
// 插入排序
public static int[] insertionSort(int[] ints) {
    for (int i = 1; i < ints.length; i++) {
        for (int j = i; j > 0; j--) {
            if (ints[j] < ints[j - 1]) {
                swap(ints, j - 1, j);
            }
        }
    }
    return ints;
}

4、希尔排序

java 复制代码
// 希尔排序(插入的升级版)
public static int[] shellSort(int[] ints) {
    for (int step = ints.length / 2; step > 0; step /= 2) {
        for (int i = step; i < ints.length; i++) {
            for (int j = i; j >= step; j -= step) {
                if (ints[j] < ints[j - step]) {
                    swap(ints, j -step, j);
                }
            }
        }
    }
    return ints;
}

5、归并排序

java 复制代码
// 归并排序
public static int[] mergeSort(int[] ints, int startIndex, int endIndex) {
    if (startIndex == endIndex) {
        return new int[] { ints[startIndex] };
    }

    int midIndex = startIndex + (endIndex - startIndex) / 2;
    int[] left = mergeSort(ints, startIndex, midIndex);
    int[] right = mergeSort(ints, midIndex + 1, endIndex);

    int[] temp = new int[left.length + right.length];

    int m = 0, n = 0, index = 0;
    while (m < left.length && n < right.length) {
        temp[index++] = left[m] < right[n] ? left[m++] : right[n++];
    }

    while (m < left.length) {
        temp[index++] = left[m++];
    }

    while (n < right.length) {
        temp[index++] = right[n++];
    }
    return temp;
}

6、快速排序

java 复制代码
// 快速排序
public static void quickSort(int[] ints, int startIndex, int endIndex) {
    if (startIndex > endIndex) {
        return;
    }

    int key = ints[startIndex];
    int i = startIndex, j = endIndex;

    while (i < j) {
        while (i < j && ints[j] > key) {
            j--;
        }

        while (i < j && ints[i] < key) {
            i++;
        }

        if (i < j) {
            swap(ints, i, j);
        }
    }

    ints[startIndex] = ints[i];
    ints[i] = key;

    quickSort(ints, startIndex, i - 1);
    quickSort(ints, i + 1, endIndex);
}

7、堆排序

java 复制代码
// 堆排序
public static int[] heapSort(int[] ints) {
    for (int i = ints.length / 2 - 1; i >= 0; i--) {
        adjustHeap(ints, i, ints.length);
    }

    for (int i = ints.length - 1; i > 0; i--) {
        swap(ints, 0, i);
        adjustHeap(ints, 0, i);
    }
    return ints;
}

public static void adjustHeap(int[] ints, int index, int length) {
    int maxIndex = index;
    int leftIndex = 2 * index + 1;
    int rightIndex = 2 * index + 2;

    if (leftIndex < length && ints[leftIndex] > ints[maxIndex]) {
        maxIndex = leftIndex;
    }

    if (rightIndex < length && ints[rightIndex] > ints[maxIndex]) {
        maxIndex = rightIndex;
    }

    if (maxIndex != index) {
        swap(ints, index, maxIndex);
        adjustHeap(ints, maxIndex, length);
    }
}

8、计数排序

java 复制代码
// 计数排序
public static int[] countingSort(int[] ints) {
    int max = ints[0], min = ints[0];
    for (int item : ints) {
        max = Math.max(max, item);
        min = Math.min(min, item);
    }

    int k = max - min + 1;
    int[] temp = new int[k];
    for (int item : ints) {
        temp[item - min]++;
    }

    int index = 0;
    for (int i = 0; i < temp.length; i++) {
        while (temp[i] > 0) {
            ints[index++] = i + min;
            temp[i]--;
        }
    }
    return ints;
}

9、桶排序

java 复制代码
// 桶排序
public static int[] bucketSort(int[] ints, int capacity) {
    int max = ints[0], min = ints[0];
    for (int item : ints) {
        max = Math.max(max, item);
        min = Math.min(min, item);
    }

    int k = (max - min) / capacity + 1;
    int[][] temp = new int[k][0];
    for (int item : ints) {
        int bucketIndex = (item - min) / capacity;
        int[] bucket = Arrays.copyOf(temp[bucketIndex], temp[bucketIndex].length + 1);
        bucket[bucket.length - 1] = item;
        temp[bucketIndex] = bucket;
    }

    int index = 0;
    for (int[] bucket : temp) {
        if (bucket.length == 0) {
            continue;
        }

        bubbleSort(bucket);

        for (int item : bucket) {
            ints[index++] = item;
        }
    }
    return ints;
}

10、基数排序

java 复制代码
// 基数排序
public static int[] radixSort(int[] ints) {
    int maxAbs = Math.abs(ints[0]);
    for (int item : ints) {
        int abs = Math.abs(item);
        maxAbs = Math.max(maxAbs, abs);
    }

    int maxDigit = 0;
    while (maxAbs != 0) {
        maxAbs /= 10;
        maxDigit++;
    }

    for (int i = 0, m = 10, n = 1; i < maxDigit; i++, m *= 10, n *= 10) {

        int[][] temp = new int[10 * 2][0];

        for (int item : ints) {
            int bucketIndex = (item % m) / n + 10;
            int[] bucket = Arrays.copyOf(temp[bucketIndex], temp[bucketIndex].length + 1);
            bucket[bucket.length - 1] = item;
            temp[bucketIndex] = bucket;
        }

        int index = 0;
        for (int[] bucket : temp) {
            if (bucket.length == 0) {
                continue;
            }

            for (int item : bucket) {
                ints[index++] = item;
            }
        }
    }
    return ints;
}

@XGLLHZ - 陈慧娴 《夜机》.mp3

相关推荐
confiself13 分钟前
大模型系列——LLAMA-O1 复刻代码解读
java·开发语言
Wlq041518 分钟前
J2EE平台
java·java-ee
XiaoLeisj24 分钟前
【JavaEE初阶 — 多线程】Thread类的方法&线程生命周期
java·开发语言·java-ee
为什么这亚子33 分钟前
九、Go语言快速入门之map
运维·开发语言·后端·算法·云原生·golang·云计算
豪宇刘39 分钟前
SpringBoot+Shiro权限管理
java·spring boot·spring
43 分钟前
开源竞争-数据驱动成长-11/05-大专生的思考
人工智能·笔记·学习·算法·机器学习
Elaine20239143 分钟前
02多线程基础知识
java·多线程
gorgor在码农1 小时前
Redis 热key总结
java·redis·热key
~yY…s<#>1 小时前
【刷题17】最小栈、栈的压入弹出、逆波兰表达式
c语言·数据结构·c++·算法·leetcode
百事老饼干1 小时前
Java[面试题]-真实面试
java·开发语言·面试