几种稳定排序算法
对Java中的一些稳定算法进行了整理,使其符合自己的编程习惯。 [code="java"] package com.study.sort; import java.util.LinkedList; public class StableSort { /** * 生成一个随机数组 * @param length 数组长度 * @return 生成的数组 */ public static int[] init(int length){ int [] before=new int[length]; Random rand=new Random(47); for(int i=0;i=0&&key0;i--){ for(int j=0;jbefore[j+1]){ int temp=before[j+1]; before[j+1]=before[j]; before[j]=temp; } } for(int j=(before.length-1-(i+1));j>i;j--){ if(before[j] temp = new LinkedList(); int indexA=lowerBound; int indexB=mid; while(indexA<=(mid-1)&&indexB<=upperBound){ if(before[indexA]0){ before[lowerBound+index]=temp.poll(); index++; } } /** * 打印数组内容到控制台 * @param before 待显示数组 */ public static void prt(int[] before){ for(int x:before){ System.out.print(x+" "); } System.out.println(""); } public static void main(String[] args){ int[] before=init(10); prt(before); // insertSort(before); // bubbleSort(before); // cocktailSort(before); mergeSort(before); prt(before); } } [/code]