java中堆的数量_堆排序(递归),当整数数目超过10000个时会报堆栈溢出错误

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:");

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值