堆排序算法实现(C++)
2012-04-17 15:47
211 查看
/* * main.cpp * * Created on: 2012-3-16 * Author: csc */ #include <iostream> using namespace std; int a[] = {49,38,65,97,76,13,27,50}; void FixTree(int a[], int i) { cout<<"i: "<<i<<endl; int j = (i-1)/2; int temp =a[i]; while(j >= 0) { if(a[j] > a[i]) break; temp = a[i]; a[i] = a[j]; a[j] = temp; i = j; if(j == 0) break; j = (j-1)/2; } } void delete_elem_tree(int a[],int i, int n) { int temp = a[0]; i = 0; int j = 2*i +1; while(j<n) { if(j+1<n &&a[j+1]>a[j]) j++; if(a[j]<temp) break; a[i] = a[j]; i = j; j = 2*j+1; } a[i] = temp; } void insertTree(int a[], int n, int value) { a = value; FixTree(a, n); } int main(void) { for(int i=1; i<8; i++) { insertTree(a, i, a[i]); } for(int i =0; i<8; i++) cout<<a[i]<<" "; cout<<endl; a[0] = a[7]; delete_elem_tree(a,0, 7); for(int i = 0; i<7; i++) cout<<a[i]<<" "; cout<<endl; }
相关文章推荐
- 堆排序算法java实现
- 堆排序算法(Heap Sort) Java实现
- 堆排序算法的Java实现
- 解读堆排序算法及用C++实现基于最大堆的堆排序示例
- Java实现的堆排序算法
- java编程题:用Java实现一个堆排序算法
- C语言实现堆排序算法
- 堆排序算法之JAVA实现
- 堆排序算法(C#实现)
- 堆排序算法及go语言实现
- 堆排序算法的讲解及Java版实现
- 堆排序算法——Java实现
- 堆排序学习笔记及堆排序算法的python实现
- 堆排序算法的实现
- 堆排序算法的实现
- 我的“堆排序算法”实现
- Swift实现堆排序算法的代码示例
- 堆排序算法实现
- 这星期实现的快速排序算法还有堆排序算法
- 堆排序算法与PHP实现