Advertisement
joharido

Untitled

Apr 3rd, 2019
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.65 KB | None | 0 0
  1. /* this algorithm takes both arrays and looks for the kth element.
  2. pre-condition: S and T are arrays with the same size and contain integers.
  3. post-condition: the kth element is returned. */
  4. Algorithm input(int S[], int indexS, int T[], int indexT, int k)
  5. if (k = 2)
  6. result <- pick the second smallest element between S[indexS], S[indexS + 1], T[indexT] and T[indexT + 1]
  7. return result;
  8. end IF
  9. int medianOfS <- S[indexS + floor(k/2)];
  10. int medianOfT <- T[indexT + floor(k/2)];
  11. if (medianOfS < medianOfT)
  12.     return input(S, indexS + floor(k/2), T, indexT, ceil(k/2));
  13. end IF
  14. else return input(S, indexS, T, indexT + floor(k/2), ceil(k/2));
  15. end ELSE
  16. END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement