代码改变世界

topN 堆排序 (int 类型)

2012-06-26 15:00  yuejianjun  阅读(385)  评论(0编辑  收藏  举报
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics; 
namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            List<int> testValues = new List<int>();
            Random rand = new Random();  
            int count = 100;
            for (int i = 0; i < count; i++)
            {
                testValues.Add(rand.Next()); 
            }
           int[] testArr = new int[testValues.Count];
           testValues.CopyTo(testArr);
           Stopwatch sw = new Stopwatch();
           sw.Reset();
           sw.Start();
           TopSort(testArr, 5); 
           sw.Stop();
           Console.WriteLine(count+" 耗时毫秒:"+sw.ElapsedMilliseconds);
           Console.Read();
        }
        public static void TopSort(int [] array, int top )
        { 
            //Judge input
            if (array.Length <= 2 || top >= array.Length / 2)
            {
                Array.Sort(array);
                return;
            }
            //One time partition
            int pivot = PartitionInt(array, 0, array.Length - 1, array.Length / 2);
            int lastPivot = pivot;
            //Run until pivot near the top
            while ((!(lastPivot >= top && pivot <= top)))
            {
                lastPivot = pivot;
                if (pivot > top)
                {
                    pivot = PartitionInt(array, 0, pivot, pivot / 2);
                    if (pivot == lastPivot)
                    {
                        pivot--;
                    }
                }
                else
                {
                    if (pivot >= array.Length - 1)
                    {
                        lastPivot = array.Length - 1;
                        break;
                    }
                    pivot = PartitionInt(array, pivot + 1, array.Length - 1, (array.Length - pivot) / 2);
                }
            }
            //Finally sort
            if (lastPivot < array.Length)
            {
                Array.Sort(array, 0, lastPivot + 1);
            }
            else
            {
                Array.Sort(array, 0, lastPivot);
            }
        }
        private static int PartitionInt(int[] array, int low, int high, int pivotIndex)
        {
            int pivotValue = array[pivotIndex];
            array[pivotIndex] = array[low];
            array[low] = pivotValue;
            while (low < high)
            {
                while (array[high] >= pivotValue && high > low)
                {
                    --high;
                }
                if (high > low)
                {
                    array[low] = array[high];
                }
                while (array[low] <= pivotValue && high > low)
                {
                    ++low;
                }
                if (high > low)
                {
                    array[high] = array[low];
                }
            }
            array[low] = pivotValue;
            return low;
        }
    }
}