2c03-review - 00039

2c03-review - 00039 - running time for sorting the list is...

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

View Full Document Right Arrow Icon
8 8.[20] Write a program (in pseudo-code) to find the mode (the most frequently occurring element) of a list of elements. What is a complexity of your program? [10] 1. MODE(A) 2. 3. // A: array of elements 4. // F: frequency 5. SORT(A) 6. m Å 0 7. l 0 8. F(1) 1 9. for i 2 to n 10. if A(i)=A(i-1) then 11. F(i)=F(i-1) + 1 12. else 13. F ( i ) 1 14. if F(i) m then 15. m F(i) 16. l i 17. return A(l) O(nlgn): sort O(n): determine the most frequent element Therefore, time complexity: O(nlgn) The bottleneck is sorting the list. Thus, the running time is O(nlogn), provided the
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: running time for sorting the list is O(nlogn). [10] C code: ************************************************************************ ****** #include <stdio.h> #include <iostreams.h> /////////////////////////////////////////////////////////////////////// /////////////// /* This programs takes a list of integers and returns most frequent element time complexity is O(nlgn) */ int right(int i){ return 2*i+2; } int left(int i){ return 2*i+1;...
View Full Document

Ask a homework question - tutors are online