施工阶段3:选择排序

This commit is contained in:
_Karasu_ 2022-08-12 12:26:18 +08:00
parent c9dc0930ed
commit 1592f00810
4 changed files with 53 additions and 7 deletions

View File

@ -8,8 +8,7 @@
</component>
<component name="ChangeListManager">
<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 afterPath="$PROJECT_DIR$/Algorithm-FSharp/SelectionSort.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" />
@ -96,7 +95,9 @@
<workItem from="1660090605139" duration="1108000" />
<workItem from="1660136010329" duration="3458000" />
<workItem from="1660181422024" duration="2137000" />
<workItem from="1660186635954" duration="838000" />
<workItem from="1660186635954" duration="1867000" />
<workItem from="1660231505344" duration="3491000" />
<workItem from="1660267756436" duration="6060000" />
</task>
<task id="LOCAL-00001" summary="施工阶段1 快速排序">
<created>1660059459924</created>
@ -119,7 +120,14 @@
<option name="project" value="LOCAL" />
<updated>1660059986664</updated>
</task>
<option name="localTasksCounter" value="4" />
<task id="LOCAL-00004" summary="施工阶段2归并排序">
<created>1660187524761</created>
<option name="number" value="00004" />
<option name="presentableId" value="LOCAL-00004" />
<option name="project" value="LOCAL" />
<updated>1660187524761</updated>
</task>
<option name="localTasksCounter" value="5" />
<servers />
</component>
<component name="TypeScriptGeneratedFilesManager">
@ -144,6 +152,7 @@
<MESSAGE value="施工阶段1 快速排序" />
<MESSAGE value="施工阶段1快速排序" />
<MESSAGE value="真的只是一点小问题!" />
<option name="LAST_COMMIT_MESSAGE" value="真的只是一点小问题!" />
<MESSAGE value="施工阶段2归并排序" />
<option name="LAST_COMMIT_MESSAGE" value="施工阶段2归并排序" />
</component>
</project>

View File

@ -8,6 +8,7 @@
<ItemGroup>
<Compile Include="TestList.fs" />
<Compile Include="SelectionSort.fs" />
<Compile Include="MergeSort.fs" />
<Compile Include="QuickSort.fs" />
<Compile Include="Program.fs" />

View File

@ -1,8 +1,8 @@
// For more information see https://aka.ms/fsharp-console-apps
module Algorithm_FSharp.Program
open Algorithm_FSharp.MergeSort
open Algorithm_FSharp.SelectionSort
open Algorithm_FSharp.TestList
//
printfn $"%A{myList
|> mergesort}"
|> selection_sort}"

View File

@ -0,0 +1,36 @@
module Algorithm_FSharp.SelectionSort
//
// select_min_inner : 'a -> 'a list -> 'a
// cur
let rec select_min_inner cur = function
| [] -> cur // cur
| x::xs -> //
match x < cur with //
| true -> select_min_inner x xs //
| false -> select_min_inner cur xs //
//
// select_min_inner便
// select_min_inner
// select_min : 'T list -> 'T
let select_min (arr: 'T list) = select_min_inner arr.Head arr
//
// remove_elem : ('a -> bool) -> 'a list -> 'a list
// pred
let rec remove_elem pred = function
| [] -> [] //
| x::xs -> //
match pred x with // pred
| true -> x::(remove_elem pred xs) //
| false -> remove_elem pred xs //
// F#
// selection_sort : 'a list -> 'a list
let rec selection_sort = function
| [] -> [] //
| [single] -> [single]
| xs -> //
let min = select_min xs //
let rest = xs //
|> remove_elem (fun x -> x <> min)
min::(selection_sort rest) //