数据结构例题 堆排序

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
int n = 8;
Output(a, n);
HeapSort(a, n);
Output(a, n);
return 0;
}
/*
49 38 65 97 76 13 27 49
13 27 38 49 49 65 76 97
Press any key to continue
*/
逆序输出:
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Heap_Sort(n);
puts("\nThe sequence after Big heap_sort is:");
for(i=1;i<=n;i++)
printf("%4d",R[i]);
puts("\n Press any key to quit...");
getch();
}
public class MaxHeap{
3两个方法的参数类型,参数个数,参数顺序必须相同;
4子类方法的权限必须不小于父类方法的权限;
5子类方法只能抛出父类方法声明抛出的异常活异常子类。
重载的规则如下:
1多个函数的名称相同;
2函数的参数个数不同,或参数类型不同,或参数顺序不同;
该算法的附加存储主要是执行记录交换时所用的一个临时记录。
因此,该算法的空间复杂性为O(1)。
堆排序是一个不稳定的排序方法。
#include <stdio.h>
void swap(int &x, int &y)
{
int temp = x;
x = y;
y = temp;
}
void Adjust(int *a, int parent, int high)
j++;
if(Heap.key() >= Heap.key()) return;
Dsutil.swap(Heap,pos,j);
pos = j;
}
}
public void insert(Elem val){
Assert.notFalse(n < size,"Heap is full");
int curr = n++;
return 2 * pos + 2;
}
public int parent(int pos){
Assert.notFalse(pos > 0,"Position has no parent");
return (pos - 1) / 2;
}
public void buildheap(){
for(int i = n/2 - 1;i >= 0;i--) siftdown(i);
Dsutil.swap(Heap,pos,--n);
if(n != 0)
siftdown(pos);
return Heap;
}
}
堆排序分为两个步骤:
第一步,根据初始输入数据,利用堆的调整算法形成初始堆。
第二步,通过一系列的记录交换和重新调整堆进行排序。
最大堆的向下调整算法:
调用了O(n)次Adjust()算法,堆排序的时间复杂性为O(nlog2n)。
}
public int leftchild(int pos){
Assert.notFalse(pos< n/2,"Position has no left child");
return 2 * pos + 1;
}
public int rightchild(int pos){
Assert.notFalse(pos < (n - 1)/2,"Position has no right child");
if (temp<R[j]) break;
R[s]=R[j];
s=j;
j=j*2;
}/* end of while */
R[s]=temp;
} /* end of Heapify */
void BuildHeap(int n)
{ /*由一个无序的序列建成一个堆*/
int i;
for(i=n/2;i>0;i--)
{
int l = 2 * parent + 1;
int r = l + 1;
int flag = parent;
if (l<=high && a[l]>a[flag]) {
flபைடு நூலகம்g = l;
}
if (r<=high && a[r]>a[flag]) {
flag = r;
}
if (flag != parent){
swap(a[parent], a[flag]);
Adjust(a, flag, high);
}
}
void HeapSort(int *a, int n)
{
int i;
for (i=n-1; i>=0; i--) {
Adjust(a, i, n - 1);
}
for (i=n-1; i>=0; i--) {
Heap = val;
while((curr != 0) && (Heap.key() > Heap.key())){
Dsutil.swap(Heap,curr,parent(curr));
curr = parent(curr);
}
}
public Elem removemax(){
Assert.notFalse(n > 0,"Removing from empty Heap");
swap(a[0], a[i]);
Adjust(a, 0, i - 1);
}
}
void Output(int *a, int n)
{
int i;
for (i=0; i<n; i++) {
printf("\t%d", a[i]);
}
printf("\n");
}
int main()
{
int a[] = { 49, 38, 65, 97, 76, 13, 27, 49 };
Heapify(i,n);
}
void Heap_Sort(int n)
{ /*对R[1..n]进行堆排序,不妨用R[0]做暂存单元*/
int i;
BuildHeap(n); /*将R[1-n]建成初始堆*/
for(i=n;i>1;i--)
{ /*对当前无序区R[1..i]进行堆排序,共做n-1趟。*/
}
private void siftdown(int pos){
Assert.notFalse((pos >= 0) && (pos < n),"Illegal heap position");
while(! isLeaf(pos)){
int j = leftchild(pos);
if((j < (n - 1)) && (Heap.key() < Heap.key()))
private Elem h,int num,int max){
Heap = h;n = num;size = max;buildheap();
}
public int heapSize(){
return n;
}
public boolean isLeaf(int pos){
return (pos >= n/2) && (pos < n);
数据结构例题堆排序
#include <stdio.h>
#define MAX 255
int R[MAX];
void Heapify(int s,int m)
{ /*对R[1..n]进行堆调整,用temp做暂存单元*/
int j,temp;
temp=R[s];
j=2*s;
while (j<=m)
{
if (R[j]>R[j+1]&&j<m) j++;
//String str = br.readLine();
//StringBuffer sb = new StringBuffer(str);
//System.out.println(sb.reverse());
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
R[0]=R[1]; R[1]=R[i];R[i]=R[0]; /*将堆顶和堆中最后一个记录交换*/
Heapify(1,i-1); /*将R[1..i-1]重新调整为堆,仅有R[1]可能违反堆性质*/
} /* end of for */
} /* end of Heap_Sort */
void main()
String str = br.readLine();
StringBuffer sb = new StringBuffer(str);
System.out.println(sb.reverse());
重载和重写:
方法覆盖需要满足一下几个规则:
1发生在父类和子类的同名方法之间;
2两个方法的返回值类型必须相同;
{
int i,n;
clrscr();
puts("Please input total element number of the sequence:");
scanf("%d",&n);
if(n<=0||n>MAX)
{
printf("n must more than 0 and lessthan %d.\n",MAX);
exit(0);
}
puts("Please input the elements one by one:");
for(i=1;i<=n;i++)
scanf("%d",&R[i]);
puts("The sequence you input is:");
for(i=1;i<=n;i++)
printf("%4d",R[i]);
Dsutil.swap(Heap,0,--n);
if(n != 0)
siftdown(0);
return Heap;
}
public Elem remove(int pos){
Assert.notFalse((pos > 0) && (pos < n),"Illegal heap position");
相关文档
最新文档