Java堆排序递归_大顶堆第二弹----堆排序(递归实现)
1 packagecom.datastruct;2
3 importjava.util.ArrayList;4 importjava.util.Arrays;5
6 public classBigHeap {7
8
9
10 /*
11 *交換堆中的兩個元素12 */
13 private void swap(ArrayList heapList,int srcIndex,intdstIndex)14 {15 int tmp =heapList.get(srcIndex);16
17 heapList.set(srcIndex,heapList.get(dstIndex));18 heapList.set(dstIndex, tmp);19
20 }21
22 /*
23 *將指定元素的位置進行上移操作24 */
25 private void HeapUp(ArrayList heapList,intindex)26 {27
28 if(index > 1)29 {30 int parent = index / 2;31 int parentVal =heapList.get(parent).intValue();32 int indexVal =heapList.get(index).intValue();33
34 if(indexVal >parentVal)35 {36 swap(heapList,parent,index);37 HeapUp(heapList,parent);38 }39
40 }41 }42
43 /*
44 *將指定元素的位置進行下移操作45 */
46 private void HeapDown(ArrayList heapList,intindex)47 {48 int heapSize = heapList.size(); //這里進行了重復的計算,可以將作為形參傳入,或者將該函數,寫成非遞歸形式
49
50 if(index > heapSize - 1)51 {//節點不存在
52 return;53 }54
55 int child = index * 2; //左孩子節點
56
57 if(child > (heapSize - 2))58 {//當前節點為葉子節點,不能進行下移操作,直接返回59 //-2是由于最后一個元素已經是要刪除的節點,不在計算范圍之內
60 return;61 }62 else if(child < heapSize - 2)63 {//有兩個孩子節點
64 if(heapList.get(child).intValue() < heapList.get(child + 1).intValue())65 {66 child++; //右孩子結點值大,作為新的父節點
67 }68 }69
70 if(heapList.get(child).intValue() >heapList.get(index).intValue())71 {//孩子節點的值大,進行下移
72 swap(heapList,child, index);73 HeapDown(heapList,child);//繼續進行下移操作
74 }75
76 }77
78 /*
79 *向大頂堆中插入一個元素80 */
81 public void HeapInsert(ArrayList heapList,intvalue)82 {83 int heapSize =heapList.size();84
85 if(heapSize == 0)86 {//第一個元素不為堆中的元素,跳過
87 heapList.add(-100);88 }89
90 heapList.add(value);91 heapSize++; //添加新元素后,改變堆的大小
92
93 HeapUp(heapList,heapSize - 1);94 }95
96 /*
97 *從大頂堆中刪除一個元素98 */
99 public void HeapDelete(ArrayList heapList,intvalue)100 {101 int index = 1,heapSize =heapList.size();102 for(; index < heapSize; index++)103 {104 if(heapList.get(index).intValue() ==value)105 {106 break;107 }108 }109
110 if (index >=heapSize)111 {//元素不存在
112 return;113 }114
115 heapList.set(index, heapList.get(heapSize-1)); //將最后一個葉子節點值賦值到當前節點
116 HeapDown(heapList,index);117
118 int parent = index / 2;119
120 if(parent > 0 && ( heapList.get(index).intValue() >(Integer)heapList.get(parent).intValue() ))121 {//如果下移操作后該元素大于父節點還要進行上移
122 HeapUp(heapList,index);123 }124
125 heapList.remove(heapSize - 1);126 }127
128 /*
129 *調整堆結構130 */
131 public void heapAdjust(ArrayListheapList,int index,intend)132 {133 int child = -1,heapSize =heapList.size();134
135 end = end > heapSize - 1 ? heapSize - 1 : end; //確保結束正確
136
137 while(index <= end / 2)138 {//只需調整非葉子節點
139 child = index * 2; //左孩子節點
140 if(child + 1 <= end && (heapList.get(child+1).intValue() >heapList.get(child).intValue()) )141 {142 child += 1; //右孩子節點值大右孩子節點上移
143 }144
145 if(heapList.get(child).intValue() >heapList.get(index).intValue())146 {147 swap(heapList, child, index);148 index =child;149 }150 else
151 {152 break;153 }154 }155 }156
157
158 /*
159 * 初始化時調整堆結構160 * 由于堆是完全二叉樹結構,所以只有前一半節點是非葉子節點161 */
162 public void heapInitAdjust(ArrayListheapList)163 {164 int heapSize =heapList.size();165
166 for(int i = heapSize / 2; i > 0; i--)167 {168 heapAdjust(heapList, i, heapSize - 1);169 }170
171 }172
173 /*
174 * 堆排序175 * 將大頂堆堆頂元素和最后一個元素交換,調整剩下元素的對結構176 * 直到調整到最后一個元素就排好序177 */
178 public void heapSort(ArrayListheapList)179 {180 int heapSize =heapList.size();181
182 for(int i = heapSize - 1; i > 0; i--)183 {184 swap(heapList, 1, i); //交換堆頂和最后一個元素
185 heapAdjust(heapList, 1, i - 1);186 }187 }188
189 /*
190 * 打印堆元素191 */
192 public void PrintHeap(ArrayListheapList)193 {194 for(int i = 1; i < heapList.size(); i++)195 {196 System.out.print(heapList.get(i) + " ");197 }198
199 System.out.println();200 }201
202
203
204 public static voidmain(String args[])205 {206 ArrayList heapList = new ArrayList<>(Arrays.asList(null,1,3,4,5,8,2,7));207
208 BigHeap bigHeap = newBigHeap();209
210 bigHeap.heapInitAdjust(heapList);211 bigHeap.PrintHeap(heapList);212
213 bigHeap.HeapInsert(heapList, 6);214 bigHeap.PrintHeap(heapList);215
216 bigHeap.heapSort(heapList);217 bigHeap.PrintHeap(heapList);218 }219 }
總結
以上是生活随笔為你收集整理的Java堆排序递归_大顶堆第二弹----堆排序(递归实现)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 网上书城java负责_网上书城项目总结(
- 下一篇: java3d 上色_Unity 着色过程