fork download
  1. :- set_prolog_flag(verbose,normal). % see
  2. :- prompt(_, ''). % stackoverflow.com/a/51926948
  3. :- use_module(library(readutil)).
  4.  
  5. % length_of_list - number_of_inferences:
  6. % bubbl(SO): 'true' bubble sort:
  7. % N infs n^ infs n^
  8. % 10 - 111
  9. % 20 - 621 2.5
  10. % 30 - 2,429 3.4
  11. % 40 - 5,698 3.0 1,030
  12. % 120 - 165,296 3.0 11,742 2.2
  13. % 240 - 1,343,303 3.0 49,410 2.1
  14.  
  15. main:-
  16. process,
  17.  
  18. process:-
  19. /* your code goes here */
  20. time( (L=[1,6,2,8,5,3,4,1,5,8,7,9,5,8,7,4,5,6,8,5,
  21. 1,6,2,8,5,3,4,1,5,8,7,9,5,8,7,4,5,6,8,5,
  22. 1,6,2,8,5,3,4,1,5,8,7,9,5,8,7,4,5,6,8,5,
  23. 1,6,2,8,5,3,4,1,5,8,7,9,5,8,7,4,5,6,8,5,
  24. 1,6,2,8,5,3,4,1,5,8,7,9,5,8,7,4,5,6,8,5,
  25. 1,6,2,8,5,3,4,1,5,8,7,9,5,8,7,4,5,6,8,5,
  26. 1,6,2,8,5,3,4,1,5,8,7,9,5,8,7,4,5,6,8,5,
  27. 1,6,2,8,5,3,4,1,5,8,7,9,5,8,7,4,5,6,8,5,
  28. 1,6,2,8,5,3,4,1,5,8,7,9,5,8,7,4,5,6,8,5,
  29. 1,6,2,8,5,3,4,1,5,8,7,9,5,8,7,4,5,6,8,5,
  30. 1,6,2,8,5,3,4,1,5,8,7,9,5,8,7,4,5,6,8,5,
  31. 1,6,2,8,5,3,4,1,5,8,7,9,5,8,7,4,5,6,8,5
  32. ],
  33. % bubbl(L,_),
  34. tbs(L,_),
  35. length(L,N), write(N), nl
  36. ) ),
  37.  
  38. bubbl([], []). % from SO
  39. bubbl([H], [H]).
  40. bubbl([H|T], S) :-
  41. bubbl(T, [M|R]),
  42. ( H =< M, S = [H,M|R]
  43. ; H > M, bubbl([M,H|R], S)
  44. ).
  45.  
  46. % 'true' bubble sort -- by Will Ness
  47. tbs([], []).
  48. tbs([H],[H]).
  49. tbs(L,S):- bubble(L,B),
  50. ( L==B -> S=L
  51. ; tbs(B,S) ).
  52. bubble([],[]).
  53. bubble([A],[A]).
  54. bubble([A,B|C],R):-
  55. ( A =< B -> bubble([B|C],X), R=[A|X]
  56. ; bubble([A|C],X), R=[B|X]
  57. ).
  58.  
  59. :- main.
Success #stdin #stdout #stderr 0.1s 14776KB
stdin
Standard input is empty
stdout
240
stderr
% 49,410 inferences, 0.031 CPU in 0.031 seconds (100% CPU, 1593390 Lips)