堆排序
来源:互联网 发布:上海网络推广哪家好 编辑:程序博客网 时间:2024/05/17 09:38
public class dui {
public class node{
private int pt;
private int value;
private int right;
private int left;
public node(){
this.right =-1;
this.left =-1;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public int getRight() {
return right;
}
public void setRight(int right) {
this.right = right;
}
public int getLeft() {
return left;
}
public void setLeft(int left) {
this.left = left;
}
public int getPt() {
return pt;
}
public void setPt(int pt) {
this.pt = pt;
}
}
public node[] duix;
//把一个数组演变成堆的完整过程
public void setDuixFromArr(int[] arr){
node[] d = change1(arr);
jiandui(d);
this.duix = d;
}
public node[] getDui(){
return this.duix;
}
public int [] sort(){
if(this.duix.length==0) return null;
int[] c = new int[this.duix.length];
for(int i=this.duix.length;i>2;i--){
c[duix.length-i] = this.duix[0].getValue();
this.duix[0].setValue(this.duix[i-1].getValue());
//this.duix.
if(duix[duix[i-1].pt].getRight() !=-1){
duix[duix[i-1].pt].setRight(-1);
}else{
if(duix[duix[i-1].pt].getLeft() !=-1){
duix[duix[i-1].pt].setLeft(-1);
}
}
Maxguize(duix, 0);
}
if(this.duix.length==1){
c[this.duix.length-1] = this.duix[0].getValue();
}
if(this.duix.length>1){
c[c.length-2] = this.duix[0].getValue();
c[c.length-1] = this.duix[1].getValue();
}
return c;
}
//先把数组演变成一个“数组和树”形结构
private node[] change1(int[] arr){
node[] duix= new node[arr.length];
int x = 1;
while(true){
if(x>arr.length){
break;
}
node nodex = new node();
nodex.setValue(arr[x-1]);
duix[x-1] = nodex;
x++;
}
x = 1;
while(true){
if(duix.length==0) break;
if(x>arr.length){
break;
}
if(2*x<=arr.length){
duix[x-1].setLeft(2*x-1);
duix[2*x-1].setPt(x-1);
}
if(2*x+1 <=arr.length){
duix[x-1].setRight(2*x);
duix[2*x].setPt(x-1);
}
if(x==1){
duix[x-1].setPt(-1);
}
x++;
}
return duix;
}
//建堆
private void jiandui(node[] duix){
if(duix.length==0) return;
for(int i= duix.length/2;i>0;i--){
Maxguize(duix, i-1);
}
}
//维护堆的规则
private void Maxguize(node[] duix,int i){
int lg = i;
if(duix[i].getLeft()!=-1){
node left = duix[duix[i].getLeft()];
if(left.getValue() > duix[i].getValue()){
lg = duix[i].getLeft();
}
}
if(duix[i].getRight()!=-1){
node right = duix[duix[i].getRight()];
if(right.getValue() > duix[lg].getValue()){
lg = duix[i].getRight();
}
}
if(lg!=i){
int zhong;
zhong = duix[i].getValue();
duix[i].setValue(duix[lg].getValue());
duix[lg].setValue(zhong);
Maxguize(duix,lg);
}
}
public static void main(String[] args) {
dui d = new dui();
int[] arr = {1,2,3,4,5,6,2,1,100};
d.setDuixFromArr(arr);
for(node n:d.getDui()){
System.out.print(n.getValue()+",");
}
System.out.println("//////////////////////////////////////");
int[] c = d.sort();
for(int c1:c){
System.out.print(c1+",");
}
System.out.println("//////////////////////////////////////");
for (node node1:d.duix){
System.out.print(node1.getValue()+",");
}
}
}
- 堆及堆排序
- 堆/堆排序特点
- 【二叉堆、堆排序】
- 二叉堆 & 堆排序
- 二叉堆 & 堆排序
- 堆与堆排序
- 堆与堆排序
- 堆与堆排序
- 堆与堆排序
- 堆与堆排序
- 堆与堆排序
- 堆和堆排序
- 堆排序(最大堆)
- 堆和堆排序
- 堆和堆排序
- 堆及堆排序
- 堆和堆排序
- 堆与堆排序
- The Monkey's Paw——Chapter 3
- KML+教程
- LAMP一键安装
- Hibernate模糊查询
- 用AMI实现通话录音功能
- 堆排序
- C语言char类型和int类型相互转换问题
- 滚动条 一
- 装centos和 win7双系统的总结
- cvInitMatHeader的一些问题
- CP异常终止(reset报文)
- LNMP一键安装
- oracle创建简单的包
- file的getPath getAbsolutePath和getCanonicalPath的不同