-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbubble.ml
89 lines (86 loc) · 1.63 KB
/
bubble.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
let print_list l = (
Printf.printf "[";
let rec inner input = (
match input with
| [hd] -> (
Printf.printf "%d]\n" hd
)
| hd :: tl -> (
Printf.printf "%d, " hd;
inner tl
)
| [] -> (
Printf.printf "]\n"
)
) in
inner l
)
let bubble_sort l = (
let rec valid input = (
match input with
| a :: b :: tl -> (
if a > b then
false
else
valid (b :: tl)
)
| _ -> true
) in
let rec bubble input output = (
match input with
| a :: b :: tl when a > b -> (
bubble (a :: tl) (b :: output)
)
| a :: b :: tl -> (
bubble (b :: tl) (a :: output)
)
| [a] -> (
List.rev (a :: output)
)
| _ -> List.rev output
) in
let rec loop input = (
let output = bubble input [] in
print_list output;
if valid output then
output
else
loop output
) in
if List.length l > 0 then (
loop l
)
else (
l
)
)
let parse_input s = (
let rec inner input output = (
if input < 0 then (
output
)
else (
let c = s.[input] in
if c >= '0' && c <= '9' then (
((Char.code c) - (Char.code '0')) :: output
)
else (
inner (input - 1) output
)
)
) in
inner ((String.length s) - 1) []
)
let () = (
print_endline "Please enter a sequence of numbers to be sorted (enter a non-number character to stop input)";
print_endline "Example: 1 2 3 x";
let rec parse output = (
try (
Scanf.scanf " %d " (fun d -> parse (d :: output))
) with exn -> output
) in
parse []
|> List.rev
|> bubble_sort
|> print_list
)