施工阶段2:归并排序
This commit is contained in:
parent
b8308cff5d
commit
c9dc0930ed
|
@ -7,7 +7,13 @@
|
||||||
<option name="autoReloadType" value="SELECTIVE" />
|
<option name="autoReloadType" value="SELECTIVE" />
|
||||||
</component>
|
</component>
|
||||||
<component name="ChangeListManager">
|
<component name="ChangeListManager">
|
||||||
<list default="true" id="32b2067c-6667-4162-834b-afaa5fbc533d" name="Changes" comment="" />
|
<list default="true" id="32b2067c-6667-4162-834b-afaa5fbc533d" name="Changes" comment="">
|
||||||
|
<change afterPath="$PROJECT_DIR$/Algorithm-FSharp/MergeSort.fs" afterDir="false" />
|
||||||
|
<change afterPath="$PROJECT_DIR$/Algorithm-FSharp/TestList.fs" afterDir="false" />
|
||||||
|
<change beforePath="$PROJECT_DIR$/.idea/.idea.Algorithm-FSharp/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/.idea.Algorithm-FSharp/.idea/workspace.xml" afterDir="false" />
|
||||||
|
<change beforePath="$PROJECT_DIR$/Algorithm-FSharp/Algorithm-FSharp.fsproj" beforeDir="false" afterPath="$PROJECT_DIR$/Algorithm-FSharp/Algorithm-FSharp.fsproj" afterDir="false" />
|
||||||
|
<change beforePath="$PROJECT_DIR$/Algorithm-FSharp/Program.fs" beforeDir="false" afterPath="$PROJECT_DIR$/Algorithm-FSharp/Program.fs" afterDir="false" />
|
||||||
|
</list>
|
||||||
<option name="SHOW_DIALOG" value="false" />
|
<option name="SHOW_DIALOG" value="false" />
|
||||||
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
||||||
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
|
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
|
||||||
|
@ -24,6 +30,12 @@
|
||||||
</option>
|
</option>
|
||||||
<option name="SET_USER_NAME_GLOBALLY" value="false" />
|
<option name="SET_USER_NAME_GLOBALLY" value="false" />
|
||||||
</component>
|
</component>
|
||||||
|
<component name="HighlightingSettingsPerFile">
|
||||||
|
<setting file="file://$APPLICATION_CONFIG_DIR$/resharper-host/DecompilerCache/decompiler/1a6d778498705eb358dcc13260cac0712f0088/78/86f0e77b/ListModule.cs" root0="SKIP_HIGHLIGHTING" />
|
||||||
|
<setting file="file://$APPLICATION_CONFIG_DIR$/resharper-host/DecompilerCache/decompiler/1a6d778498705eb358dcc13260cac0712f0088/7d/a1c780dd/FSharpList`1.cs" root0="SKIP_HIGHLIGHTING" />
|
||||||
|
<setting file="file://$APPLICATION_CONFIG_DIR$/resharper-host/DecompilerCache/decompiler/1a6d778498705eb358dcc13260cac0712f0088/b1/c3821aa7/Operators.cs" root0="SKIP_HIGHLIGHTING" />
|
||||||
|
<setting file="file://$APPLICATION_CONFIG_DIR$/resharper-host/DecompilerCache/decompiler/1ef8b93e10614b3ea02536efff511b2f9ec400/72/34c20450/Tuple`2.cs" root0="SKIP_HIGHLIGHTING" />
|
||||||
|
</component>
|
||||||
<component name="MarkdownSettingsMigration">
|
<component name="MarkdownSettingsMigration">
|
||||||
<option name="stateVersion" value="1" />
|
<option name="stateVersion" value="1" />
|
||||||
</component>
|
</component>
|
||||||
|
@ -79,6 +91,12 @@
|
||||||
<workItem from="1660053666119" duration="717000" />
|
<workItem from="1660053666119" duration="717000" />
|
||||||
<workItem from="1660054562587" duration="1032000" />
|
<workItem from="1660054562587" duration="1032000" />
|
||||||
<workItem from="1660055708455" duration="4008000" />
|
<workItem from="1660055708455" duration="4008000" />
|
||||||
|
<workItem from="1660059846802" duration="148000" />
|
||||||
|
<workItem from="1660066411792" duration="599000" />
|
||||||
|
<workItem from="1660090605139" duration="1108000" />
|
||||||
|
<workItem from="1660136010329" duration="3458000" />
|
||||||
|
<workItem from="1660181422024" duration="2137000" />
|
||||||
|
<workItem from="1660186635954" duration="838000" />
|
||||||
</task>
|
</task>
|
||||||
<task id="LOCAL-00001" summary="施工阶段1 快速排序">
|
<task id="LOCAL-00001" summary="施工阶段1 快速排序">
|
||||||
<created>1660059459924</created>
|
<created>1660059459924</created>
|
||||||
|
@ -87,7 +105,21 @@
|
||||||
<option name="project" value="LOCAL" />
|
<option name="project" value="LOCAL" />
|
||||||
<updated>1660059459924</updated>
|
<updated>1660059459924</updated>
|
||||||
</task>
|
</task>
|
||||||
<option name="localTasksCounter" value="2" />
|
<task id="LOCAL-00002" summary="施工阶段1:快速排序">
|
||||||
|
<created>1660059889588</created>
|
||||||
|
<option name="number" value="00002" />
|
||||||
|
<option name="presentableId" value="LOCAL-00002" />
|
||||||
|
<option name="project" value="LOCAL" />
|
||||||
|
<updated>1660059889588</updated>
|
||||||
|
</task>
|
||||||
|
<task id="LOCAL-00003" summary="真的只是一点小问题!">
|
||||||
|
<created>1660059986664</created>
|
||||||
|
<option name="number" value="00003" />
|
||||||
|
<option name="presentableId" value="LOCAL-00003" />
|
||||||
|
<option name="project" value="LOCAL" />
|
||||||
|
<updated>1660059986664</updated>
|
||||||
|
</task>
|
||||||
|
<option name="localTasksCounter" value="4" />
|
||||||
<servers />
|
<servers />
|
||||||
</component>
|
</component>
|
||||||
<component name="TypeScriptGeneratedFilesManager">
|
<component name="TypeScriptGeneratedFilesManager">
|
||||||
|
@ -109,8 +141,9 @@
|
||||||
<component name="VcsManagerConfiguration">
|
<component name="VcsManagerConfiguration">
|
||||||
<option name="CLEAR_INITIAL_COMMIT_MESSAGE" value="true" />
|
<option name="CLEAR_INITIAL_COMMIT_MESSAGE" value="true" />
|
||||||
<MESSAGE value="施工阶段1" />
|
<MESSAGE value="施工阶段1" />
|
||||||
<MESSAGE value="施工阶段1:快速排序" />
|
|
||||||
<MESSAGE value="施工阶段1 快速排序" />
|
<MESSAGE value="施工阶段1 快速排序" />
|
||||||
<option name="LAST_COMMIT_MESSAGE" value="施工阶段1 快速排序" />
|
<MESSAGE value="施工阶段1:快速排序" />
|
||||||
|
<MESSAGE value="真的只是一点小问题!" />
|
||||||
|
<option name="LAST_COMMIT_MESSAGE" value="真的只是一点小问题!" />
|
||||||
</component>
|
</component>
|
||||||
</project>
|
</project>
|
|
@ -7,6 +7,8 @@
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<Compile Include="TestList.fs" />
|
||||||
|
<Compile Include="MergeSort.fs" />
|
||||||
<Compile Include="QuickSort.fs" />
|
<Compile Include="QuickSort.fs" />
|
||||||
<Compile Include="Program.fs" />
|
<Compile Include="Program.fs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
|
@ -0,0 +1,26 @@
|
||||||
|
module Algorithm_FSharp.MergeSort
|
||||||
|
|
||||||
|
// 合并有序数列算法
|
||||||
|
// merge : 'a list -> 'a list -> 'a list -> 'a list
|
||||||
|
// 待合并的有序数列存储在lhs和rhs中,每一步合并后中间结果都存放在res中
|
||||||
|
let rec merge lhs rhs res =
|
||||||
|
match (lhs, rhs) with // 判断lhs和rhs是否为空
|
||||||
|
| [], [] -> res // 若两者均为空,直接返回res
|
||||||
|
| [], ys -> res @ ys // 若lhs中的数已经用光,则将rhs追加到res末尾并返回
|
||||||
|
| xs, [] -> res @ xs // 若rhs中的数已经用光,则将lhs追加到res末尾并返回
|
||||||
|
| x::xs, y::ys -> // 若二者均不为空
|
||||||
|
match x < y with // 取出第一个元素,并判断二者的大小关系
|
||||||
|
| true -> merge xs (y::ys) (res @ [x]) // 若lhs的第一个元素小于rhs的第一个元素,则将其取出并放置在res的末尾,递归合并剩余的元素
|
||||||
|
| false -> merge (x::xs) ys (res @ [y]) // 若rhs的第一个元素小于lhs的第一个元素,则将其取出并放置在res的末尾,递归合并剩余的元素
|
||||||
|
|
||||||
|
// 归并排序算法的F#实现
|
||||||
|
// mergesort : 'a list -> 'a list
|
||||||
|
let rec mergesort arr =
|
||||||
|
match arr with // 模式匹配
|
||||||
|
| [] -> [] // 若arr为空或只有一个元素,则它已经有序,直接返回
|
||||||
|
| [single] -> [single]
|
||||||
|
| _ -> // 若arr的元素多于一个
|
||||||
|
let mid = arr.Length / 2 // 计算序列中点的索引
|
||||||
|
let left, right = arr // 从中点切分序列为两部分
|
||||||
|
|> List.splitAt mid
|
||||||
|
merge (mergesort left) (mergesort right) [] // 将两部分分别归并排序,然后合并为有序数列
|
|
@ -1,8 +1,8 @@
|
||||||
// For more information see https://aka.ms/fsharp-console-apps
|
// For more information see https://aka.ms/fsharp-console-apps
|
||||||
module Algorithm_FSharp.Program
|
module Algorithm_FSharp.Program
|
||||||
open Algorithm_FSharp.QuickSort
|
open Algorithm_FSharp.MergeSort
|
||||||
|
open Algorithm_FSharp.TestList
|
||||||
|
|
||||||
// 测试用例
|
// 测试用例
|
||||||
let myList = [1; 1; 4; 5; 1; 4; 1; 9; 1; 9; 8; 1; 0] // 你是一个一个一个快速排序算法啊啊啊啊啊啊
|
|
||||||
printfn $"%A{myList
|
printfn $"%A{myList
|
||||||
|> quicksort}"
|
|> mergesort}"
|
||||||
|
|
|
@ -0,0 +1,104 @@
|
||||||
|
module Algorithm_FSharp.TestList
|
||||||
|
// 测试用数据集
|
||||||
|
let myList = [
|
||||||
|
371
|
||||||
|
42
|
||||||
|
583
|
||||||
|
67
|
||||||
|
201
|
||||||
|
740
|
||||||
|
468
|
||||||
|
424
|
||||||
|
853
|
||||||
|
539
|
||||||
|
158
|
||||||
|
982
|
||||||
|
178
|
||||||
|
644
|
||||||
|
585
|
||||||
|
152
|
||||||
|
820
|
||||||
|
719
|
||||||
|
994
|
||||||
|
43
|
||||||
|
368
|
||||||
|
378
|
||||||
|
95
|
||||||
|
685
|
||||||
|
609
|
||||||
|
153
|
||||||
|
841
|
||||||
|
466
|
||||||
|
214
|
||||||
|
432
|
||||||
|
809
|
||||||
|
499
|
||||||
|
66
|
||||||
|
512
|
||||||
|
652
|
||||||
|
111
|
||||||
|
851
|
||||||
|
98
|
||||||
|
277
|
||||||
|
223
|
||||||
|
922
|
||||||
|
533
|
||||||
|
969
|
||||||
|
407
|
||||||
|
694
|
||||||
|
32
|
||||||
|
36
|
||||||
|
242
|
||||||
|
491
|
||||||
|
99
|
||||||
|
987
|
||||||
|
519
|
||||||
|
336
|
||||||
|
1000
|
||||||
|
359
|
||||||
|
930
|
||||||
|
892
|
||||||
|
833
|
||||||
|
417
|
||||||
|
981
|
||||||
|
827
|
||||||
|
945
|
||||||
|
195
|
||||||
|
389
|
||||||
|
664
|
||||||
|
601
|
||||||
|
864
|
||||||
|
607
|
||||||
|
911
|
||||||
|
445
|
||||||
|
252
|
||||||
|
681
|
||||||
|
413
|
||||||
|
228
|
||||||
|
77
|
||||||
|
758
|
||||||
|
799
|
||||||
|
620
|
||||||
|
898
|
||||||
|
545
|
||||||
|
110
|
||||||
|
560
|
||||||
|
883
|
||||||
|
107
|
||||||
|
703
|
||||||
|
263
|
||||||
|
148
|
||||||
|
617
|
||||||
|
137
|
||||||
|
628
|
||||||
|
155
|
||||||
|
908
|
||||||
|
598
|
||||||
|
220
|
||||||
|
318
|
||||||
|
877
|
||||||
|
315
|
||||||
|
978
|
||||||
|
645
|
||||||
|
75
|
||||||
|
]
|
Loading…
Reference in New Issue