Advertisement
Guest User

Untitled

a guest
Jun 16th, 2015
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. let swap tab a b =
  2. if a <> b then
  3. (
  4. let t = tab.(a) in
  5. tab.(a) <- tab.(b);
  6. tab.(b) <- t
  7. )
  8. in
  9. let msort tab a b =
  10. let m = b / 2 in
  11. let li = ref a in
  12. let ri = ref (a + m) in
  13. let s = ref 0 in
  14. for i = 0 to (b - 1) do
  15. if tab.(!li) > tab.(!ri) then
  16. (
  17. swap tab (a + i) (!ri);
  18. ri := !ri + 1;
  19. ) else
  20. (
  21. swap tab (a + i) (!li);
  22. if (!li + 1 < !ri) then
  23. li := !li + 1
  24. else
  25. li := a + i + 1
  26. )
  27. done
  28. in
  29. let rsort tab a b =
  30. if b > 1 then
  31. (
  32. let m = b / 2;
  33. rsort tab a m;
  34. rsort tab (a + m) (b - m);
  35. msort tab a b
  36. )
  37. in
  38. let sort tab =
  39. rsort tab 0 (length tab)
  40. in
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement