【排序】Heap Sort
2012-11-30 10:06
274 查看
What's heap ? it's main feature: root is the smallest(largest), nodes with children are larger than children. Root which is the smallest ,called Small Heap. Otherwise, we called it Big Heap if it's largest.
we now represent the heap in data stuct of Heap in the way of array.
In order to finish heap sort, we need to do 2 things:
build a heap
remove the root ,which is largest/smallest element , and rebuild the heap
Here is the code:
#include <stdio.h>
#include "type.h"
void swap(ElemType *a, ElemType *b)
{
ElemType tmp = *a;
*a=*b;
*b=tmp;
}
void HeapAdjust(SqList *L, int start, int end)
{
printf("Adjust %d %d \n", start, end);
int j, temp, i;
temp = L->data[start];
i = start;
//Note:2*start , if start==0, then j==0 . That's why we NEED THE LIST TO START FROM 1, INSTEAD OF 0
for(j = 2*start; j<=end; j*=2)
{
//compare left child [2*start] and right child [2*start+1]
if(L->data[j]<L->data[j+1] && j<end)
j++;//right child is larger than left child
if(temp>=L->data[j])//temp(position [start]) is already the largest
break;
L->data[start] = L->data[j];// Note:change
start = j;//above line changed the heap, we now need to reconstruct
}
printf("largest now[%d]:%d <-> [%d]:%d \n",start, L->data[start], temp, i);
L->data[start] = temp;
}
void HeapSort(SqList *L)
{
//construct the list into a heap, starting from nodes with children
for(int i = (L->length)/2; i>0; i--)
HeapAdjust(L, i, L->length);
//swap out the largest , and then reconstruct the list into a new heap
for(int i=L->length; i>1; i--)
{
swap(&(L->data[1]), &(L->data[i]));
HeapAdjust(L, 1, i-1);
}
}
void printContent(SqList *L)
{
for(int i = 1; i<= L->length; i++)
{
printf("%d \t",L->data[i] );
}
}
int main(void)
{
SqList l ;
l.data={-1,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1};
l.length = 16;
printContent(&l);
printf("\n");
HeapSort(&l);
printContent(&l);
printf("\n");
return 0;
}
Note that: we create array with [0] empty , for the conveniece for j=2*start, during of computing the tree.
EOF
we now represent the heap in data stuct of Heap in the way of array.
In order to finish heap sort, we need to do 2 things:
build a heap
remove the root ,which is largest/smallest element , and rebuild the heap
Here is the code:
#include <stdio.h>
#include "type.h"
void swap(ElemType *a, ElemType *b)
{
ElemType tmp = *a;
*a=*b;
*b=tmp;
}
void HeapAdjust(SqList *L, int start, int end)
{
printf("Adjust %d %d \n", start, end);
int j, temp, i;
temp = L->data[start];
i = start;
//Note:2*start , if start==0, then j==0 . That's why we NEED THE LIST TO START FROM 1, INSTEAD OF 0
for(j = 2*start; j<=end; j*=2)
{
//compare left child [2*start] and right child [2*start+1]
if(L->data[j]<L->data[j+1] && j<end)
j++;//right child is larger than left child
if(temp>=L->data[j])//temp(position [start]) is already the largest
break;
L->data[start] = L->data[j];// Note:change
start = j;//above line changed the heap, we now need to reconstruct
}
printf("largest now[%d]:%d <-> [%d]:%d \n",start, L->data[start], temp, i);
L->data[start] = temp;
}
void HeapSort(SqList *L)
{
//construct the list into a heap, starting from nodes with children
for(int i = (L->length)/2; i>0; i--)
HeapAdjust(L, i, L->length);
//swap out the largest , and then reconstruct the list into a new heap
for(int i=L->length; i>1; i--)
{
swap(&(L->data[1]), &(L->data[i]));
HeapAdjust(L, 1, i-1);
}
}
void printContent(SqList *L)
{
for(int i = 1; i<= L->length; i++)
{
printf("%d \t",L->data[i] );
}
}
int main(void)
{
SqList l ;
l.data={-1,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1};
l.length = 16;
printContent(&l);
printf("\n");
HeapSort(&l);
printContent(&l);
printf("\n");
return 0;
}
Note that: we create array with [0] empty , for the conveniece for j=2*start, during of computing the tree.
EOF
相关文章推荐
- 堆排序 Heap Sort
- 堆积排序(HeapSort) - 改良的选择排序
- 排序1+4:归并排序(MergeSort)和堆排序(HeapSort)
- 排序算法(Max_Heap_Sort最大堆排序)
- 排序1+4:归并排序(MergeSort)和堆排序(HeapSort)
- 排序——堆排序(Heap Sortd)
- 八大排序算法之四选择排序—堆排序(Heap Sort)
- PAT - 甲级 - 1098. Insertion or Heap Sort (25)(排序)
- 09-排序3 Insertion or Heap Sort
- 09-排序3 Insertion or Heap Sort
- Atitit 算法之道 attilax著 1. 第二部分(Part II) 排序与顺序统计(Sorting and Order Statistics) 1 2. 第六章 堆排序(Heapsort)
- 堆积排序-堆排序-heap sort
- 堆排序 heapsort
- PHP实现排序堆排序(Heap Sort)算法
- 堆排序heapSort_legend
- 浙大pat | 浙大pat 牛客网甲级 1098. Insertion or Heap Sort (25)判断是哪种排序方式
- 基本排序方法及分析(七):HeapSort 堆排序
- 一天一排序之“堆排序(heapsort)”
- 堆排序(Heap-Sort)
- 堆排序源码(heap sort)