Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdlib.h>
- #include<stdio.h>
- #include<time.h>
- #define s 10
- long a[s];
- long heapsize;
- void max_heapify(long i){
- long largest;
- long l= 2*i+1;
- long r=2*i+2 ;
- if (l< heapsize && a[l] >a[i])
- largest =l;
- else
- largest =i;
- if (r< heapsize && a[r] >a[largest])
- largest =r;
- if(largest !=i){
- long temp1= a[i];
- a[i]=a[largest];
- a[largest] = temp1;
- max_heapify(largest);}
- }
- void build_max_heap(){
- heapsize = s ;
- for(long i=(s/2)-1 ;i>=0; i--)
- max_heapify(i);
- }
- void heap_sort(){
- build_max_heap();
- for(long i=s-1; i>0 ;i--)
- {long temp2 = a[i];
- a[i]=a[0];
- a[0]=temp2;
- heapsize =heapsize -1;
- max_heapify(0);}
- }
- int main () {
- long i=0,j=0,temp;
- clock_t begin,end;
- double time_spent;
- srand(time(NULL));
- for(long i= 0; i<s; i++){
- a[i]= rand()%s;}
- begin = clock();
- heap_sort();
- end=clock();
- time_spent=(double)(end-begin);
- for(long i=0; i<s; i++){
- printf(" %d ", a[i]);}
- printf("\nTime Elapsed:%f\n",time_spent);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement