整理的排序算法总结
来源:互联网 发布:闭口粉刺怎么去除知乎 编辑:程序博客网 时间:2024/05/18 13:28
private
int
[] MergeSort(
int
[] i_list) {
if
(i_list.length ==
1
) {
return
i_list;
}
else
{
int
[] listL =
new
int
[i_list.length /
2
];
int
[] listR =
new
int
[i_list.length - i_list.length /
2
];
int
Center = i_list.length /
2
;
for
(
int
i =
0
; i < Center; i++) {
listL[i] = i_list[i];
}
for
(
int
i = Center, j =
0
; i < i_list.length; i++, j++) {
listR[j] = i_list[i];
}
int
[] SortedListL=MergeSort(listL);
int
[] SortedListR=MergeSort(listR);
int
[] o_list = MergeTwoList(SortedListL, SortedListR);
return
o_list;
}
}
private
int
[] MergeTwoList(
int
[] listL,
int
[] listR) {
int
i =
0
, j =
0
;
int
[] o_list =
new
int
[listL.length + listR.length];
int
foot =
0
;
while
(i < listL.length && j < listR.length) {
if
(listL[i] <= listR[j]) {
o_list[foot] = listL[i];
i++;
}
else
{
o_list[foot] = listR[j];
j++;
}
foot++;
}
if
(i == listL.length) {
while
(j < listR.length) {
o_list[foot++] = listR[j++];
}
}
else
{
// j==listR.length
while
(i < listL.length) {
o_list[foot++] = listL[i++];
}
}
return
o_list;
}
}
当面试的时候被问到,你对堆有没有做过一些了解??
当时我我反问了一下:您是指堆数据结构还是JVM里面存储对象等信息的堆时??
当时感觉自己还挺高端的,居然可以反问,但是当面试官说是堆排序的时候我就傻眼了,
不会,只记得个大概,于是今天花了点时候把堆排序搞懂了!!
详解如下:
对于一个数组:
int a[] = { 0, 4, 1, 3, 2, 16, 9, 10, 14, 8, 7 };
他可以用来表示堆,堆是一颗完全二叉树,分大值堆和小值堆,最后得出的分别是从大到小的排列顺序和从小到达的排列顺序,看你要排序的顺序觉定大小堆!
0(1)
4 (2) 1(3)
3(4) 2(5) 16(6) 9(7)
10(8) 14(9) 8(10) 7(11)
存在一个如上图的关系,
left[i] = 2* i ;
right[i] = 2+i +1;
知道这个关系后,我们就可以开始建堆了:
从 a.length/2 到第一个元素 开始对每个元素进行建堆(递归),使每个元素下的树都维持最小值堆的特性;
a.length/2+1 到 a.length 个元素都是叶子节点,所以不需要维持最小值堆操作,因为已经是最小值堆了!!
代码如下:
public static void max_heapify(int[] a, int i) {
int left = leftChild(i);
int right = rightChild(i);
int largest = 0;
if (left < heap_size && a[i] < a[left]) {
largest = left;
} else {
largest = i;
}
if (right < heap_size && a[right] > a[largest]) {
largest = right;
}
if (largest == i) {
return;
} else {
int temp = a[i];
a[i] = a[largest];
a[largest] = temp;
max_heapify(a, largest);
}
}
上一步骤理解了,那么之后的操作就很简单了。
建好最小值堆后,每次取堆顶元素和数组最后一个元素(最后第n个元素,n为交换次数)交换,剩下的元素采用维持堆性质的操作,
循环至最后堆只剩下一个元素即可,代码如下:
public static void heapSort(int[] a) {
build_max_heap(a);
for (int i = a.length - 1; i >= 2; i--) {
int temp = a[1];
a[1] = a[i];
a[i] = temp;
heap_size--;
max_heapify(a, 1);
}
}
时间复杂度为O(N*logN)。
至此,堆排序讲解完毕!希望大家可以明白堆排序的原理了!
附上完整可运行代码:
public class HeapSort {
public static int heap_size;
// 双亲编号
public static int parent(int i) {
return i / 2;
}
// 左孩子编号
public static int leftChild(int i) {
return 2 * i;
}
// 右孩子编号
public static int rightChild(int i) {
return 2 * i + 1;
}
/**
* 堆排序:首先使用建立最大堆的算法建立好最大堆,然后将堆顶元素(最大值)与最后一个值交换,同时使得堆的长度减小1
* ,调用保持最大堆性质的算法调整,使得堆顶元素成为最大值,此时最后一个元素已被排除在外、
*/
public static void heapSort(int[] a) {
build_max_heap(a);
for (int i = a.length - 1; i >= 2; i--) {
int temp = a[1];
a[1] = a[i];
a[i] = temp;
heap_size--;
max_heapify(a, 1);
}
}
/**
* 建立最大堆。在数据中,a.length/2+1一直到最后的元素都是叶子元素,也就是平凡最大堆,因此从其前一个元素开始,一直到
* 第一个元素,重复调用max_heapify函数,使其保持最大堆的性质
*
* @param a
*/
public static void build_max_heap(int[] a) {
for (int i = a.length / 2; i >= 1; i--) {
max_heapify(a, i);
}
}
/**
* 保持最大堆的性质
*
* @param a
* ,堆中的数组元素
* @param i
* ,对以该元素为根元素的堆进行调整,假设前提:左右子树都是最大堆
*
* 由于左右孩子都是最大堆,首先比较根元素与左右孩子,找出最大值,假如不是根元素,则调整两个元素的值;
* 由于左孩子(右孩子)的值与根元素交换,有可能打破左子树(右子树)的最大堆性质,因此继续调用,直至叶子元素。
*/
public static void max_heapify(int[] a, int i) {
int left = leftChild(i);
int right = rightChild(i);
int largest = 0;
if (left < heap_size && a[i] < a[left]) {
largest = left;
} else {
largest = i;
}
if (right < heap_size && a[right] > a[largest]) {
largest = right;
}
if (largest == i) {
return;
} else {
int temp = a[i];
a[i] = a[largest];
a[largest] = temp;
max_heapify(a, largest);
}
}
public static void main(String[] args) {
int a[] = { 0, 4, 1, 3, 2, 16, 9, 10, 14, 8, 7 };
heap_size = a.length;
heapSort(a);
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
}
}
第一步
第二步
第三步
2效率分析
- 整理的排序算法总结
- 整理的8种排序算法的总结和比较
- 排序算法的比较整理
- 排序算法的代码整理
- 常用的排序算法整理
- 整理的常见排序算法
- 排序算法的总结
- 排序算法的总结
- 排序算法的总结
- 排序算法的总结
- 排序算法的总结
- 排序算法的总结
- 排序算法的总结
- 各种常用的排序算法实现对数组的排序——整理总结(代码实现)
- 自己整理的快速排序算法
- 排序算法的整理(1)
- java的各种排序算法代码整理
- 8种排序算法的整理
- 跨越千年的RSA算法
- k-th Smallest Element of Two Sorted Arrays
- jquery 获取及设置input各种类型的值
- warning
- Leetcode: Pascal's Triangle
- 整理的排序算法总结
- iOS中各种控件的使用
- 全表扫描出现db file sequential read
- myeclipse设置字体快捷方式
- 铁道部新客票系统的设计(一)
- Java JNI学习(二)
- hdu 3436 Queue-jumpers(Splay)
- 虚析构函数
- C++服务器与android客户端通信程序————C++服务器程序