十大排序算法

十大排序算法

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


字典:

  • 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

相关推荐
吾日三省吾码2 小时前
JVM 性能调优
java
LNTON羚通3 小时前
摄像机视频分析软件下载LiteAIServer视频智能分析平台玩手机打电话检测算法技术的实现
算法·目标检测·音视频·监控·视频监控
弗拉唐3 小时前
springBoot,mp,ssm整合案例
java·spring boot·mybatis
oi774 小时前
使用itextpdf进行pdf模版填充中文文本时部分字不显示问题
java·服务器
少说多做3434 小时前
Android 不同情况下使用 runOnUiThread
android·java
知兀4 小时前
Java的方法、基本和引用数据类型
java·笔记·黑马程序员
哭泣的眼泪4084 小时前
解析粗糙度仪在工业制造及材料科学和建筑工程领域的重要性
python·算法·django·virtualenv·pygame
蓝黑20205 小时前
IntelliJ IDEA常用快捷键
java·ide·intellij-idea
Ysjt | 深5 小时前
C++多线程编程入门教程(优质版)
java·开发语言·jvm·c++