Microsoft and other data structures and algorithms interview question 5

Source: Internet
Author: User

Question 5
Find the minimum k Elements
Question: Enter n integers and output the smallest k integers.
For example, if you enter the 8 numbers 1, 2, 3, 5, 6, 7, and 8, the minimum four digits are 1, 2, 3, and 4.

Analysis: This question requires the minimum K Number of n Integers to be calculated. The question does not directly require complexity. Therefore, there are many solutions.
For example, there are many solutions such as output after sorting. If the complexity is required to be klogn, it is easier to think of the use of the shard (recursive) algorithm.

Here we use two methods. The first is the classic least heap algorithm, and the second is the sharding algorithm. One thing to note here is that these two algorithms are essentially the same and can both be recursive or divide. It is just a different expression in implementation.

Minimum heap code:
[Cpp]
# Include <iostream>
 
Using namespace std;
 
Class minHeap {
Private:
Int size;
Int * data;
Int currentSize;


Int Parent (int pos) const; // The value of the member variable is not changed.
 
Public:
Min heap (int size = 100 );
~ MinHeap () {delete [] data ;}
Bool Insert (const int node );
Void siftUp (int pos );
Void siftDown (); // put it in the left subtree
Int removeMin ();
 
};
 
 
Int minHeap: Parent (int pos) const
{
Return (pos-1)/2;
}
MinHeap: minHeap (const int size)
{
If (size <0)
Return;
Data = new int [size];
CurrentSize = 0;
}
 
Bool minHeap: Insert (const int node)
{
If (currentSize <size-1)
Return false;
Else {
Data [currentSize] = node;
SiftUp (currentSize );
CurrentSize = currentSize + 1;
Return true;
}
}
 
Void minHeap: siftUp (int pos)
{
Int temp;
While (pos> 0 & data [pos] <data [Parent (pos)])
{
Temp = data [pos];
Data [pos] = data [Parent (pos)];
Data [Parent (pos)] = temp;

Pos = Parent (pos );
}

}
 
Void minHeap: siftDown ()
{
// By default, it starts to drop from 0 and is placed in the left subtree.
 
Int temp;
// Switch to the last position
Temp = data [0];
Data [0] = data [currentSize-1];
Data [currentSize-1] = temp;
 
CurrentSize = currentSize-1;
 
Int I = 0;
While (I * 2 + 1 <= currentSize-1 & (data [I]> data [2 * I + 1] | data [I]> data [2 * I + 2 ]) // left and right subtree exists
{

If (I * 2 + 2 <= currentSize-1 ){
// If both left and right subtree have
If (data [2 * I + 1] <data [2 * I + 2])
{
Temp = data [I];
Data [I] = data [2 * I + 1];
Data [2 * I + 1] = temp;
I = 2 * I + 1;
}
Else
{
Temp = data [I];
Data [I] = data [2 * I + 2];
Data [2 * I + 2] = temp;
I = 2 * I + 2;
}
}
Else if (data [I]> data [2 * I + 1])
{
// If there is only the left subtree
Temp = data [I];
Data [I] = data [2 * I + 1];
Data [2 * I + 1] = temp;
I = 2 * I + 1;
}
Else
{
I = 2 * I + 1;
}
}


}
 
Int minHeap: removeMin ()
{
Int minValue = data [0];
SiftDown ();

Return minValue;
}
Int main ()
{
MinHeap B;
Int a [] = {2, 3, 4, 5, 7, 1, 9 };
For (int I = 0; I <sizeof (a)/sizeof (int); I ++)
B. Insert (a [I]);
For (int I = 0; I <sizeof (a)/sizeof (int); I ++)
Cout <B. removeMin () <endl;
Return 0;
}

 

Recursive code (max ):
[Cpp]
# Include <iostream>
 
Using namespace std;
 
 
Void maxHeap (int * a, int length, int index)
{
Int temp;
// No subtree
If (index * 2 + 1> length-1 ){}
// Only the left subtree
Else if (index * 2 + 1 = length-1)
{
If (a [index * 2 + 1]> a [index]) {
Temp = a [index * 2 + 1];
A [index * 2 + 1] = a [index];
A [index] = temp;
}
 

}
Else {
// Left/right subtree, recursion
MaxHeap (a, length, index * 2 + 1 );
Int max_left = a [index * 2 + 1];
MaxHeap (a, length, index * 2 + 2 );
Int max_right = a [index * 2 + 2];
 
If (max_left> max_right ){
If (a [index] <max_left ){
Temp = a [index * 2 + 1];
A [index * 2 + 1] = a [index];
A [index] = temp;
}

}
Else {
If (a [index] <max_right ){
Temp = a [index * 2 + 2];
A [index * 2 + 2] = a [index];
A [index] = temp;
}
}
}
 
}
 
 
Int maxHeapDelete (int * a, int length)
{
 
MaxHeap (a, length, 0 );
 
Int temp;
 
Temp = a [length-1];
A [length-1] = a [0];
A [0] = temp;
 
Return a [length-1];
 
}
 
Int main (){
 
Int a [8] = {1, 2, 6, 4, 3, 7, 9, 11 };
// MaxHeap (a, 8, 0 );
// Cout <a [0] <"";
 
Int temp;
For (int I = 0; I <8; I ++)
{
Cout <maxHeapDelete (a, 8-i) <"";
}
// For (int I = 0; I <8; I ++) cout <a [I] <"";
 
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.