堆排序

堆的概念:

  1. 它是一棵完全二叉树
  2. 每个结点大于或等于它的任意一个孩子。(最大堆)

堆排序:

堆可以存储在一个ArrayList或数组中,树根在位置0处,它的两个孩子在位置1和2处。对于位置 i 处的结点它的左孩子在位置2i + 1 处,它的右孩子在2i+ 2 处,而它的父亲在位置(i-1)/2 处。

  • 添加一个新结点

Let the last node be the current node;

while(the current node is greater than its parent)

{

Swap the current node with its parent;

Now the current node is one level up;

}

  • 删除根结点

Move the last node to repalce the root;

Let the root be the current node;

 1 public class HeapSort {
 2     public static <E extends Comparable>void HeapSort(E[] list)
 3     {
 4         Heap<E> heap  = new Heap<E>();
 5         for(int i = 0; i< list.length;i++)
 6             heap.add(list[i]);
 7         for(int i = list.length - 1; i >= 0;i--)
 8             list[i] = heap.remove();
 9     }
10     public static void main(String[] args) {
11         // TODO Auto-generated method stub
12         Integer[] list = {2, 3, 2, 5, 6};
13         HeapSort(list);
14         for(int i = 0; i< list.length; i++)
15             System.out.print(list[i]+"");
16     }
17 
18 }
19 class Heap<E extends Comparable>
20 {
21     private java.util.ArrayList<E> list = new java.util.ArrayList<E>();
22     public Heap()
23     {
24         
25     }
26     public Heap(E[] objects)
27     {
28         for(int i = 0; i < objects.length; i++)
29             add(objects[i]);
30     }
31     public void add(E newobject)//向堆中加入元素
32     {
33         list.add(newobject);//将 newobject 加入ArrayList中
34         int currentIndex = list.size() - 1;
35         while(currentIndex > 0)
36         {
37             int parentIndex = (currentIndex - 1)/2;
38             if(list.get(currentIndex).compareTo(list.get(parentIndex)) > 0)
39             {
40                 E temp = list.get(currentIndex);
41                 list.set(currentIndex, list.get(parentIndex));
42                 list.set(parentIndex, temp);
43             }
44             else 
45                 break;
46             currentIndex = parentIndex;
47         }
48     }
49     public E remove()//删除堆的root
50     {
51         if(list.size() == 0) return null;
52         E removeObject = list.get(0);
53         list.set(0, list.get(list.size() - 1));//将最后一个节点置为root
54         list.remove(list.size() - 1);//将ArrayList的最后一个元素删除
55         
56         int currentIndex = 0;
57         while(currentIndex < list.size())
58         {
59             int leftChildIndex = 2 * currentIndex + 1;
60             int rightChildIndex =  2 * currentIndex + 2;
61             if(leftChildIndex >= list.size())
62                 break;
63             int maxIndex = leftChildIndex;
64             if(rightChildIndex < list.size())
65             {
66                 if(list.get(maxIndex).compareTo(list.get(rightChildIndex)) < 0)
67                     maxIndex = rightChildIndex;
68             }
69             if(list.get(currentIndex).compareTo(list.get(maxIndex)) < 0)
70             {
71                 E temp = list.get(maxIndex);//节点的交换
72                 list.set(maxIndex,list.get(currentIndex));
73                 list.set(currentIndex, temp);
74                 currentIndex = maxIndex;
75             }
76             else
77                 break;
78         }
79         return removeObject;
80     }
81     public int getSize()
82     {
83         return list.size();
84     }
85 }
View Code

 

posted @ 2013-10-17 16:29  soul390  阅读(225)  评论(0编辑  收藏  举报