首页 > 代码库 > 寻找长为N的数组的前M大的元素并输出

寻找长为N的数组的前M大的元素并输出

/*
 *寻找长为N的数组的前M大的元素并输出。

 *用堆的性质,使用数组N建立一个M大的最小堆,遍历数组剩余N-M个元素,与堆顶元素比较,如果大于堆顶,则与堆顶交换,并调整堆

 *最后输出堆内容即可

 *时间复杂度分析:  建堆时间O(M)
 *遍历数组,并在堆中比较时间O((N-M)logM)
 *总时间复杂度O(M)+ O((N-M)logM)
 *
 *空间复杂度 O(M)
 */
 
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define NDEBUG
#include <assert.h>

int  input(void);
void print(int * p,int len);
void adjust(int *heap,int heap_size,int index);
void swap(int *a,int *b);
void built_heap(int *heap,int heap_size,int *a);
void find_max(int *heap,int heap_size,int *a,int len);



int main(void)
{
  int heap_size;
  int *heap;
  int a[]={1,231,45,69,78,15,36,49,14,36,24,15,89,12,11,69,122,192,126,2125,1,12,4548,125,4755,232,            5,26,24,25,22,22,66,214,1235,2,332,56,66,662,552,225,226,226,2262,262,626,2626,2626,789,         622,15,25,262,551,789,781,895,895,9511,942,412,233,203,202,30,95,2,31,22,12,23,325,235,266,45678,15629,7894};
  heap_size=input();
  heap=(int *)malloc(sizeof(int)*heap_size);
  find_max(heap,heap_size,a,sizeof(a)/sizeof(int));
  print(heap,heap_size);
  free(heap);
  return 0;
}

void print(int * p,int len)
{
    int i;
    for(i=0;i<len;i++)
      printf("%d\n",p[i]);
    return;  
}

int  input(void)
{
    int m;
    printf("input the M:");
    scanf("%d",&m);
    return m;
}

//建立堆
void built_heap(int *heap,int heap_size,int *a)
{
    int i;
    assert(heap!=NULL&&a!=NULL);
    for(i=0;i<heap_size;i++)
         heap[i]=a[i];
    for(i=(heap_size>>1);i>=0;i--)
       adjust(heap,heap_size,i);
    return;         
}
//堆调整
void adjust(int *heap,int heap_size,int index)
{
   int left=(index<<1)+1;
   int right=(index<<1)+2;
   int parent=index;
   assert(heap!=NULL);
   if(left<heap_size&&heap[left]<heap[parent])
            parent=left;     
   if(right<heap_size&&heap[right]<heap[parent])
            parent=right;
   if(parent!=index)
     {
        swap(&heap[parent],&heap[index]);
        adjust(heap,heap_size,parent);
     }             
    return;  
}


void swap(int *a,int *b)
{
    *a=*a^*b;
    *b=*a^*b;
    *a=*a^*b;
}

void find_max(int *heap,int heap_size,int *a,int len)
{
     int i;
     int j;
     assert(heap!=NULL&a!=NULL);
     if(heap_size>=len)
     return;
     built_heap(heap,heap_size,a);
     for(i=heap_size;i<len;i++)
     {
         if(a[i]>heap[0])
            {
               heap[0]=a[i];
               adjust(heap,heap_size,0);
            }   
     }
     return;
}

以上程序运行结果:

[trageday@lei-yum code_test]$ gcc   -o arryN_maxM  arryN_maxM.c
[trageday@lei-yum code_test]$ ./arryN_maxM 
input the M:6
4548
4755
9511
7894
45678
15629




寻找长为N的数组的前M大的元素并输出