微软等数据结构+算法面试100题005
2011-01-20 19:44
316 查看
#include <iostream>
void downHeap(int heapArray[], int lengtArray, int key)
{
int temp = key;
int flag = 0;
int maxkey ;
while ( temp <= (lengtArray-2) / 2 ) {
if ( temp*2 + 2 <= lengtArray -1 ) {//最后一个节点有右儿子
maxkey = ( heapArray[temp*2+1] < heapArray[temp*2+2] ) ? (temp*2+2) : (temp*2+1) ;
}
else {
maxkey = temp*2 + 1;
}
if ( heapArray[temp] < heapArray[maxkey]) {
flag = heapArray[temp];
heapArray[temp] = heapArray[maxkey];
heapArray[maxkey] = flag ;
temp = maxkey ; //继续向下搜索
}
else
return ;
}
}
void buildMaxHeap(int heapArray[], int lengthArray)
{
for (int i = (lengthArray - 2) / 2 ; i >= 0; i --) {
downHeap(heapArray, lengthArray, i);
}
}
int mindata(int heapArray[])
{
return heapArray[0];
}
void Printheap(int heapArray[], int length)
{
int i = length - 1;
int temp;
while( i > 1 ) {
temp = heapArray[i];
heapArray[i] = heapArray[0];
heapArray[0] = temp;
if ( i > 2) { // 因为是downHeap是为了建最大堆,当只有两个数时,则经过一次转换已经是排好序了,不需要了。
downHeap(heapArray, i, 0);
}
i--;
}
i = 0;
while(i < length ){
std::cout<<heapArray[i]<<" ";
i++;
}
std::cout<<std::endl;
}
void findtheleast(int number, int data[], int length, int heapArray[])
{
for(int i = 0; i < number; i++ ) {
heapArray[i] = data[i];
}
buildMaxHeap(heapArray, number);
for(int i = number; i < length; i++) {
if ( data[i] < mindata(heapArray) ) {
heapArray[0] = data[i];
downHeap(heapArray, number, 0);
/* for (int i = 0; i< 4; i++){
std::cout<<heapArray[i]<<" ";
}
std::cout<<std::endl;*/
}
}
Printheap(heapArray, number);
}
int main()
{
int data[] ={8,7,6,5,4,3,2,1};
int heapArray[4] ={0};
findtheleast(4, data, sizeof(data)/sizeof(int), heapArray);
return 0;
}
#include <iostream>
void downHeap(int heapArray[], int lengtArray, int key)
{
int temp = key;
int flag = 0;
int maxkey ;
while ( temp <= (lengtArray-2) / 2 ) {
if ( temp*2 + 2 <= lengtArray -1 ) {//最后一个节点有右儿子
maxkey = ( heapArray[temp*2+1] < heapArray[temp*2+2] ) ? (temp*2+2) : (temp*2+1) ;
}
else {
maxkey = temp*2 + 1;
}
if ( heapArray[temp] < heapArray[maxkey]) {
flag = heapArray[temp];
heapArray[temp] = heapArray[maxkey];
heapArray[maxkey] = flag ;
temp = maxkey ; //继续向下搜索
}
else
return ;
}
}
void buildMaxHeap(int heapArray[], int lengthArray)
{
for (int i = (lengthArray - 2) / 2 ; i >= 0; i --) {
downHeap(heapArray, lengthArray, i);
}
}
int mindata(int heapArray[])
{
return heapArray[0];
}
void Printheap(int heapArray[], int length)
{
int i = length - 1;
int temp;
while( i > 1 ) {
temp = heapArray[i];
heapArray[i] = heapArray[0];
heapArray[0] = temp;
if ( i > 2) { // 因为是downHeap是为了建最大堆,当只有两个数时,则经过一次转换已经是排好序了,不需要了。
downHeap(heapArray, i, 0);
}
i--;
}
i = 0;
while(i < length ){
std::cout<<heapArray[i]<<" ";
i++;
}
std::cout<<std::endl;
}
void findtheleast(int number, int data[], int length, int heapArray[])
{
for(int i = 0; i < number; i++ ) {
heapArray[i] = data[i];
}
buildMaxHeap(heapArray, number);
for(int i = number; i < length; i++) {
if ( data[i] < mindata(heapArray) ) {
heapArray[0] = data[i];
downHeap(heapArray, number, 0);
/* for (int i = 0; i< 4; i++){
std::cout<<heapArray[i]<<" ";
}
std::cout<<std::endl;*/
}
}
Printheap(heapArray, number);
}
int main()
{
int data[] ={8,7,6,5,4,3,2,1};
int heapArray[4] ={0};
findtheleast(4, data, sizeof(data)/sizeof(int), heapArray);
return 0;
}#include <iostream>
void downHeap(int heapArray[], int lengtArray, int key)
{
int temp = key;
int flag = 0;
int maxkey ;
while ( temp <= (lengtArray-2) / 2 ) {
if ( temp*2 + 2 <= lengtArray -1 ) {//最后一个节点有右儿子
maxkey = ( heapArray[temp*2+1] < heapArray[temp*2+2] ) ? (temp*2+2) : (temp*2+1) ;
}
else {
maxkey = temp*2 + 1;
}
if ( heapArray[temp] < heapArray[maxkey]) {
flag = heapArray[temp];
heapArray[temp] = heapArray[maxkey];
heapArray[maxkey] = flag ;
temp = maxkey ; //继续向下搜索
}
else
return ;
}
}
void buildMaxHeap(int heapArray[], int lengthArray)
{
for (int i = (lengthArray - 2) / 2 ; i >= 0; i --) {
downHeap(heapArray, lengthArray, i);
}
}
int mindata(int heapArray[])
{
return heapArray[0];
}
void Printheap(int heapArray[], int length)
{
int i = length - 1;
int temp;
while( i > 1 ) {
temp = heapArray[i];
heapArray[i] = heapArray[0];
heapArray[0] = temp;
if ( i > 2) { // 因为是downHeap是为了建最大堆,当只有两个数时,则经过一次转换已经是排好序了,不需要了。
downHeap(heapArray, i, 0);
}
i--;
}
i = 0;
while(i < length ){
std::cout<<heapArray[i]<<" ";
i++;
}
std::cout<<std::endl;
}
void findtheleast(int number, int data[], int length, int heapArray[])
{
for(int i = 0; i < number; i++ ) {
heapArray[i] = data[i];
}
buildMaxHeap(heapArray, number);
for(int i = number; i < length; i++) {
if ( data[i] < mindata(heapArray) ) {
heapArray[0] = data[i];
downHeap(heapArray, number, 0);
/* for (int i = 0; i< 4; i++){
std::cout<<heapArray[i]<<" ";
}
std::cout<<std::endl;*/
}
}
Printheap(heapArray, number);
}
int main()
{
int data[] ={8,7,6,5,4,3,2,1};
int heapArray[4] ={0};
findtheleast(4, data, sizeof(data)/sizeof(int), heapArray);
return 0;
}
基本的思路是:通过一个4个单元的数组,记录当前的最小的4个数 通过堆排序。 最后将这四个数输出。
void downHeap(int heapArray[], int lengtArray, int key)
{
int temp = key;
int flag = 0;
int maxkey ;
while ( temp <= (lengtArray-2) / 2 ) {
if ( temp*2 + 2 <= lengtArray -1 ) {//最后一个节点有右儿子
maxkey = ( heapArray[temp*2+1] < heapArray[temp*2+2] ) ? (temp*2+2) : (temp*2+1) ;
}
else {
maxkey = temp*2 + 1;
}
if ( heapArray[temp] < heapArray[maxkey]) {
flag = heapArray[temp];
heapArray[temp] = heapArray[maxkey];
heapArray[maxkey] = flag ;
temp = maxkey ; //继续向下搜索
}
else
return ;
}
}
void buildMaxHeap(int heapArray[], int lengthArray)
{
for (int i = (lengthArray - 2) / 2 ; i >= 0; i --) {
downHeap(heapArray, lengthArray, i);
}
}
int mindata(int heapArray[])
{
return heapArray[0];
}
void Printheap(int heapArray[], int length)
{
int i = length - 1;
int temp;
while( i > 1 ) {
temp = heapArray[i];
heapArray[i] = heapArray[0];
heapArray[0] = temp;
if ( i > 2) { // 因为是downHeap是为了建最大堆,当只有两个数时,则经过一次转换已经是排好序了,不需要了。
downHeap(heapArray, i, 0);
}
i--;
}
i = 0;
while(i < length ){
std::cout<<heapArray[i]<<" ";
i++;
}
std::cout<<std::endl;
}
void findtheleast(int number, int data[], int length, int heapArray[])
{
for(int i = 0; i < number; i++ ) {
heapArray[i] = data[i];
}
buildMaxHeap(heapArray, number);
for(int i = number; i < length; i++) {
if ( data[i] < mindata(heapArray) ) {
heapArray[0] = data[i];
downHeap(heapArray, number, 0);
/* for (int i = 0; i< 4; i++){
std::cout<<heapArray[i]<<" ";
}
std::cout<<std::endl;*/
}
}
Printheap(heapArray, number);
}
int main()
{
int data[] ={8,7,6,5,4,3,2,1};
int heapArray[4] ={0};
findtheleast(4, data, sizeof(data)/sizeof(int), heapArray);
return 0;
}
#include <iostream>
void downHeap(int heapArray[], int lengtArray, int key)
{
int temp = key;
int flag = 0;
int maxkey ;
while ( temp <= (lengtArray-2) / 2 ) {
if ( temp*2 + 2 <= lengtArray -1 ) {//最后一个节点有右儿子
maxkey = ( heapArray[temp*2+1] < heapArray[temp*2+2] ) ? (temp*2+2) : (temp*2+1) ;
}
else {
maxkey = temp*2 + 1;
}
if ( heapArray[temp] < heapArray[maxkey]) {
flag = heapArray[temp];
heapArray[temp] = heapArray[maxkey];
heapArray[maxkey] = flag ;
temp = maxkey ; //继续向下搜索
}
else
return ;
}
}
void buildMaxHeap(int heapArray[], int lengthArray)
{
for (int i = (lengthArray - 2) / 2 ; i >= 0; i --) {
downHeap(heapArray, lengthArray, i);
}
}
int mindata(int heapArray[])
{
return heapArray[0];
}
void Printheap(int heapArray[], int length)
{
int i = length - 1;
int temp;
while( i > 1 ) {
temp = heapArray[i];
heapArray[i] = heapArray[0];
heapArray[0] = temp;
if ( i > 2) { // 因为是downHeap是为了建最大堆,当只有两个数时,则经过一次转换已经是排好序了,不需要了。
downHeap(heapArray, i, 0);
}
i--;
}
i = 0;
while(i < length ){
std::cout<<heapArray[i]<<" ";
i++;
}
std::cout<<std::endl;
}
void findtheleast(int number, int data[], int length, int heapArray[])
{
for(int i = 0; i < number; i++ ) {
heapArray[i] = data[i];
}
buildMaxHeap(heapArray, number);
for(int i = number; i < length; i++) {
if ( data[i] < mindata(heapArray) ) {
heapArray[0] = data[i];
downHeap(heapArray, number, 0);
/* for (int i = 0; i< 4; i++){
std::cout<<heapArray[i]<<" ";
}
std::cout<<std::endl;*/
}
}
Printheap(heapArray, number);
}
int main()
{
int data[] ={8,7,6,5,4,3,2,1};
int heapArray[4] ={0};
findtheleast(4, data, sizeof(data)/sizeof(int), heapArray);
return 0;
}#include <iostream>
void downHeap(int heapArray[], int lengtArray, int key)
{
int temp = key;
int flag = 0;
int maxkey ;
while ( temp <= (lengtArray-2) / 2 ) {
if ( temp*2 + 2 <= lengtArray -1 ) {//最后一个节点有右儿子
maxkey = ( heapArray[temp*2+1] < heapArray[temp*2+2] ) ? (temp*2+2) : (temp*2+1) ;
}
else {
maxkey = temp*2 + 1;
}
if ( heapArray[temp] < heapArray[maxkey]) {
flag = heapArray[temp];
heapArray[temp] = heapArray[maxkey];
heapArray[maxkey] = flag ;
temp = maxkey ; //继续向下搜索
}
else
return ;
}
}
void buildMaxHeap(int heapArray[], int lengthArray)
{
for (int i = (lengthArray - 2) / 2 ; i >= 0; i --) {
downHeap(heapArray, lengthArray, i);
}
}
int mindata(int heapArray[])
{
return heapArray[0];
}
void Printheap(int heapArray[], int length)
{
int i = length - 1;
int temp;
while( i > 1 ) {
temp = heapArray[i];
heapArray[i] = heapArray[0];
heapArray[0] = temp;
if ( i > 2) { // 因为是downHeap是为了建最大堆,当只有两个数时,则经过一次转换已经是排好序了,不需要了。
downHeap(heapArray, i, 0);
}
i--;
}
i = 0;
while(i < length ){
std::cout<<heapArray[i]<<" ";
i++;
}
std::cout<<std::endl;
}
void findtheleast(int number, int data[], int length, int heapArray[])
{
for(int i = 0; i < number; i++ ) {
heapArray[i] = data[i];
}
buildMaxHeap(heapArray, number);
for(int i = number; i < length; i++) {
if ( data[i] < mindata(heapArray) ) {
heapArray[0] = data[i];
downHeap(heapArray, number, 0);
/* for (int i = 0; i< 4; i++){
std::cout<<heapArray[i]<<" ";
}
std::cout<<std::endl;*/
}
}
Printheap(heapArray, number);
}
int main()
{
int data[] ={8,7,6,5,4,3,2,1};
int heapArray[4] ={0};
findtheleast(4, data, sizeof(data)/sizeof(int), heapArray);
return 0;
}
基本的思路是:通过一个4个单元的数组,记录当前的最小的4个数 通过堆排序。 最后将这四个数输出。
相关文章推荐
- 微软等数据结构+算法面试100题全部答案集锦
- [答案V0.1版]精选微软等数据结构+算法面试100题 [前20题]
- [最新答案V0.3版]微软等数据结构+算法面试100题[第21-40题答案]
- 微软等数据结构+算法面试100题全部答案集锦
- 微软等数据结构+算法面试100题,为什么会这样火?
- 微软等数据结构+算法面试100题(2)--给出一个函数来输出一个字符串的所有排列
- 微软等数据结构+算法面试100题(14)--实现strstr 功能
- 微软等数据结构+算法面试100题(39)-- 左旋数组中查找
- 微软等数据结构+算法面试 100 题
- [答案V0.1版]精选微软等数据结构+算法面试100题 [前20题]
- [最新答案V0.3版]微软等数据结构+算法面试100题[第21-40题答案]
- [答案V0.2版]精选微软等数据结构+算法面试100题[1-20]
- 微软等数据结构+算法面试100题(15)--求最大连续递增数字串
- 微软等数据结构+算法面试100题(40)-- 旋转数组中的最小元素
- [最新答案V0.3版]微软等数据结构+算法面试100题[第21-40题答案]
- 横空出世,席卷互联网--评微软等公司数据结构和算法面试100题
- 珍藏版、微软等数据结构+算法面试100题全部出炉
- 评微软等公司数据结构和算法面试100题
- [答案V0.1版]精选微软等数据结构+算法面试100题 [前20题]
- [最新答案V0.3版]微软等数据结构+算法面试100题[第21-40题答案]