2c03-review - 00040

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&lt;&lt;&quot;This programs takes a list of integers and returns most frequent element(time complexity is O(nlgn))&quot;&lt;&lt;endl; cout&lt;&lt;&quot;---------------------------------------------------------------------&quot;&lt;&lt;endl; int size; //number of elements int *A; //list of integers cout&lt;&lt;&quot;Please enter the number of elements:&quot;&lt;&lt;endl; cin&gt;&gt;size; cout&lt;&lt;&quot;Please enter elements:&quot;&lt;&lt;endl; A=new int[size];...
View Full Document

This note was uploaded on 12/10/2009 for the course CAS 2c03 taught by Professor Janicki during the Spring '03 term at McMaster University.

Ask a homework question - tutors are online