七大基本排序算法之堆排序

import java.io.IOException;
import Input.InputString;

/**
 * 堆排序
 * @author xiaomi
 * 2012.4.1
 */
public class HeapSort {	
	private static int[] heap;
	public static void main(String[] args) throws IOException{
		String s = InputString.getString();
		String[] str = s.split(" ");
		heap = new int[str.length+1];//从下标1开始算起
		for(int i = 0;i < str.length;i++){
			heap[i+1] = Integer.parseInt(str[i]);
		}
		heapSort();
		for(int i = 1;i < heap.length;i++){
			System.out.print(heap[i]+" ");
		}
	}
	
	public static void heapSort(){
		createHeap();
		for(int i = 1;i < heap.length;i++){
			ajustHeap(i);
		}
	}
	
	//每加入一个节点就要向上筛选一次
	public static void createHeap(){
		for(int i = 2; i < heap.length;i++){
			for(int j = i; j/2>=1;j/=2){
				if(heap[j]>heap[j/2]){
					int temp = heap[j];
					heap[j] = heap[j/2];
					heap[j/2] = temp;
				}
			}
		}
	}
	
	//每移走一个节点就要向下调整一次
	public static void ajustHeap(int k){
		int temp = heap[1];
		heap[1] = heap[heap.length-k];
		heap[heap.length-k] = temp;
		int i = 1;
		while((2*i) < heap.length-k){
			if((2*i+1) < heap.length-k){
				if(heap[i] > heap[2*i] && heap[i] > heap[2*i+1]){
					break;
				}
				else{//与较大的一个子节点交换
					if(heap[2*i] < heap[2*i+1]){
						int a = heap[i];
						heap[i] = heap[2*i+1];
						heap[2*i+1] = a;
						i = 2*i+1;
					}else{
						int a = heap[i];
						heap[i] = heap[2*i];
						heap[2*i] = a;
						i = 2*i;
					}
				}
			}else{//不存在右节点的时候
				if(heap[i] > heap[2*i]){
					break;
				}
				else{
					int a = heap[i];
					heap[i] = heap[2*i];
					heap[2*i] = a;
					i = 2*i;
				}
			}	
		}
	}
}

 

package Sort;

import java.io.IOException;

import Input.InputString;

public class HeapSort {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try {
			String str = InputString.getString();
			String[] s = str.split(" ");
			int[] a = new int[s.length+1];
			for(int i = 1; i<=s.length;i++){
				a[i] = Integer.parseInt(s[i-1]);
			}
			heapSort(a);
			for(int i = 1; i<a.length;i++){
				System.out.print(a[i]+" ");
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void heapAdjust(int[] a, int begin,int end){
		int temp = a[begin];
		for(int i = 2*begin; i <= end; i = 2*i){
			if(i < end && a[i] < a[i+1]){
				i++;
			}
			if(temp >= a[i]){
				break;
			}
			a[begin] = a[i];
			begin = i;
		}
		a[begin] = temp;
	}
	
	public static void heapSort(int[] a){
		for(int i = a.length/2; i > 0; i--){
			heapAdjust(a, i, a.length-1);
		}
		for(int i = a.length-1; i > 1; i--){
			int temp = a[1];
			a[1] = a[i];
			a[i] = temp;
			heapAdjust(a, 1, i-1);
		}	
	}
}

 

七大基本排序算法之冒泡排序

七大基本排序算法之选择排序

七大基本排序算法之插入排序

七大基本排序算法之希尔排序

七大基本排序算法之堆排序

七大基本排序算法之快速排序

七大基本排序算法之归并排序

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值