堆排序Java实现
排序父类
-
排序模板用于继承
Sort.java
package MYSORT;
public class Sort {
public static void sort(Comparable[] a, boolean printProcess)
{}
public static boolean less(Comparable v, Comparable w)
{ return v.compareTo(w) < 0; }
public static void exchange(Comparable[] a, int i, int j)
{
Comparable t = a[i];
a[i] = a[j];
a[j] = t;
}
public static void print(Comparable[] a)
{
String status = "Disordered";
if (isSorted(a)) status="Ordered";
for (int i = 0; i<a.length; i++)
System.out.print(a[i]+"| ");
System.out.print(status);
System.out.println();
}
public static boolean isSorted(Comparable[] a)
{
for (int i = 0; i<a.length-1; i++)
if (less(a[i+1], a[i]))
return false;
return true;
}
}
元素类型
-
实现了Comparable接口
-
用作排序的元素
intForSort.java
package MYSORT;
public class intForSort implements Comparable<intForSort> {
private int data;
public intForSort(int data)
{this.data = data;}
@Override
public int compareTo(intForSort o) {
if (this.data>o.data) return 1;
if (this.data==o.data) return 0;
return -1;
}
@Override
public String toString() {
return String.valueOf(data);
}
}
堆排序
HeapSort.java
package MYSORT;
public class HeapSort extends Sort
{
private static int HeapSize;
private static int GetHeapSize()
{ return HeapSize; }
private static void SetHeapSize(int o)
{ HeapSize=o; }
public static void sort(Comparable[] a, boolean printProcess)
{
BuildMaxHeap(a);
for (int i=a.length-1;i>=1;i--)
{
exchange(a, i, 0);
SetHeapSize(GetHeapSize()-1);
MaxHeapify(a,0);
if (printProcess) print(a);
}
}
private static void BuildMaxHeap(Comparable[] a)
{
SetHeapSize(a.length);
for (int i=(int) Math.ceil((double)a.length/2)-1;i>=0;i--) MaxHeapify(a,i);
}
private static void MaxHeapify(Comparable[] a, int i)
{
int l ,r ,largest;
l = GetLeftChild(i);
r = GetRightChild(i);
if (l<GetHeapSize() && less(a[i], a[l])) largest=l;
else largest=i;
if (r<GetHeapSize() && less(a[largest], a[r])) largest=r;
if (largest != i) {
exchange(a, i, largest);
MaxHeapify(a, largest);
}
}
private int GetParent(int i)
{ return i/2-1; }
private static int GetLeftChild(int i)
{ return 2*i+1; }
private static int GetRightChild(int i)
{ return 2*i+2; }
}
排序测试
SortTest.java
package MYSORT;
public class SortTest {
public static void main(String[] args) {
int[] data = {5,5,9,6,7,9,1,12,35,62,14,181,17,2,23,88,45,76,98,145,126,26,45,18,13,24};
intForSort []elems = new intForSort[data.length];
for (int i = 0;i<data.length;i++)
{
elems[i] = new intForSort(data[i]);
}
System.out.println("排序前:");
Sort.print(elems);
System.out.println("堆排序过程:");
HeapSort.sort(elems,true);
System.out.println("排序后:");
Sort.print(elems);
}
}
运行结果
排序前:
5| 5| 9| 6| 7| 9| 1| 12| 35| 62| 14| 181| 17| 2| 23| 88| 45| 76| 98| 145| 126| 26| 45| 18| 13| 24| Disordered
堆排序过程:
145| 126| 24| 98| 62| 18| 23| 88| 76| 9| 45| 13| 17| 2| 1| 12| 45| 6| 35| 5| 7| 26| 14| 9| 5| 181| Disordered
126| 98| 24| 88| 62| 18| 23| 45| 76| 9| 45| 13| 17| 2| 1| 12| 5| 6| 35| 5| 7| 26| 14| 9| 145| 181| Disordered
98| 88| 24| 76| 62| 18| 23| 45| 35| 9| 45| 13| 17| 2| 1| 12| 5| 6| 9| 5| 7| 26| 14| 126| 145| 181| Disordered
88| 76| 24| 45| 62| 18| 23| 14| 35| 9| 45| 13| 17| 2| 1| 12| 5| 6| 9| 5| 7| 26| 98| 126| 145| 181| Disordered
76| 62| 24| 45| 45| 18| 23| 14| 35| 9| 26| 13| 17| 2| 1| 12| 5| 6| 9| 5| 7| 88| 98| 126| 145| 181| Disordered
62| 45| 24| 35| 45| 18| 23| 14| 9| 9| 26| 13| 17| 2| 1| 12| 5| 6| 7| 5| 76| 88| 98| 126| 145| 181| Disordered
45| 45| 24| 35| 26| 18| 23| 14| 9| 9| 5| 13| 17| 2| 1| 12| 5| 6| 7| 62| 76| 88| 98| 126| 145| 181| Disordered
45| 35| 24| 14| 26| 18| 23| 12| 9| 9| 5| 13| 17| 2| 1| 7| 5| 6| 45| 62| 76| 88| 98| 126| 145| 181| Disordered
35| 26| 24| 14| 9| 18| 23| 12| 9| 6| 5| 13| 17| 2| 1| 7| 5| 45| 45| 62| 76| 88| 98| 126| 145| 181| Disordered
26| 14| 24| 12| 9| 18| 23| 7| 9| 6| 5| 13| 17| 2| 1| 5| 35| 45| 45| 62| 76| 88| 98| 126| 145| 181| Disordered
24| 14| 23| 12| 9| 18| 5| 7| 9| 6| 5| 13| 17| 2| 1| 26| 35| 45| 45| 62| 76| 88| 98| 126| 145| 181| Disordered
23| 14| 18| 12| 9| 17| 5| 7| 9| 6| 5| 13| 1| 2| 24| 26| 35| 45| 45| 62| 76| 88| 98| 126| 145| 181| Disordered
18| 14| 17| 12| 9| 13| 5| 7| 9| 6| 5| 2| 1| 23| 24| 26| 35| 45| 45| 62| 76| 88| 98| 126| 145| 181| Disordered
17| 14| 13| 12| 9| 2| 5| 7| 9| 6| 5| 1| 18| 23| 24| 26| 35| 45| 45| 62| 76| 88| 98| 126| 145| 181| Disordered
14| 12| 13| 9| 9| 2| 5| 7| 1| 6| 5| 17| 18| 23| 24| 26| 35| 45| 45| 62| 76| 88| 98| 126| 145| 181| Disordered
13| 12| 5| 9| 9| 2| 5| 7| 1| 6| 14| 17| 18| 23| 24| 26| 35| 45| 45| 62| 76| 88| 98| 126| 145| 181| Disordered
12| 9| 5| 7| 9| 2| 5| 6| 1| 13| 14| 17| 18| 23| 24| 26| 35| 45| 45| 62| 76| 88| 98| 126| 145| 181| Disordered
9| 9| 5| 7| 1| 2| 5| 6| 12| 13| 14| 17| 18| 23| 24| 26| 35| 45| 45| 62| 76| 88| 98| 126| 145| 181| Disordered
9| 7| 5| 6| 1| 2| 5| 9| 12| 13| 14| 17| 18| 23| 24| 26| 35| 45| 45| 62| 76| 88| 98| 126| 145| 181| Disordered
7| 6| 5| 5| 1| 2| 9| 9| 12| 13| 14| 17| 18| 23| 24| 26| 35| 45| 45| 62| 76| 88| 98| 126| 145| 181| Disordered
6| 5| 5| 2| 1| 7| 9| 9| 12| 13| 14| 17| 18| 23| 24| 26| 35| 45| 45| 62| 76| 88| 98| 126| 145| 181| Disordered
5| 2| 5| 1| 6| 7| 9| 9| 12| 13| 14| 17| 18| 23| 24| 26| 35| 45| 45| 62| 76| 88| 98| 126| 145| 181| Disordered
5| 2| 1| 5| 6| 7| 9| 9| 12| 13| 14| 17| 18| 23| 24| 26| 35| 45| 45| 62| 76| 88| 98| 126| 145| 181| Disordered
2| 1| 5| 5| 6| 7| 9| 9| 12| 13| 14| 17| 18| 23| 24| 26| 35| 45| 45| 62| 76| 88| 98| 126| 145| 181| Disordered
1| 2| 5| 5| 6| 7| 9| 9| 12| 13| 14| 17| 18| 23| 24| 26| 35| 45| 45| 62| 76| 88| 98| 126| 145| 181| Ordered
排序后:
1| 2| 5| 5| 6| 7| 9| 9| 12| 13| 14| 17| 18| 23| 24| 26| 35| 45| 45| 62| 76| 88| 98| 126| 145| 181| Ordered
参考资料
Robert Sedgewick and Kevin Wayne《Algorithms》FOURTH EDITION
Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, Clifford Stein 《Introduction to Algorithms》SECOND EDITION