方法一:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            List<int> seqlist = new List<int> { 42, 20,17, 27,13,8,17,48};
            HeadOrder(seqlist, 0, seqlist.Count - 1);

        }

        public void BubbleSort(List<int> sqList)
        {
           
        }


        public static void CreadHeap(List<int> seqList, int low, int high)
        {            
            int k;
            int j=0;
            int temp = 0;

            for (int i = high / 2; i >= low; i--)
            {
                k = i;
                temp = seqList[i];

                j = 2 * k + 1;               

                while (j <= high)
                {                  
                    if (j < high 
                        && j + 1 <= high 
                        && seqList[j] < seqList[j + 1])
                    {
                        j++;
                    }

                    if (temp < seqList[j])
                    {
                        seqList[k] = seqList[j];                                          

                        k = j;
                        j = 2 * k + 1;
                    }
                    else
                    {
                        j = high + 1;
                    }
                }

                seqList[k] = temp;
            }

        }


        public static void HeadOrder(List<int> seqlist, int low, int high)
        {
            if (high <= low)
            {
                return;
            }
           
            int temp = 0;

            CreadHeap(seqlist, 0, high);

            for (int i = seqlist.Count - 1; i > low; i--)
            {

                temp = seqlist[0];

                seqlist[0] = seqlist[i];

                seqlist[i] = temp;

                CreadHeap(seqlist, 0, i-1);               
            }
        }

    }
}

 方法二:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            List<int> seqlist = new List<int> { 42, 20,17, 27,13,8,17,48};

            HeadOrder(seqlist, 0, seqlist.Count - 1);
        }

        public void BubbleSort(List<int> sqList)
        {
            int tmp = 0;

            for (int i = 0; i < sqList.Count -1; ++i)
            {
                for (int j = i +1; j<sqList.Count; j++)
                {
                    if (sqList[i] < sqList[j])
                    {
                        tmp = sqList[i];
                        sqList[i] = sqList[j];
                        sqList[j] = tmp;
                    }
                }
            }
        }


        public static void CreadHeap(List<int> seqList, int low, int high)
        {
            int k;
            int j = 0;
            int temp = 0;

            for (int i = high / 2; i >= low; i--)
            {
                k = i;
                j = 2 * k + 1;

                while (j <= high)
                {
                    temp = seqList[k];

                    if (j < high
                        && j + 1 <= high
                        && seqList[j] < seqList[j + 1])
                    {
                        j++;
                    }

                    if (temp < seqList[j])
                    {
                        seqList[k] = seqList[j];
                        seqList[j] = temp;

                        k = j;
                        j = 2 * k + 1;
                    }
                    else
                    {
                        j = high + 1;
                    }

                }
            }
        }


        public static void HeadOrder(List<int> seqlist, int low, int high)
        {
            if (high <= low)
            {
                return;
            }

            int i = low;
            int temp = 0;

            CreadHeap(seqlist, 0, high);

            temp = seqlist[0];

            seqlist[0] = seqlist[high];

            seqlist[high] = temp;

            HeadOrder(seqlist, i, high - 1);
        }

    }
}

 

 

posted on 2013-12-11 13:54  higirle  阅读(431)  评论(0编辑  收藏  举报