{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

2c03-review - 00040 - int t=A[0 A[0]=A[i A[i]=t...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
9 } void maxHeapify(int A[],int i,int heapSize){ int r=right(i); int l=left(i); int largest; if(l<heapSize && A[l]>A[i]) largest=l; else largest=i; if(r<heapSize && A[r]>A[largest]) largest=r; if(largest!=i){ int t=A[i]; A[i]=A[largest]; A[largest]=t; maxHeapify(A,largest,heapSize); } } void buildMaxHeap(int A[],int heapSize){ for(int i=heapSize/2-1;i>=0;i--) maxHeapify(A,i,heapSize); } void heapSort(int A[],int size){ buildMaxHeap(A,size); for(int i=size-1;i!=0;i--){
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: int t=A[0]; A[0]=A[i]; A[i]=t; maxHeapify(A,0,i); } } void main(){ cout<<"This programs takes a list of integers and returns most frequent element(time complexity is O(nlgn))"<<endl; cout<<"---------------------------------------------------------------------"<<endl; int size; //number of elements int *A; //list of integers cout<<"Please enter the number of elements:"<<endl; cin>>size; cout<<"Please enter elements:"<<endl; A=new int[size];...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online