1、利用 JAVA 实现数据结构中常用的插入排序和快速排序算法 在网上看的,挺全的,收了先。 。第十章 排序源程序:Data.javapackage Sort;class Data Comparable key;Object value;public Data() public Data(Data data)this.key=data.key;this.value=data.value;public Data(Comparable key,Object value)this.key=key;this.value=value;public String toString()return “key=“
2、+key+“;“+“value=“+value+“;“+“n“;Insertion.javapackage Sort;public class InsertionSort public InsertionSort() /直接插入排序,从下标 1 开始public static void straightInsertionSort(Data data) int i, j;for (i = 2; i =high+1;j-)dataj+1=dataj;datahigh+1=data0;/插入/表插入排序public static void ListInsertionSort(Data data)in
3、t i,j,k;/inner class:Tableclass TableComparable key;int next;Table table=new Tabledata.length;for(i=1;i 1) lastChangeIndex = 1;for (j = 1; j = 0) high-;datalow = datahigh;while (low 0; i-) HeapAdjust(data, i, data.length-1); /建立大顶堆for (i = (data.length - 1); i 1; i-) temp = data1;data1 = datai;datai
4、 = temp;HeapAdjust(data, 1, i - 1);private static void HeapAdjust(Data data, int start, int end) int j;Data temp;temp = datastart;for (j = 2 * start; j = 0) break;datastart = dataj;start = j;datastart = temp;/简单选择排序public static void SimpleSelectSort(Data data) int i, j;Data temp;for (i = 1; i 0) te
5、mp = datai.key;j = i;return j;/归并排序private static Data originalnewData2;private static Data newData2;public static void MergingSort(Data data) originalnewData2 = new Datadata.length;newData2 = new Datadata.length;for (int i = 1; i 7000 ,内存溢出) :100 500 1000 3000 6000普通快排 10 10 80 250 991优化快排 0 40 20 160 731由此可知,优化快排能缓解恶化。