Guest User

Untitled

a guest
Dec 19th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.52 KB | None | 0 0
  1. function findTopN(Array list, int n)
  2. {
  3. Set sortedSet<Integer> = new TreeSet<>(Comparators.naturalOrder());
  4.  
  5. // add all elements from list to sortedSet
  6.  
  7. // return the first n from sortedSet
  8. }
  9.  
  10. i = 0
  11. top[0] <= 5
  12.  
  13. i = 1
  14. top[1] <= 2
  15.  
  16. i = 2
  17. top[2] <= top[1] (2)
  18. top[1] <= top[0] (5)
  19. top[0] <= 8
  20.  
  21. i = 3
  22. top[2] <= top[1] (5)
  23. top[1] <= 7
  24.  
  25. i = 4
  26. top[2] <= top[1] (7)
  27. top[1] <= top[0] (8)
  28. top[0] <= 9
  29.  
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <time.h>
  33.  
  34. #define SRCSZ 100
  35. #define DSTSZ 10
  36.  
  37. int main (void) {
  38. int unused[SRCSZ], source[SRCSZ], dest[DSTSZ], i, j, pos;
  39.  
  40. srand (time (NULL));
  41. for (i = 0; i < SRCSZ; i++) {
  42. unused[i] = 1;
  43. source[i] = rand() % 1000;
  44. }
  45.  
  46. for (i = 0; i < DSTSZ; i++) {
  47. pos = -1;
  48. for (j = 0; j < SRCSZ; j++) {
  49. if (pos == -1) {
  50. if (unused[j]) {
  51. pos = j;
  52. }
  53. } else {
  54. if (unused[j] && (source[j] > source[pos])) {
  55. pos = j;
  56. }
  57. }
  58. }
  59. dest[i] = source[pos];
  60. unused[pos] = 0;
  61. }
  62.  
  63. printf ("Source:");
  64. for (i = 0; i < SRCSZ; i++) printf (" %d", source[i]);
  65. printf ("nDest:");
  66. for (i = 0; i < DSTSZ; i++) printf (" %d", dest[i]);
  67. printf ("n");
  68.  
  69. return 0;
  70. }
  71.  
  72. Source: 403 459 646 467 120 346 430 247 68 312 701 304 707 443
  73. 753 433 986 921 513 634 861 741 482 794 679 409 145 93
  74. 512 947 19 9 385 208 795 742 851 638 924 637 638 141
  75. 382 89 998 713 210 732 784 67 273 628 187 902 42 25
  76. 747 471 686 504 255 74 638 610 227 892 156 86 48 133
  77. 63 234 639 899 815 986 750 177 413 581 899 494 292 359
  78. 60 106 944 926 257 370 310 726 393 800 986 827 856 835
  79. 66 183 901
  80. Dest: 998 986 986 986 947 944 926 924 921 902
  81.  
  82. real 0m0.063s
  83. user 0m0.046s
  84. sys 0m0.031s
  85.  
  86. public interface FindTopValues
  87. {
  88. int[] findTopNValues(int[] data, int n);
  89. }
  90.  
  91. public class FindTopValuesInsertionSortImpl implements FindTopValues {
  92.  
  93. /**
  94. * Finds list of the highest 'n' values in the source list, ordered naturally,
  95. * with the highest value at the start of the array and returns it
  96. */
  97. @Override
  98. public int[] findTopNValues(int[] values, int n) {
  99.  
  100. int length = values.length;
  101. for (int i=1; i<length; i++) {
  102. int curPos = i;
  103. while ((curPos > 0) && (values[i] > values[curPos-1])) {
  104. curPos--;
  105. }
  106.  
  107. if (curPos != i) {
  108. int element = values[i];
  109. System.arraycopy(values, curPos, values, curPos+1, (i-curPos));
  110. values[curPos] = element;
  111. }
  112. }
  113.  
  114. return Arrays.copyOf(values, n);
  115. }
  116.  
  117. }
  118.  
  119. public class FindTopValuesSelectionSortImpl implements FindTopValues {
  120.  
  121. /**
  122. * Finds list of the highest 'n' values in the source list, ordered naturally,
  123. * with the highest value at the start of the array and returns it
  124. */
  125. @Override
  126. public int[] findTopNValues(int[] values, int n) {
  127. int length = values.length;
  128.  
  129. for (int i=0; i<=n; i++) {
  130. int maxPos = i;
  131. for (int j=i+1; j<length; j++) {
  132. if (values[j] > values[maxPos]) {
  133. maxPos = j;
  134. }
  135. }
  136.  
  137. if (maxPos != i) {
  138. int maxValue = values[maxPos];
  139. values[maxPos] = values[i];
  140. values[i] = maxValue;
  141. }
  142. }
  143. return Arrays.copyOf(values, n);
  144. }
  145. }
  146.  
  147. import java.util.Comparator;
  148. import java.util.List;
  149. import java.util.stream.Collector;
  150.  
  151. import org.junit.Test;
  152.  
  153. import com.google.common.collect.Comparators;
  154. import com.google.common.collect.Lists;
  155.  
  156. public class TestComparator {
  157.  
  158. @Test
  159. public void testTopN() {
  160. final List<Integer> numbers = Lists.newArrayList(1, 3, 8, 2, 6, 4, 7, 5, 9, 0);
  161. final Collector<Integer, ?, List<Integer>> collector = Comparators.greatest(5,
  162. Comparator.<Integer>naturalOrder());
  163. final List<Integer> top = numbers.stream().collect(collector);
  164. System.out.println(top);
  165. }
  166.  
  167. }
  168.  
  169. public int[] findTopNValues(int[] anyOldOrderValues, int n) {
  170. if (n < 0) {
  171. return new int[]{};
  172. }
  173. if (n == 1) {
  174. return new int[]{findMaxValue(anyOldOrderValues)};
  175. }
  176.  
  177. int[] result = new int[n + 1];
  178. for (int i = 0; i < Math.min(n, anyOldOrderValues.length); i++) {
  179. result[i] = anyOldOrderValues[i];
  180. }
  181. Arrays.sort(result);
  182.  
  183. int max = result[0];
  184. for (int i = n - 1; i < anyOldOrderValues.length; i++) {
  185. int value = anyOldOrderValues[i];
  186. if (max < value) {
  187. result[n] = value;
  188. Arrays.sort(result);
  189. int[] result1 = new int[n + 1];
  190. System.arraycopy(result, 1, result1, 0, n);
  191. result = result1;
  192. max = result[0];
  193. }
  194. }
  195. return convertAndFlip(result, n);
  196. }
  197.  
  198. public static int[] convertAndFlip(int[] integers, int n) {
  199. int[] result = new int[n];
  200. int j = 0;
  201. for (int i = n - 1; i > -1; i--) {
  202. result[j++] = integers[i];
  203. }
  204. return result;
  205. }
  206.  
  207. public void testFindTopNValues() throws Exception {
  208. final int N = 100000000;
  209. final int MAX_VALUE = 100000000;
  210. final int returnArray = 1000;
  211. final int repeatTimes = 5;
  212.  
  213. FindTopValuesArraySorting arraySorting = new FindTopValuesArraySorting();
  214.  
  215. int[] randomArray = createRandomArray(N, MAX_VALUE);
  216. for (int i = 0; i < repeatTimes; i++) {
  217.  
  218. long start = System.currentTimeMillis();
  219. int[] topNValues = arraySorting.findTopNValues(randomArray, returnArray);
  220. long stop = System.currentTimeMillis();
  221.  
  222. System.out.println("findTopNValues() from " + N + " elements, where MAX value=" + (MAX_VALUE - 1) + " and return array size " + returnArray + " elements : " + (stop - start) + "msec");
  223. // System.out.println("Result list = " + Arrays.toString(topNValues));
  224. }
  225. }
  226.  
  227. private static int[] createRandomArray(int n, int maxValue) {
  228. Random r = new Random();
  229. int[] arr = new int[n];
  230. for (int i = 0; i < n; i++) {
  231. arr[i] = r.nextInt(maxValue);
  232. }
  233. return arr;
  234. }
  235.  
  236. findTopNValues() from 100000000 elements, where MAX value=99999999 and return array size 1000 elements : 395msec
  237. findTopNValues() from 100000000 elements, where MAX value=99999999 and return array size 1000 elements : 311msec
  238. findTopNValues() from 100000000 elements, where MAX value=99999999 and return array size 1000 elements : 473msec
  239. findTopNValues() from 100000000 elements, where MAX value=99999999 and return array size 1000 elements : 380msec
  240. findTopNValues() from 100000000 elements, where MAX value=99999999 and return array size 1000 elements : 406msec
  241.  
  242. findTopNValues() from 101 elements and return array size 10 elements : 1msec
  243. Result list = [998, 986, 986, 986, 947, 944, 926, 924, 921, 902]
  244. Original list = [403, 459, 646, 467, 120, 346, 430, 247, 68, 312, 701, 304, 707, 443, 753, 433, 986, 921, 513, 634, 861, 741, 482, 794, 679, 409, 145, 93, 512, 947, 19, 9, 385, 208, 795, 742, 851, 638, 924, 637, 638, 141, 382, 89, 998, 713, 210, 732, 784, 67, 273, 628, 187, 902, 42, 25, 747, 471, 686, 504, 255, 74, 638, 610, 227, 892, 156, 86, 48, 133, 63, 234, 639, 899, 815, 986, 750, 177, 413, 581, 899, 494, 292, 359, 60, 106, 944, 926, 257, 370, 310, 726, 393, 800, 986, 827, 856, 835, 66, 183, 901]
  245.  
  246. public class FindTopValuesSelectionSortImpl implements FindTopValues {
  247.  
  248. /**
  249. * Finds list of the highest 'n' values in the source list, ordered naturally,
  250. * with the highest value at the start of the array and returns it
  251. */
  252. @Override
  253. public int[] findTopNValues(int[] values, int n) {
  254. int length = values.length;
  255.  
  256. for (int i=0; i<=n; i++) {
  257. int maxPos = i;
  258. for (int j=i+1; j<length; j++) {
  259. if (values[j] > values[maxPos]) {
  260. maxPos = j;
  261. }
  262. }
  263.  
  264. if (maxPos != i) {
  265. int maxValue = values[maxPos];
  266. values[maxPos] = values[i];**strong text**
  267. values[i] = maxValue;
  268. }
  269. }
  270. return Arrays.copyOf(values, n);
  271. }
  272. }
  273.  
  274. import java.util.Arrays;
  275. import java.util.PriorityQueue;
  276. import java.util.Random;
  277. import java.util.stream.IntStream;
  278.  
  279. public class UpToNLargestNumbersFromArray {
  280.  
  281. public static int[] upToNLargestNumbersFromArray(int[] arr, int N) {
  282. PriorityQueue<Integer> pq = new PriorityQueue<>();
  283. for (int num : arr) {
  284. pq.offer(num);
  285. if (pq.size() > N) {
  286. pq.poll();
  287. }
  288. }
  289. int[] result = new int[pq.size()];
  290. int i = 0;
  291. while (!pq.isEmpty()) {
  292. result[i] = pq.remove();
  293. i++;
  294. }
  295. return result;
  296. }
  297.  
  298. public static void main(String[] args) {
  299. int[] example10randomIntsArray = IntStream.generate(() -> new Random().nextInt(1000)).limit(10).toArray();
  300. System.out.println("example10randomIntsArray: " + Arrays.toString(example10randomIntsArray));
  301. // Example usage where N <= the number of elements in the array
  302. int[] upTo3largestFromArray = upToNLargestNumbersFromArray(example10randomIntsArray, 3);
  303. System.out.println("upTo3largestFromArray: " + Arrays.toString(upTo3largestFromArray));
  304. // Example usage where N > the number of elements in the array
  305. int[] upTo15largestFromArray = upToNLargestNumbersFromArray(example10randomIntsArray, 15);
  306. System.out.println("upTo15largestFromArray: " + Arrays.toString(upTo15largestFromArray));
  307. }
  308.  
  309. }
  310.  
  311. example10randomIntsArray: [289, 571, 531, 285, 319, 336, 974, 600, 749, 175]
  312. upTo3largestFromArray: [600, 749, 974]
  313. upTo15largestFromArray: [175, 285, 289, 319, 336, 531, 571, 600, 749, 974]
Add Comment
Please, Sign In to add comment