摘要:
堆排序(Heapsort)是指利用堆积树(堆)这种数据结构所设计的一种排序算法,它是选择排序的一种。可以利用数组的特点快速定位指定索引的元素。
目录:
- 基本思想
- Java实现
- Python实现
- 参考代码
正文:
一. 基本思想
- 将初始待排序关键字序列(R1,R2….Rn)构建成大顶堆,此堆为初始的无序区;
- 将堆顶元素R[1]与最后一个元素R[n]交换,此时得到新的无序区(R1,R2,……Rn-1)和新的有序区(Rn),且满足R[1,2…n-1]<=R[n];
- 由于交换后新的堆顶R[1]可能违反堆的性质,因此需要对当前无序区(R1,R2,……Rn-1)调整为新堆,然后再次将R[1]与无序区最后一个元素交换,得到新的无序区(R1,R2….Rn-2)和新的有序区(Rn-1,Rn)。不断重复此过程直到有序区的元素个数为n-1,则整个排序过程完成。
二. Java实现
package com.timen.daily_20161220;
public class HeapSort {
/**
* 数据交换
*/
public static void swap(int[] data, int i, int j) {
int temp = data[i];
data[i] = data[j];
data[j] = temp;
}
/**
* 输出数组
*/
public static void output(int[] data) {
for (int i = 0; i < data.length; i++) {
System.out.print(data[i] + " ");
}
System.out.println();
}
/**
* 堆排序
* 步骤:
* 1. 构建堆(大顶堆 or 小顶堆)
* 2. 堆顶元素R[1] 与 R[n]交换,之后重新调整堆
* 3. 循环R[1]到R[n-1]直到长度为1
* 备注:以升序为例 构建大顶堆
*/
public static void heap_sort(int[] data) {
System.out.println("排序过程");
for (int i = 0; i < data.length; i++) {
create_heap(data, data.length -1 - i);
swap(data, 0, data.length -1 - i);
output(data);
}
System.out.println("最终排序结果");
output(data);
}
/**
* 构建大顶堆
*/
public static void create_heap(int[] data, int last_index) {
int lenght = last_index / 2 -1;
for (int i = lenght; i >= 0; i--) {
if (data[2*i + 1] >= data[i] & last_index >= (2 * i + 1)) {
swap(data, 2*i + 1, i);
}
if (data[2*i + 2] >= data[i] & last_index >= (2 * i + 2)) {
swap(data, 2*i + 2, i);
}
}
}
/**
* 主方法
*/
public static void main(String[] args) {
int[] test_data = {1, 3, 6, 9, 12, 11, 15, 4, 2, 3, 8};
System.out.println("原数据");
output(test_data);
heap_sort(test_data);
}
}
运行结果:
原数据
1 3 6 9 12 11 15 4 2 3 8
排序过程
8 1 12 3 9 6 11 4 2 3 15
3 8 9 1 4 6 11 3 2 12 15
2 3 8 3 4 6 9 1 11 12 15
1 2 4 3 3 6 8 9 11 12 15
6 1 3 2 3 4 8 9 11 12 15
4 3 3 1 2 6 8 9 11 12 15
2 3 3 1 4 6 8 9 11 12 15
1 2 3 3 4 6 8 9 11 12 15
2 1 3 3 4 6 8 9 11 12 15
1 2 3 3 4 6 8 9 11 12 15
1 2 3 3 4 6 8 9 11 12 15
最终排序结果
1 2 3 3 4 6 8 9 11 12 15
三. Python实现
# -*- coding:utf-8 -*-
# 堆排序
def heap_sort(test_list):
for i in range(len(test_list)):
create_heap(test_list, len(test_list) -1 -i)
swap(test_list, 0, len(test_list) -1 -i)
return test_list
# 构建大顶堆
def create_heap(test_list, last_index):
lenght = int(len(test_list) / 2 - 1)
for i in range(lenght, -1, -1):
if test_list[ 2 * int(i) +1] >= test_list[int(i)] and last_index >= (2 * int(i) +1):
swap(test_list, (2 * int(i) +1), int(i))
if test_list[2 * int(i) + 2] >= test_list[int(i)] and last_index >= (2 * int(i) + 2):
swap(test_list, (2 * int(i) + 2), int(i))
# 数据交换
def swap(test_list, i, j):
temp = test_list[i]
test_list[i] = test_list[j]
test_list[j] = temp
if __name__ == '__main__':
lis = [1, 3, 6, 9, 12, 11, 15, 4, 2, 3, 8]
print u"原数据"
print lis
print u"最终排序结果"
print heap_sort(lis)
运行结果:
D:\Python\python.exe D:/PythonProject/Timen_daily/daily_20161220_heapsort.py
原数据
[1, 3, 6, 9, 12, 11, 15, 4, 2, 3, 8]
最终排序结果
[1, 2, 3, 3, 4, 6, 8, 9, 11, 12, 15]
Process finished with exit code 0
四. 参考代码
# -*- coding:utf-8 -*-
class HeapSort:
def __init__(self, lis=None):
self.r = lis
# 数据交换
def swap(self, i, j):
temp = self.r[i]
self.r[i] = self.r[j]
self.r[j] = temp
# 对排序
def heap_sort(self):
length = len(self.r)
i = int(length/2)
while i >= 0:
self.heap_adjust(i, length-1)
i -= 1
j = length-1
while j > 0:
self.swap(0, j)
self.heap_adjust(0, j-1)
j -= 1
# 调整堆
def heap_adjust(self, s, m):
lis = self.r
temp = lis[s]
i = 2*s
while i <= m:
if i < m and lis[i] < lis[i+1]:
i += 1
if temp >= lis[i]:
break
lis[s] = lis[i]
s = i
i *= 2
lis[s] = temp
# 输出列表
def __str__(self):
ret = ""
for i in self.r:
ret += " %s" % i
return ret
if __name__ == '__main__':
Heap_Sort = HeapSort([1, 3, 6, 9, 12, 11, 15, 4, 2, 3, 8])
Heap_Sort.heap_sort()
print(Heap_Sort)
总结:
堆排序 平均性能 O(N*logN) 由于建初始堆所需的比较次数较多,所以堆排序不适宜于记录数较少的文件。
1万+

被折叠的 条评论
为什么被折叠?



