My classmate Dalong gave me a messageAlgorithmQuestion: How to pick out the minimum M number (M <= n) for an array with N unordered length )?
My first thought was to sort the entire array by fast sorting, traverse the sorted array, and select the m smallest number from the list. Although this method is feasible, it is not the best.
The heap sorting method can solve this problem well.Create a small top heap, and then throw the minimum element at the top of the heap each time. The minimum M number can be obtained after M cycles.
This algorithm can also be considered as an application of heap sorting.
View my other part of heap Sorting AlgorithmArticle:Http://www.cnblogs.com/mingmingruyuedlut/archive/2011/09/13/2175308.html
DetailsCodeAs follows:
/// <Summary>
/// Obtains the minimum N (count) number from the given array.
/// </Summary>
/// <Param name = "array"> Passed integer Array </Param>
/// <Param name = "count"> Returns the minimum count. </Param>
Private Static Void Getsmallernumbers ( Int [] Array, Int Count)
{
Buildminheap (array ); // Create a small push
For ( Int I = Array. Length - 1 ; I > = Array. Length - Count; I -- ) // Traverse small top push
{
Console. writeline (array [ 0 ]); // Output the top heel element (that is, the minimum number in the heap)
Swap ( Ref Array [ 0 ], Ref Array [I]); // Swap the smallest element at the top of the heap with the last element in the heap
Minheapify (array, 0 , I ); // Adjusted to a small top push
}
}
/// <Summary>
/// Create a small root heap
/// </Summary>
/// <Param name = "array"> Passed Array </Param>
Private Static Void Buildminheap ( Int [] Array)
{
// Based on the relationship between the heap and the array, we can see that the subscript ranges from 0 ~ The array element of array. Length/2-1 is the root node, and the other elements are leaf nodes.
For ( Int I = Array. Length / 2 - 1 ; I > = 0 ; I -- )
{
Minheapify (array, I, array. Length ); // Adjusted to small top push
}
}
/// <Summary>
/// Bottom-up: the process of adjusting the small root heap
/// </Summary>
/// <Param name = "array"> Passed Array </Param>
/// <Param name = "currentindex"> Current root node to be adjusted </Param>
/// <Param name = "heapsize"> The size of the small top heap (that is, the number of all array elements in the small top heap) </Param>
Private Static Void Minheapify ( Int [] Array, Int Currentindex, Int Heapsize)
{
Int Leftchildindex = Currentindex * 2 + 1 ; // Subscript of the Left subnode of the Root Node
Int Rightchildindex = Currentindex * 2 + 2 ; // Subscript of the right subnode of the Root Node
Int Smallestindex = Currentindex; // Subscript of the minimum element among the three (root node, left subnode, and right subnode)
If (Leftchildindex < Heapsize && Array [leftchildindex] < Array [smallestindex])
{
Smallestindex = Leftchildindex;
}
If (Rightchildindex < Heapsize && Array [rightchildindex] < Array [smallestindex])
{
Smallestindex = Rightchildindex;
}
If (Smallestindex ! = Currentindex) // The left and right subnodes are smaller than the root node element.
{
Swap ( Ref Array [currentindex], Ref Array [smallestindex]);
Minheapify (array, smallestindex, array. Length ); // Recursive Adjustment
}
}
/// <Summary>
/// Exchange Functions
/// </Summary>
/// <Param name = "A"> Element </Param>
/// <Param name = "B"> Element B </Param>
Private Static Void Swap ( Ref Int A, Ref Int B)
{
Int Temp = 0 ;
Temp = A;
A = B;
B = Temp;
}
....