算法设计与分析基础-6.4、堆和堆排序

堆排序递归实现,用java,最后两个函数是为了输出好看
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class BuildMaxHeap {
private static int[] sort= new int[] { 3, 4, 43, 5, 1, 6, 23, 55, 12, 6 };

public static void main(String[] args) {
// for (int i = 0; i < 10; i++) {
// Scanner scanner = new Scanner(System.in);
// sort[i] = scanner.nextInt();
// }
print(sort);
buildMaxHeap(sort);
print(sort);
heapSort(sort);
        print(sort);
}

private static int getParent(int i) {
return (i - 1) >> 1;

}

private static int getLeftChild(int i) {
return (i << 1) + 1;

}

private static int getRightChild(int i) {
return (i << 1) + 2;
}

private static void buildMaxHeap(int[] data) {
int length = data.length;
int startIndex = length / 2 - 1;
for (int i = startIndex; i >= 0; i--)
maxHeap(data, length, i);
}


private static void maxHeap(int[] data, int heapSize, int index) {
int left = getLeftChild(index);
int right = getRightChild(index);
int largest = index;
if (left < heapSize && data[left] > data[index])
largest = left;
if (right < heapSize && data[right] > data[largest])   //编写的时候这块除了问题,不要把边界、还有变量变化搞混了!!!
largest = right;
if (largest != index) {
int tmp = data[index];
data[index] = data[largest];
data[largest] = tmp;
maxHeap(data, heapSize, largest);
}
}

private static void heapSort(int[] data)
{
for(int i=data.length-1;i>=0;i--)
{
int tmp=data[0];
data[0]=data[i];
data[i]=tmp;
maxHeap(data, i, 0);
}
}
    private static void print(int[] data){
        int pre = -2;
        for (int i = 0; i < data.length; i++) {
                if (pre < (int)getLog(i+1)) {
                        pre = (int)getLog(i+1);
                        System.out.println();
                } 
                System.out.print(data[i] + " |");
        }
}

/**
 * 以2为底的对数
 * @param param
 * @return
 */
private static double getLog(double param){
        return Math.log(param)/Math.log(2);
}
}

//还是最大堆,加入了插入元素,这时候用数组表示就不合适,改用List,这样,堆的长度可以动态变换
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.Set;


import javax.swing.text.ChangedCharSetException;
import javax.xml.crypto.Data;


public class BuildMaxHeap {
// private static int[] sort= new int[] { 3, 4, 43, 5, 1, 6, 23, 55, 12, 6 };
private static List<Integer> sort=new ArrayList<Integer>();

public static void main(String[] args) {
sort.add(3);
sort.add(4);
sort.add(43);
sort.add(5);
sort.add(1);
sort.add(6);
sort.add(23);
sort.add(55);
sort.add(12);
sort.add(6);

// for (int i = 0; i < 10; i++) {
// Scanner scanner = new Scanner(System.in);
// sort[i] = scanner.nextInt();
// }
print(sort);
buildMaxHeap(sort);
//print(sort);
//heapSort(sort);
   print(sort);
   insert(sort, 19);
   print(sort);
   heapSort(sort);
   print(sort);
}


private static int getParent(int i) {
return (i - 1) >> 1;


}


private static int getLeftChild(int i) {
return (i << 1) + 1;


}


private static int getRightChild(int i) {
return (i << 1) + 2;


}


private static void buildMaxHeap(List<Integer> data) {
int length = data.size();
int startIndex = length / 2 - 1;
for (int i = startIndex; i >= 0; i--)
maxHeap(data, length, i);


}


private static void maxHeap(List<Integer> data, int heapSize, int index) {
int left = getLeftChild(index);
int right = getRightChild(index);
int largest = index;
if (left < heapSize && data.get(left) > data.get(index))
largest = left;
if (right < heapSize && data.get(right) > data.get(largest))
largest = right;
if (largest != index) {
int tmp = data.get(index);
data.set(index, data.get(largest));
data.set(largest, tmp);
maxHeap(data, heapSize, largest);
}
}

private static void heapSort(List<Integer> data)
{
for(int i=data.size()-1;i>=0;i--)
{
int tmp=data.get(0);
data.set(0, data.get(i));
data.set(i, tmp);
maxHeap(data, i, 0);
}
}

private static void insert(List<Integer> data,int v)
{
int last=data.size();
data.add(v);
int i;
int tmp;
int parent;
for(i=last;i>=0;i=parent)
{
parent = (i-1)/2;
if(parent>=0&&data.get(i)>data.get(parent))    //注意parent的值  (-1)>>1=-1   -1/2=0
{
tmp=data.get(i);
data.set(i, data.get(parent));
data.set(parent, tmp);
}
else {
break;
}
}
}

    private static void print(List<Integer> data){
        int pre = -2;
        for (int i = 0; i < data.size(); i++) {
                if (pre < (int)getLog(i+1)) {
                        pre = (int)getLog(i+1);
                        System.out.println();
                } 
                System.out.print(data.get(i) + " |");
        }
}


/**
 * 以2为底的对数
 * @param param
 * @return
 */
private static double getLog(double param){
        return Math.log(param)/Math.log(2);
}
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值