Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.59 KB | None | 0 0
  1. Your assignment should consist of a single file called hw2.cpp that
  2. contains:
  3.  
  4. your name
  5.  
  6. * a class timer (given here):
  7.  
  8. class Timer{
  9.  
  10. int start;
  11. public:
  12. Timer(){start=clock();}
  13. void reset(){start=clock();}
  14. double elapsed(){return (clock()-start)/CLOCKS_PER_SEC;}
  15. };
  16.  
  17. * the function fill_array
  18.  
  19. * the three functions topm_1, topm_2, and topm_3
  20.  
  21. * the function max_element
  22.  
  23. * the function quicksort
  24.  
  25. * the function quickselect
  26.  
  27. * a function main whose output looks something like this:
  28.  
  29. ______________________________________________________________
  30. Which algorithm (1,2,3 or 0=exit): 1
  31. Enter the value of n: 10000
  32. Enter the value of m: 10
  33. Generating random array of length 10000.
  34. The top 10 numbers in the array are
  35. 32767 32764 32764 32760 32758 32750 32748 32746 32745 32739
  36. Time to compute: approx. 0.01 sec
  37. Which algorithm (1,2,3 or 0=exit): 3
  38. Enter the value of n: 10000
  39. Enter the value of m: 10
  40. Generating random array of length 10000.
  41. The top 10 numbers in the array are
  42. 32767 32766 32758 32758 32757 32751 32736 32735 32732 32732
  43. Time to compute: approx. 0 sec
  44. Which algorithm (1,2,3 or 0=exit): 0
  45. Thank you.
  46. ______________________________________________________________
  47.  
  48. Before the main function executes an algorithm, it should generate an
  49. array of the appropriate size (implemented as a vector), and fill that
  50. array with random positive integers using the function fill_array.
  51.  
  52. The problem addressed in this programming assignment is to output,
  53. in decreasing order, the largest m elements in an unsorted input array
  54. of n elements. The entries of the input array are assumed to be
  55. positive integers.
  56.  
  57. For example, if the input array is [27, 52, 66, 55, 13, 10, 100, 7]
  58. and m=4, then the algorithm should output
  59. 100, 66, 55, 52
  60.  
  61. Note: If the array contains duplicate values, then the output
  62. list may contain duplicates. For example, if the input array is
  63. [10 2 7 4 10 5 7 9] and m=5, then the output should be 10 10 9 7 7
  64.  
  65. Call this problem the "top m problem".
  66.  
  67. 1) Consider the following algorithm for solving the top m problem.
  68.  
  69. ***********************************************************
  70. Repeat m times on the array:
  71.  
  72. Find the maximum element in the array (if there's more
  73. than one, find just one.)
  74. Output it
  75. Replace it in the array with the number 0.
  76.  
  77. ***********************************************************
  78.  
  79. Write a function max_element(const vector <int> &A) that returns
  80. the index of the maximum element of the array.
  81.  
  82. Then write a function
  83. topm_1(const vector <int> & A, int m) which
  84. implements the above algorithm for solving the top m problem.
  85.  
  86.  
  87. 2) A second algorithm for solving the top m problem is as follows.
  88.  
  89. ************************************************************
  90.  
  91. Sort the array using quicksort.
  92. Use the median of three method for partitioning.
  93.  
  94. Output the elements in positions n-1 through n-m in the
  95. sorted array.
  96.  
  97. ************************************************************
  98.  
  99.  
  100.  
  101. Write a function topm_2(const vector <int> & A, int m) which
  102. implements this second algorithm.
  103. The function topm_2 should call a quicksort function,
  104. which you should also write. Your quicksort function
  105. should use median-of-three partitioning, but shouldn't
  106. use a cutoff (that is, it shouldn't call insertion sort
  107. on small arrays). So, it should be similar to the quicksort code
  108. on p. 347, but without the cutoff.
  109.  
  110. Warning: The quicksort code on p. 347 does partitioning in lines
  111. 22 through 33. This partitioning does not work
  112. properly for arrays of size 2 (The code was written assuming
  113. that CUTOFF was at least 4, so that quicksort is
  114. only used on arrays of length at least 3.)
  115. You should handle arrays of size 2 as a special case.
  116.  
  117. 3) A third algorithm for solving the top m problem uses
  118. the quickselect algorithm discussed in Section 9.7 of the
  119. textbook.
  120.  
  121. ************************************************************
  122.  
  123. Use quickselect to find the mth largest element in
  124. the array (that is, the element of
  125. rank n-m+1). When quickselect finishes, the
  126. largest m elements in the array will be in positions
  127. n-m through n-1 of the copied array, but they
  128. may not be in sorted order.
  129.  
  130. Sort the largest m elements using quicksort, and output
  131. them in descending order.
  132.  
  133. ************************************************************
  134.  
  135. Write a function topm_3(const vector <int> & A, int m) that
  136. implements this third algorithm, and calls quicksort and
  137. a quickselect function. You should also write quickselect.
  138. Your quickselect can be essentially like the version
  139. in the textbook on p. 350, but do not use a cutoff
  140. (that is, do not do insertion sort for small arrays).
  141. Again, as with quicksort, you should handle arrays
  142. of length 2 as a special case.
  143.  
  144.  
  145. 4) Write a function fill_array(A)
  146. that takes as input a vector A, and fills the
  147. entries of the vector with random positive integers.
  148. You will probably want to use the function rand(),
  149. which requires you to include the header file <cstdlib>.
  150. The expression (1 + rand()) will produce
  151. a random positive integer between 1 and
  152. RAND_MAX + 1. RAND_MAX is a constant
  153. whose value is defined in cstdlib (in one
  154. recent version of stdlib, RAND_MAX is 32767).
  155.  
  156. 5) Using an STL multiset container, design a sorting algorithm for any datatype (which can be stored in the set). Remember a multiset stores things in sorted order.
  157.  
  158. Your function should have the following interface:
  159. void mySort1(vector<int>& v); //not int, but ANY data type.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement