MCPcopy Create free account
hub / github.com/careercup/ctci / pickMRandomly

Method pickMRandomly

java/Chapter 18/Question18_3/Question.java:14–22  ·  view source on GitHub ↗
(int[] original, int m)

Source from the content-addressed store, hash-verified

12 /* pick M elements from original array. Clone original array so that
13 * we don�t destroy the input. */
14 public static int[] pickMRandomly(int[] original, int m) {
15 for (int i = 0; i < original.length; i++) {
16 int k = rand(0, i);
17 int temp = original[k];
18 original[k] = original[i];
19 original[i] = temp;
20 }
21 return original;
22 }
23
24 public static void main(String[] args) {
25 int[] cards = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

Callers 1

mainMethod · 0.95

Calls 1

randMethod · 0.95

Tested by

no test coverage detected