Main » 2011 » November » 9 » 堆排序(以合并果子为例)
6:29 PM
堆排序(以合并果子为例)

堆:

我感觉着就是维护和建堆小困难,本人第一次写堆今天,代码很长,唉,日后会发一些后续代码,发上代码吧!
C++语言

made by PaulInsider!
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
int n,heap[10001],answer=0;
void weihu(int x);
void swap(int x,int y);
int main()
{
    freopen ("fruit.in","r",stdin);
    freopen ("fruit.out","w",stdout);
    scanf("%d",&n);
    for (int p=1;p<=n;p++)
    {
        scanf("%d",&heap[p]);
    }
    for (int j=(n>>1);j>=1;j--)
    {
        weihu(j);
    }
    while (1)
    {
        if (n==1)
        {
            break;
        }
        answer+=heap[1];
        if (3<=n)
        {
            if (heap[2]>heap[3])
            {
                answer+=heap[3];
                heap[1]+=heap[3];
                heap[3]=heap[n];
                n--;
                weihu(3);
                weihu(1);
            }
            else
            {
                answer+=heap[2];
                heap[1]+=heap[2];
                heap[2]=heap[n];
                n--;
                weihu(2);
                weihu(1);
            }
        }
        else
        {
            answer+=heap[2];
            heap[1]+=heap[2];
            heap[2]=heap[n];
            n--;
            weihu(2);
            weihu(1);
        }
    }
    cout<<answer;
    return 0;
}
void weihu(int x)
{
    int temp;
    temp=heap[x];
    int i;
    i=x;
    while (i<=n)
    {
        int a,b;
        a=(i<<1);
        b=(i<<1)+1;
        if (a>n)
        {
            break;
        }
        else
        {
            if (b>n)
            {
                if (heap[a]<heap[i])
                {
                    swap(a,i);
                    i=a;
                    continue;
                }
                else
                {
                    break;
                }
            }
            else
            {
                if (heap[a]<heap[b])
                {
                    if (heap[a]<heap[i])
                    {
                        swap(a,i);
                        i=a;
                    }
                    else
                    {
                        if (heap[a]==heap[i])
                        {
                            break;
                        }
                        if (heap[a]>heap[i])
                        {
                            break;
                        }
                    }
                }
                else
                {
                    if (heap[b]<heap[i])
                    {
                        swap(b,i);
                        i=b;
                    }
                    else
                    {
                        if (heap[b]==heap[i])
                        {
                            break;
                        }
                        if (heap[b]>heap[i])
                        {
                            break;
                        }
                    }
                }
            }
        }
    }
}
void swap(int x,int y)
{
    int temp;
    temp=heap[x];
    heap[x]=heap[y];
    heap[y]=temp;
}

有点长,神牛飘过,水牛留步!
Views: 488 | Added by: dandan | Tags: exercise | Rating: 0.0/0
Total comments: 0
Name *:
Email *:
Code *: