常用的排序算法

来源:互联网 发布:vmware装mac os 编辑:程序博客网 时间:2024/06/05 19:39

简介

排序的目的是将一组“无序”的记录序列调整为“有序”的记录序列,当待排序记录的关键字都不相同时,排序结果是惟一的,否则排序结果不惟一。在待排序的文件中,若存在多个关键字相同的记录,经过排序后这些具有相同关键字的记录之间的相对次序保持不变,该排序方法是稳定的;若具有相同关键字的记录之间的相对次序发生改变,则称这种排序方法是不稳定的。要注意的是排序算法的稳定性是针对所有输入实例而言的,即在所有可能的输入实例中,只要有一个实例使得算法不满足稳定性要求,则该排序算法就是不稳定的。其中冒泡、直接插入、基数、归并排序属于稳定排序,简单选择、快速、希尔、堆排序属于不稳定排序。

排序分为内排序和外排序两种。若整个排序过程不需要访问外存便能完成,则称此类排序为内部排序。反之若参加排序的记录数量很大,整个序列的排序过程不可能在内存中完成,必须根据排序过程的要求不断在内、外存之间移动,则称此类排序为外部排序。内排序按所用策略不同可归纳为五类:插入排序(直接插入排序、希尔排序)、选择排序(简单选择排序、堆排序)、交换排序(冒泡排序、快速排序)、归并排序、基数排序。

常用排序

1.直接插入排序

每次从无序表中取出第一个元素,把它插入到有序表的合适位置,使有序表仍然有序。第一趟比较前两个数,然后把第二个数按大小插入到有序表中;第二趟把第三个数与前两个数从后向前扫描,把第三个数按大小插入到有序表中;依次进行下去,进行了(n-1)趟扫描以后就完成了整个排序过程。

基本思想:每步将一个待排序的记录按其关键字的大小插到前面已经排序的序列中的适当位置,直到全部记录插入完毕为止。

直接插入排序

代码实现:

/** * @Description 直接插入排序 */public static void insertSort() {    int array[] = {49, 38, 65, 97, 76, 13, 27, 54, 56, 17, 18, 23};    int temp = 0;    //第0位独自作为有序数列,从第1位开始向后遍历    for (int i = 1; i < array.length; i++) {        //[0,i-1]都是有序的,如果待插入元素比arr[i-1]还大则无需再与[i-1]前面的元素进行比较了,反之则进入if语句        if (array[i] < array[i - 1]) {            int j = i - 1;            //保存第i位的值            temp = array[i];            //从第i-1位向前遍历并移位,直至找到小于第i位值停止            for (; j >= 0 && temp < array[j]; j--) {                //把比temp大或相等的元素全部往后移动一个位置                array[j + 1] = array[j];            }            //把待排序的元素temp插入腾出位置的(j+1)            array[j + 1] = temp;        }    }}

2.希尔排序

希尔排序是基于插入排序的以下两点性质而提出改进方法的:
1. 插入排序在对几乎已经排好序的数据操作时效率高,即可以达到线性排序的效率;
2. 插入排序一般来说是低效的,因为插入排序每次只能将数据移动一位。

基本思想:算法先将要排序的一组数按某个增量dn/2n为要排序数的个数)分成若干组,每组中记录的下标相差d,对每组中全部元素进行直接插入排序,然后再用一个较小的增量(d/2)对它进行分组,在每组中再进行直接插入排序。当增量减到1时,进行直接插入排序后排序完成。

希尔排序

代码实现:

/** * @Description 希尔排序(最小增量排序) */public static void shellSort() {    int array[] = {4, 54, 6, 39, 25, 34, 12, 45, 56, 10};    double d1 = array.length;    int temp = 0;    while (true) {        d1 = Math.ceil(d1 / 2);        int d = (int) d1;        for (int x = 0; x < d; x++) {            for (int i = x + d; i < array.length; i += d) {                int j = i - d;                temp = array[i];                for (; j >= 0 && temp < array[j]; j -= d) {                    array[j + d] = array[j];                }                array[j + d] = temp;            }        }        if (d == 1)            break;    }}

3.简单选择排序

在要排序的一组数中,选出最小的一个数与第一个位置的数交换,然后在剩下的数当中再找最小的与第二个位置的数交换,如此循环到倒数第二个数和最后一个数比较为止。

简单选择排序

代码实现:

/** * @Description 简单选择排序 */public static void selectSort() {    int array[] = {1, 54, 46, 73, 78, 34, 12, 45};    int position = 0;    for (int i = 0; i < array.length; i++) {        int j = i + 1;        position = i;        int temp = array[i];        for (; j < array.length; j++) {            if (array[j] < temp) {                temp = array[j];                position = j;            }        }        array[position] = array[i];        array[i] = temp;    }}

4.堆排序

堆排序是一种树形选择排序,是对直接选择排序的有效改进。

堆排序

代码实现:

/** * @Description 堆排序 */public static void heapSort() {    int array[] = {49, 38, 65, 97, 76, 13, 27, 78, 34, 12, 64, 25, 53, 51};    int arrayLength = array.length;    //循环建堆      for (int i = 0; i < arrayLength - 1; i++) {        //建堆(对data数组从0到lastIndex建大顶堆)        buildMaxHeap(array, arrayLength - 1 - i);        //交换堆顶和最后一个元素          swap(array, 0, arrayLength - 1 - i);    }}public static void swap(int[] data, int i, int j) {    int tmp = data[i];    data[i] = data[j];    data[j] = tmp;}public static void buildMaxHeap(int[] data, int lastIndex) {    //从lastIndex处节点(最后一个节点)的父节点开始    for (int i = (lastIndex - 1) / 2; i >= 0; i--) {        //k保存正在判断的节点          int k = i;        //如果当前k节点的子节点存在          while (k * 2 + 1 <= lastIndex) {            //k节点的左子节点的索引              int biggerIndex = 2 * k + 1;            //如果biggerIndex小于lastIndex,即biggerIndex+1代表的k节点的右子节点存在              if (biggerIndex < lastIndex) {                //若果右子节点的值较大                  if (data[biggerIndex] < data[biggerIndex + 1]) {                    //biggerIndex总是记录较大子节点的索引                      biggerIndex++;                }            }            //如果k节点的值小于其较大的子节点的值              if (data[k] < data[biggerIndex]) {                //交换他们                  swap(data, k, biggerIndex);                //将biggerIndex赋予k,开始while循环的下一次循环,重新保证k节点的值大于其左右子节点的值                  k = biggerIndex;            } else {                break;            }        }    }}

5.冒泡排序

算法原理如下:
1.比较相邻的元素。如果第一个比第二个大,就交换他们两个;
2.对每一对相邻元素作同样的工作,从开始第一对到结尾的最后一对。在这一点,最后的元素应该会是最大的数;
3.针对所有的元素重复以上的步骤,除了最后一个;
4.持续每次对越来越少的元素重复上面的步骤,直到没有任何一对数字需要比较。

基本思想:在要排序的一组数中,对当前还未排好序范围内的全部数,自上而下对相邻的两个数依次进行比较和调整,让较大的数往下沉,较小的往上冒。即:每当两相邻的数比较后发现它们的排序与排序要求相反时,就将它们互换。

冒泡排序

代码实现:

/** * @Description 冒泡排序 */public static void bubbleSort() {    int array[] = {49, 38, 65, 97, 76, 13, 12, 64, 34, 15, 35, 25, 53, 51};    int temp = 0;    for (int i = 0; i < array.length - 1; i++) {        for (int j = 0; j < array.length - 1 - i; j++) {            if (array[j] > array[j + 1]) {                temp = array[j];                array[j] = array[j + 1];                array[j + 1] = temp;            }        }    }}

6.快速排序

基本思想:选择一个基准元素,通常选择第一个元素或者最后一个元素,通过一趟扫描将待排序列分成两部分,一部分比基准元素小,一部分大于等于基准元素,此时基准元素在其排好序后的正确位置,然后再用同样的方法递归地排序划分的两部分。

快速排序

代码实现:

/** * @Description 快速排序 */public static void quickSort() {    int array[] = {69, 38, 76, 13, 27, 49, 78, 15, 35, 25, 53, 51};    quick(array);}public static int getMiddle(int[] list, int low, int high) {    int tmp = list[low];    //数组的第一个作为中轴    while (low < high) {        while (low < high && list[high] >= tmp) {            high--;        }        list[low] = list[high];   //比中轴小的记录移到低端        while (low < high && list[low] <= tmp) {            low++;        }        list[high] = list[low];   //比中轴大的记录移到高端    }    list[low] = tmp;              //中轴记录到尾    return low;                   //返回中轴的位置}public static void _quickSort(int[] list, int low, int high) {    if (low < high) {        int middle = getMiddle(list, low, high);  //将list数组进行一分为二        _quickSort(list, low, middle - 1);        //对低字表进行递归排序        _quickSort(list, middle + 1, high);       //对高字表进行递归排序    }}public static void quick(int[] a2) {    //查看数组是否为空    if (a2.length > 0) {        _quickSort(a2, 0, a2.length - 1);    }}

7.归并排序

基本排序:归并(Merge)排序法是将两个(或两个以上)有序表合并成一个新的有序表,即把待排序序列分为若干个子序列,每个子序列是有序的,然后再把有序子序列合并为整体有序序列。

归并排序

代码实现:

/** * @Description 归并排序 */public static void mergingSort() {    int array[] = {49, 34, 12, 64, 62, 98, 54, 56, 17, 18, 23, 34, 15, 51};    sort(array, 0, array.length - 1);}public static void sort(int[] data, int left, int right) {    if (left < right) {        //找出中间索引        int center = (left + right) / 2;        //对左边数组进行递归        sort(data, left, center);        //对右边数组进行递归        sort(data, center + 1, right);        //合并        merge(data, left, center, right);    }}public static void merge(int[] data, int left, int center, int right) {    int[] tmpArr = new int[data.length];    int mid = center + 1;    //third记录中间数组的索引    int third = left;    int tmp = left;    while (left <= center && mid <= right) {        //从两个数组中取出最小的放入中间数组        if (data[left] <= data[mid]) {            tmpArr[third++] = data[left++];        } else {            tmpArr[third++] = data[mid++];        }    }    //剩余部分依次放入中间数组    while (mid <= right) {        tmpArr[third++] = data[mid++];    }    while (left <= center) {        tmpArr[third++] = data[left++];    }    //将中间数组中的内容复制回原数组    while (tmp <= right) {        data[tmp] = tmpArr[tmp++];    }}

8.基数排序

基本思想:将所有待比较数值(正整数)统一为同样的数位长度,数位较短的数前面补零。然后从最低位开始依次进行一次排序,这样从最低位排序一直到最高位排序完成以后,数列就变成一个有序序列。

基数排序

代码实现:

/** * @Description 基数排序 */public static void radixSort() {    int array[] = {65, 13, 27, 49, 78, 12, 64, 62, 99, 54, 17, 18, 23, 34};    //首先确定排序的趟数    int max = array[0];    for (int i = 1; i < array.length; i++) {        if (array[i] > max) {            max = array[i];        }    }    int time = 0;    //判断位数    while (max > 0) {        max /= 10;        time++;    }    //建立10个队列    List<ArrayList> queue = new ArrayList<>();    for (int i = 0; i < 10; i++) {        ArrayList<Integer> queue1 = new ArrayList<>();        queue.add(queue1);    }    //进行time次分配和收集    for (int i = 0; i < time; i++) {        //分配数组元素        for (int j = 0; j < array.length; j++) {            //得到数字的第time+1位数            int x = array[j] % (int) Math.pow(10, i + 1) / (int) Math.pow(10, i);            ArrayList<Integer> queue2 = queue.get(x);            queue2.add(array[j]);            queue.set(x, queue2);        }        //元素计数器        int count = 0;        //收集队列元素        for (int k = 0; k < 10; k++) {            while (queue.get(k).size() > 0) {                ArrayList<Integer> queue3 = queue.get(k);                array[count] = queue3.get(0);                queue3.remove(0);                count++;            }        }    }}

项目地址 ☞ 传送门

0 0
原创粉丝点击