回到主题,现在分析一下常见的排序算法的稳定性,每个都给出简单的理由。
(1)冒泡排序
冒泡排序就是把小的元素往前调或者把大的元素往后调。比较是相邻的两个元素比较,交换也发生在这两个元素之间。所以,如果两个元素相等,我想你是不会再无聊地把他们俩交换一下的;如果两个相等的元素没有相邻,那么即使通过前面的两两交换把两个相邻起来,这时候也不会交换,所以相同元素的前后顺序并没有改变,所以冒泡排序是一种稳定排序算法。
(2)选择排序
选择排序是给每个位置选择当前元素最小的,比如给第一个位置选择最小的,在剩余元素里面给第二个元素选择第二小的,依次类推,直到第n - 1个元素,第n个元素不用选择了,因为只剩下它一个最大的元素了。那么,在一趟选择,如果当前元素比一个元素小,而该小的元素又出现在一个和当前元素相等的元素后面,那么交换后稳定性就被破坏了。比较拗口,举个例子,序列5 8 5 2 9,我们知道第一遍选择第1个元素5会和2交换,那么原序列中2个5的相对前后顺序就被破坏了,所以选择排序不是一个稳定的排序算法。
(3)插入排序
插入排序是在一个已经有序的小序列的基础上,一次插入一个元素。当然,刚开始这个有序的小序列只有1个元素,就是第一个元素。比较是从有序序列的末尾开始,也就是想要插入的元素和已经有序的最大者开始比起,如果比它大则直接插入在其后面,否则一直往前找直到找到它该插入的位置。如果碰见一个和插入元素相等的,那么插入元素把想插入的元素放在相等元素的后面。所以,相等元素的前后顺序没有改变,从原无序序列出去的顺序就是排好序后的顺序,所以插入排序是稳定的。
(4)快速排序
快速排序有两个方向,左边的i下标一直往右走,当a[i] <= a[center_index],其中center_index是中枢元素的数组下标,一般取为数组第0个元素。而右边的j下标一直往左走,当a[j] > a[center_index]。如果i和j都走不动了,i <= j,交换a[i]和a[j],重复上面的过程,直到i > j。 交换a[j]和a[center_index],完成一趟快速排序。在中枢元素和a[j]交换的时候,很有可能把前面的元素的稳定性打乱,比如序列为5 3 3 4 3 8 9 10 11,现在中枢元素5和3(第5个元素,下标从1开始计)交换就会把元素3的稳定性打乱,所以快速排序是一个不稳定的排序算法,不稳定发生在中枢元素和a[j] 交换的时刻。
(5)归并排序
归并排序是把序列递归地分成短序列,递归出口是短序列只有1个元素(认为直接有序)或者2个序列(1次比较和交换),然后把各个有序的段序列合并成一个有序的长序列,不断合并直到原序列全部排好序。可以发现,在1个或2个元素时,1个元素不会交换,2个元素如果大小相等也没有人故意交换,这不会破坏稳定性。那么,在短的有序序列合并的过程中,稳定是是否受到破坏?没有,合并过程中我们可以保证如果两个当前元素相等时,我们把处在前面的序列的元素保存在结果序列的前面,这样就保证了稳定性。所以,归并排序也是稳定的排序算法。
(6)基数排序
基数排序是按照低位先排序,然后收集;再按照高位排序,然后再收集;依次类推,直到最高位。有时候有些属性是有优先级顺序的,先按低优先级排序,再按高优先级排序,最后的次序就是高优先级高的在前,高优先级相同的低优先级高的在前。基数排序基于分别排序,分别收集,所以其是稳定的排序算法。
(7)希尔排序(shell)
希尔排序是按照不同步长对元素进行插入排序,当刚开始元素很无序的时候,步长最大,所以插入排序的元素个数很少,速度很快;当元素基本有序了,步长很小, 插入排序对于有序的序列效率很高。所以,希尔排序的时间复杂度会比O(n^2)好一些。由于多次插入排序,我们知道一次插入排序是稳定的,不会改变相同元素的相对顺序,但在不同的插入排序过程中,相同的元素可能在各自的插入排序中移动,最后其稳定性就会被打乱,所以shell排序是不稳定的。
(8)堆排序
我们知道堆的结构是节点i的孩子为2 * i和2 * i + 1节点,大顶堆要求父节点大于等于其2个子节点,小顶堆要求父节点小于等于其2个子节点。在一个长为n 的序列,堆排序的过程是从第n / 2开始和其子节点共3个值选择最大(大顶堆)或者最小(小顶堆),这3个元素之间的选择当然不会破坏稳定性。但当为n / 2 - 1, n / 2 - 2, ... 1这些个父节点选择元素时,就会破坏稳定性。有可能第n / 2个父节点交换把后面一个元素交换过去了,而第n / 2 - 1个父节点把后面一个相同的元素没 有交换,那么这2个相同的元素之间的稳定性就被破坏了。所以,堆排序不是稳定的排序算法。
综上,得出结论: 选择排序、快速排序、希尔排序、堆排序不是稳定的排序算法,而冒泡排序、插入排序、归并排序和基数排序是稳定的排序算法
其实,我觉得任何一些算法稳定性是根据你写的代码来判断是否稳定的。
下面给出我用java实现的八大排序算法:
- <span style="font-size:12px;">public class Sort
- {
- /*
- * 首先,排序算法的稳定性大家应该都知道, 通俗地讲就是能保证排序前2个相等的数其在序列的前后位置顺序和排序后它们两个的前后位置顺序相同。
- * 在简单形式化一下,如果A[i] = A[j],A[i]原来在位置前,排序后A[i]还是要在A[j]位置前,这才能叫稳定排序。
- */
- public static void main(String[] args)
- {
- int[] array =
- { 4, 12, 15, 17, 8, 12, 10, 41, 22, 19, 69, 35, 68, 1 };
- //{5,1,7,8,4,2,3};
- // bubbleSort(array);
- // selectSort(array);
- // insertSort(array);
- //quickSort(array);
- //binarySort(array);
- //heapSort(array);
- //radixSort(array);
- shellSort(array);
- print(array);
- }
- /*
- * 冒泡排序
- * 原理:两两比较,大的向后面冒泡
- * 稳定排序:时间复杂度为O(n^2)
- */
- public static void bubbleSort(int[] array)
- {
- for (int i = 0; i < array.length; i++)
- for (int j = 0; j < array.length - i - 1; j++)
- if (array[j] > array[j + 1])
- {
- int temp = array[j + 1];
- array[j + 1] = array[j];
- array[j] = temp;
- }
- }
- /*
- * 选择排序
- * 原理:总是选择一个最小值或者最大值作为待排序的元素
- * 不稳定排序:时间复杂度O(n^2)
- */
- public static void selectSort(int[] array)
- {
- for (int i = 0; i < array.length; i++)
- for (int j = i; j < array.length - 1; j++)
- {
- if (array[i] > array[j + 1])
- {
- int temp = array[i];
- array[i] = array[j + 1];
- array[j + 1] = temp;
- }
- }
- }
- /*
- * 插入排序
- * 原理:已排序的数组为有序的序列,将待排序的元素插入到该已有序的序列中
- * 稳定排序:时间复杂度O(n^2)
- */
- public static void insertSort(int[] array)
- {
- for (int i = 1; i < array.length; i++)
- for (int j = i - 1; j > -1; j--)
- {
- if (array[j] > array[j + 1])
- {
- int temp = array[j];
- array[j] = array[j + 1];
- array[j + 1] = temp;
- }
- }
- }
- /*
- * 快速排序
- * 原理:挖坑填数+分治法。将中枢数的位置看做一个坑,合适的数字填到坑里面后,形成一个新的坑
- * 不稳定排序:时间复杂度O(nlgn)
- */
- public static void quickSort(int[] array)
- {
- quickSort(array, 0, array.length - 1);
- }
- private static void quickSort(int[] array, int start, int end)
- {
- if (start < end)
- {
- // 选择中枢元素,比中枢元素大的放在有右边,反正左边
- int i = start, j = end;
- int x = array[i];
- while (i < j)
- {
- // 从end处开始比较
- while (i < j && x < array[j])
- j--;
- if (i < j)
- array[i++] = array[j];
- while (i < j && x > array[i])
- i++;
- if (i < j)
- array[j--] = array[i];
- }
- array[i] = x;
- quickSort(array, start, i - 1);
- quickSort(array, i + 1, end);
- }
- }
- /*
- * 归并排序
- * 原理:二分排序,先将数组二分,然后合并排序
- * 稳定排序:时间复杂度(nlgn)
- */
- public static void binarySort(int []array)
- {
- binarySort(array,0,array.length-1);
- }
- private static void binarySort(int []array,int left,int right)
- {
- if(left==right)//只有一个元素
- return;
- else if(left+1==right)//两个元素,直接排序
- {
- if(array[left]>array[right])
- {
- int temp=array[left];
- array[left]=array[right];
- array[right]=temp;
- }
- }
- else
- {
- int mid=(left+right)/2;
- binarySort(array,left,mid);
- binarySort(array,mid+1,right);
- merge(array, left, right,mid);
- }
- }
- //将两个有序队列合并
- private static void merge(int []array,int left,int right,int mid)
- {
- for(int i=mid+1;i<=right;i++)
- for(int j=i-1;j>=left;j--)
- {
- while(array[j+1]<array[j])
- {
- int temp=array[j+1];
- array[j+1]=array[j];
- array[j]=temp;
- break;
- }
- }
- }
- /*
- * 堆排序
- * 原理:构造最大或者最小堆,然后逐个删除根元素
- * 不稳定排序,时间复杂度最差:时间复杂度(nlgn)
- * 其实很麻烦,但是效率高。首先要构建堆,然后才删除根节点排序
- */
- public static void heapSort(int []array)
- {
- //构建一个最大堆
- //构建堆的原理:从后面开始插入。
- //构建一个空根,比根小则直接插入,比根大,则将根下移,插入节点上滤
- for(int i=1;i<array.length;i++)
- {
- int k=i;
- while(k>0&&array[(k-1)/2]<array[k])
- {
- int temp=array[k];
- array[k]=array[(k-1)/2];
- array[(k-1)/2]=temp;
- k=(k-1)/2;
- }
- }
- //print(array);
- //将堆排序
- //删除根元素,为了节省空间,可以把根元素放在最后的位置
- for(int i=0;i<array.length;i++)
- {
- int max=array[0];
- int k=0;
- while(2*k+1<array.length-i-1)
- {
- if(array[2*k+1]>array[2*k+2])
- {
- array[k]=array[2*k+1];
- k=2*k+1;
- }
- else {
- array[k]=array[2*k+2];
- k=2*k+2;
- }
- }
- array[k]=array[array.length-i-1];
- array[array.length-i-1]=max;
- //可能出现一种意外 的情形,需要调整k的位置
- //
- if(k<array.length-i-1)
- {
- while(k>0&&array[(k-1)/2]<array[k])
- {
- int temp=array[k];
- array[k]=array[(k-1)/2];
- array[(k-1)/2]=temp;
- k=(k-1)/2;
- }
- }
- }
- }
- /*
- * 基数排序,分为MSD和LST两种
- * MSD:从最高位开始排序
- * LSD:从最低位开始排序
- * 原理:分别将每个位置上(个十百)的数字排序
- * 稳定排序:时间复杂度O(n)
- *优点:时间复杂度低
- *缺点:浪费大量空间,对数字分配要求比较高,越是均匀分配,空间消耗越低
- */
- public static void radixSort(int []array)
- {
- //10倍空间,因为不知道是否是平均分配
- //当然,某个位置上的数字全部是一样的话,那么就是极端情况,需要10倍空间
- int []temp=new int[10*array.length];
- for(int i=0;i<temp.length;i++)//初始化
- temp[i]=0;
- //假设待排序的数最大就是两位数
- //对个位数排序
- for(int i=0;i<array.length;i++)
- {
- int d1=getDigit(array[i], 1);
- //int d2=getDigit(array[i], 2);
- int offset=0;
- while(temp[d1*array.length+offset]!=0)
- {
- offset++;
- }
- temp[d1*array.length+offset]=array[i];
- }
- int pos=0;
- for(int i=0;i<temp.length;i++)
- {
- if(temp[i]!=0)
- {
- array[pos]=temp[i];
- pos++;
- }
- temp[i]=0;
- }
- //对十位上数排序
- for(int i=0;i<array.length;i++)
- {
- int d2=getDigit(array[i], 2);
- int offset=0;
- while(temp[d2*array.length+offset]!=0)
- {
- offset++;
- }
- temp[d2*array.length+offset]=array[i];
- }
- pos=0;
- for(int i=0;i<temp.length;i++)
- {
- if(temp[i]!=0)
- {
- array[pos]=temp[i];
- pos++;
- }
- temp[i]=0;
- }
- }
- /**
- * 得到number的第d位上的数字
- * @param number
- * @param d
- * @return
- */
- private static int getDigit(int number,int d)
- {
- while(d>1)
- {
- d--;
- number/=10;
- }
- return number%10;
- }
- /*
- * 希尔排序
- * 原理:分组插入排序,选择增量,变化增量
- * 不稳定排序。
- */
- public static void shellSort(int []array)
- {
- int d=array.length;//增量
- while(d>0)
- {
- d=d/2;
- for(int j=d;j<array.length;j+=d)
- {
- if(array[j]<array[j-d])
- {
- int temp=array[j];
- int k=j-d;
- while(k>=0&&temp<array[k])
- {
- array[k+d]=array[k];
- k-=d;
- }
- array[k+d]=temp;
- }
- }
- }
- }
- public static void print(int []array)
- {
- for (int e : array)
- {
- System.out.print(e + " ");
- }
- System.out.println();
- }
- }</span>