施工阶段2:归并排序

This commit is contained in:
_Karasu_ 2022-08-11 11:12:04 +08:00
parent b8308cff5d
commit c9dc0930ed
5 changed files with 172 additions and 7 deletions

View File

@ -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>

View File

@ -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>

View File

@ -0,0 +1,26 @@
module Algorithm_FSharp.MergeSort
//
// merge : 'a list -> 'a list -> 'a list -> 'a list
// lhsrhsres
let rec merge lhs rhs res =
match (lhs, rhs) with // lhsrhs
| [], [] -> res // res
| [], ys -> res @ ys // lhsrhsres
| xs, [] -> res @ xs // rhslhsres
| x::xs, y::ys -> //
match x < y with //
| true -> merge xs (y::ys) (res @ [x]) // lhsrhsres
| false -> merge (x::xs) ys (res @ [y]) // rhslhsres
// 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) [] //

View File

@ -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}"

View File

@ -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
]