zoukankan      html  css  js  c++  java
  • 编程之美2.5:寻找最大的K个数

    编程之美2.5:寻找最大的K个数

    引申:寻找第k大的数:

    方法一:

    // 选择第k大的数(通过改进快速排序来实现)
            public static void SelectShort(int[] array, int low, int high, int k, out int value)
            {
                int i = low;
                int j = high;
                int tempItem = array[low];
                value = int.MinValue;
    
                while (low < high)
                {
                    while (array[high] > tempItem
                        && high > low)
                    {
                        high--;
                    }
    
                    if (low < high)
                    {
                        array[low] = array[high];
                        low++;
    
                        while (array[low] <= tempItem
                            && low < high)
                        {
                            low++;
                        }
    
                        array[high] = array[low];
                        high--;
                    }
                }
    
                array[low] = tempItem;
    
    
                if (low == array.Length - k)
                {
                    value = array[low];
                    return;
                }
                else if (low < array.Length -k) // 第k个元素在右分支中
                {
                    if (low + 1 < j)
                    {
                        SelectShort(array, low + 1, j, k, out value);
                    }               
                }
                else // (low > k-1)第k个元素在左分支中
                {
                    if (i < low - 1)
                    {
                        SelectShort(array, i, low - 1,k,out value);
                    }               
                }            
            }

     方法二:

    借助编程之美的思想,求前k个最大的数,其中最小的就是。

      // 创建最大堆
            public static void CreateStack(int[] array, int startIndex, int lastIndex)
            {
                for (int root = lastIndex / 2; root >= startIndex; root--)
                {
                    int currentRoot = root;
                    int leftLeafIndex = 2 * root + 1; // 左孩子
                    int rightLeafIndex = leftLeafIndex +1; // 右孩子
    
                    while (leftLeafIndex <= lastIndex)
                    {
                        if (leftLeafIndex < lastIndex
                            && rightLeafIndex <= lastIndex
                            && array[leftLeafIndex] < array[rightLeafIndex])// 右孩子存在而且右孩子大于左孩子
                        {
                            leftLeafIndex++;
                        }
    
                        if (array[leftLeafIndex] > array[root])
                        {
                            int temp = array[root];
                            array[root] = array[leftLeafIndex];
                            array[leftLeafIndex] = temp;
    
                            currentRoot = 2 * currentRoot + 1; //继续寻找是否到了叶子结点
                            leftLeafIndex = 2 * currentRoot + 1;
                        }
                        else
                        {
                            break;
                        }
                    }
                }
            }
    
            //堆排序
            public static void StackSort(int[] arrary)
            {
                CreateStack(arrary, 0, arrary.Length - 1);
    
                int temp;
    
                for (int j = arrary.Length - 1; j > 0; j--)
                {
                    temp = arrary[j];
                    arrary[j] = arrary[0];
                    arrary[0] = temp;
    
                    CreateStack(arrary, 0, j - 1);
                }
            }
  • 相关阅读:
    c#中开发ActiveX的学习笔记
    [转]李战大师悟透delphi 第七章 组织你的模块
    在网页中实现QQ的屏幕截图功能
    vs.net的调试小技巧之#define debug(适合新手)
    socket中的byte消息格式设计
    [转]李战大师悟透delphi第五章 包
    [转]李战大师悟透delphi 第九章 多层体系结构
    [转]李战大师悟透delphi第一章 delphi的原子世界
    重温delphi之控制台程序:Hello World!
    silverlight中的socket编程注意事项
  • 原文地址:https://www.cnblogs.com/Jessy/p/3508857.html
Copyright © 2011-2022 走看看