package com.package2;
/**
* Function: 测试堆排序
*/
import java.util.*;
public class SortMethod {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//QuickSort st=new QuickSort(); //递归时: 3千万,6s; 1亿,20s; 非递归时: 3千万,11s; 1亿,36s
//MergeSort st=new MergeSort(); //非递归时: 3千万,5s; 1亿,15s; 递归时: 3千万,7s; 1亿,26s;
HeapSort st = new HeapSort();
TestSort ts=new TestSort();
//int size=11; boolean debug=false;
int size=8970; boolean debug=true; //debug=true时仅打印排序开始与结束时间,不打印排序过程
int arr[]=new int[size];
for(int i=0;i
ts.process(debug, st, arr);
}
}
class TestSort{
public void process(boolean debug, Sort sort, int[] arr){
sort.setArr(arr);
sort.print(debug);
sort.sort(debug);
sort.print(debug);
}
}
interface Sort{
public void setArr(int[] arr);
public void print(boolean debug);
public void printSystime(boolean debug, String pre);
public void sort(boolean debug);
}
class HeapSort implements Sort{
int arr[];
public void setArr(int[] arr) {
this.arr = arr;
}
public void print(boolean debug){
if(!debug){
String text="";
for(int i=0;i
text+=","+arr;
}
text=text.replaceFirst(",","");
System.out.println("Array is:"+text);
//System.out.print(text+"\n");
}
}
public void printSystime(boolean debug, String pre){
if(debug){
Calendar cal=Calendar.getInstance();
System.out.println(pre+cal.getTime());
}
}
public void heapSort(boolean debug, int[] arr, int high){
if(high>0){
initSort(debug,arr,high);//初始化堆,找出最大的放在堆顶
arr[0]=arr[high]+arr[0];
arr[high]=arr[0]-arr[high];
arr[0]=arr[0]-arr[high];
heapSort(debug, arr, high-1);
}
}
public void initSort(boolean debug, int[] arr,int high){
int max=(high+1)/2; //完全二叉树中非叶子节点的最大下标
/**
* 未优化(自上向下):从树的根部开始排序,会造成过多的交换
*/
/*for(int root=0;root
boolean flag=buildHeap(arr,high,root);
//如果孩子之间有交换,就要重新开始
if(flag) root=-1;
print(debug);
}*/
/**
* 优化(自下向上):从树的n-1层开始排序,
*/
for(int root=max-1;root>=0;root--){
buildHeap(arr,high,root); //boolean flag=buildHeap(arr,high,root);
//优化后,子节点之间有序,不需要重复排序
//未优化前,如果子节点之间有交换,就要重新开始排序;
//if(flag) root=max;
print(debug);
}
}
//返回一个标记,如果有根与子节点交换就要重新从顶根开始查找不满足最大堆树结构
public boolean buildHeap(int arr[],int high,int root){
int l_child=2*root+1;//左节点
int r_child=2*root+2;//右节点
int swap;
//判断是否有右子节点
if(r_child>high){ //没有的话直接比较,小于交换
if(arr[root]
swap = arr[root];
arr[root]=arr[l_child];
arr[l_child]=swap;
return true;
}else{
return false;
}
}
//在根与两个子节点之间进行排序交换
if((arr[root]
swap = arr[root]; //将root值放入交换区
//在根与两个子节点之间找出最大的那个值进行交换
if(arr[l_child]>=arr[r_child]){
//交换根与左子节点的值
arr[root]=arr[l_child];
arr[l_child]=swap;
//左节点>右节点,左右交换,使排序后 左节点<=右节点
if(arr[l_child]>arr[r_child]){
arr[l_child]=arr[r_child];
arr[r_child]=swap;
}
return true;
}else{
//交换根与右子节点的值
arr[root]=arr[r_child];
arr[r_child]=swap;
//左节点>右节点,左右交换,使排序后 左节点<=右节点
if(arr[l_child]>arr[r_child]){
arr[r_child]=arr[l_child];
arr[l_child]=swap;
}
return true;
}
}else{ //两个子节点之间进行排序交换
if(arr[l_child]>arr[r_child]){
swap = arr[l_child];
arr[l_child]=arr[r_child];
arr[r_child]=swap;
}
}
return false;
}
@Override
public void sort(boolean debug) {
// TODO Auto-generated method stub
printSystime(debug,"HeapSort Start at:");
heapSort(debug, arr,arr.length-1);
printSystime(debug,"HeapSort End at:");
}
}