-
Notifications
You must be signed in to change notification settings - Fork 0
/
QuickSort.fsx
79 lines (73 loc) · 3.01 KB
/
QuickSort.fsx
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
// https://en.wikipedia.org/wiki/Quicksort
let quickSort (listToSort: 'A list) : 'A list =
let pickAndRun list func =
match list with
| [] -> []
| [el] -> [el]
| head::tail -> func head tail
let rec partition before after pivot list =
match list with
| [] ->
let beforeSorted = (partition [] []) |> pickAndRun before
let afterSorted = (partition [] []) |> pickAndRun after
beforeSorted @ (pivot :: afterSorted)
| head::tail ->
if head < pivot
then partition (head::before) after pivot tail
else partition before (head::after) pivot tail
(partition [] []) |> pickAndRun listToSort
/// async
// let quickSort2 (listToSort: 'A list) : 'A list =
// let pickAndRun list func =
// match list with
// | [] -> []
// | [el] -> [el]
// | head::tail -> func head tail
// let rec partition before after pivot list =
// match list with
// | [] ->
// let beforeLength = before |> List.length
// let afterLength = after |> List.length
// let [| beforeSorted; afterSorted |] =
// [ before; after ]
// |> List.map (fun list -> async { return (partition [] []) |> pickAndRun list })
// // |> Async.Parallel
// |> (fun asyncs ->
// asyncs
// |> if beforeLength > 100 && afterLength > 100
// then Async.Parallel
// else Async.Sequential)
// |> Async.RunSynchronously
// beforeSorted @ (pivot :: afterSorted)
// | head::tail ->
// if head < pivot
// then partition (head::before) after pivot tail
// else partition before (head::after) pivot tail
// (partition [] []) |> pickAndRun listToSort
let quickSort2 (listToSort: 'A list) : 'A list =
let pickAndRun func list =
match list with
| [] -> []
| [el] -> [el]
| head::tail -> func head tail
let rec partition before after pivot list =
match list with
| [] ->
let beforeLength = before |> List.length
let afterLength = after |> List.length
let pickAndRun = pickAndRun (partition [] [])
let [| beforeSorted; afterSorted |] =
[| before; after |]
|> if beforeLength > 30000 && afterLength > 30000
then
Array.map (fun list -> async { return pickAndRun list})
>> Async.Parallel
>> Async.RunSynchronously
else
Array.map pickAndRun
beforeSorted @ (pivot :: afterSorted)
| head::tail ->
if head < pivot
then partition (head::before) after pivot tail
else partition before (head::after) pivot tail
listToSort |> pickAndRun (partition [] [])