施工阶段3:选择排序
This commit is contained in:
parent
c9dc0930ed
commit
1592f00810
|
@ -8,8 +8,7 @@
|
||||||
</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/SelectionSort.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$/.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/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" />
|
<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="1660090605139" duration="1108000" />
|
||||||
<workItem from="1660136010329" duration="3458000" />
|
<workItem from="1660136010329" duration="3458000" />
|
||||||
<workItem from="1660181422024" duration="2137000" />
|
<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>
|
||||||
<task id="LOCAL-00001" summary="施工阶段1 快速排序">
|
<task id="LOCAL-00001" summary="施工阶段1 快速排序">
|
||||||
<created>1660059459924</created>
|
<created>1660059459924</created>
|
||||||
|
@ -119,7 +120,14 @@
|
||||||
<option name="project" value="LOCAL" />
|
<option name="project" value="LOCAL" />
|
||||||
<updated>1660059986664</updated>
|
<updated>1660059986664</updated>
|
||||||
</task>
|
</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 />
|
<servers />
|
||||||
</component>
|
</component>
|
||||||
<component name="TypeScriptGeneratedFilesManager">
|
<component name="TypeScriptGeneratedFilesManager">
|
||||||
|
@ -144,6 +152,7 @@
|
||||||
<MESSAGE value="施工阶段1 快速排序" />
|
<MESSAGE value="施工阶段1 快速排序" />
|
||||||
<MESSAGE value="施工阶段1:快速排序" />
|
<MESSAGE value="施工阶段1:快速排序" />
|
||||||
<MESSAGE value="真的只是一点小问题!" />
|
<MESSAGE value="真的只是一点小问题!" />
|
||||||
<option name="LAST_COMMIT_MESSAGE" value="真的只是一点小问题!" />
|
<MESSAGE value="施工阶段2:归并排序" />
|
||||||
|
<option name="LAST_COMMIT_MESSAGE" value="施工阶段2:归并排序" />
|
||||||
</component>
|
</component>
|
||||||
</project>
|
</project>
|
|
@ -8,6 +8,7 @@
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="TestList.fs" />
|
<Compile Include="TestList.fs" />
|
||||||
|
<Compile Include="SelectionSort.fs" />
|
||||||
<Compile Include="MergeSort.fs" />
|
<Compile Include="MergeSort.fs" />
|
||||||
<Compile Include="QuickSort.fs" />
|
<Compile Include="QuickSort.fs" />
|
||||||
<Compile Include="Program.fs" />
|
<Compile Include="Program.fs" />
|
||||||
|
|
|
@ -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.MergeSort
|
open Algorithm_FSharp.SelectionSort
|
||||||
open Algorithm_FSharp.TestList
|
open Algorithm_FSharp.TestList
|
||||||
|
|
||||||
// 测试用例
|
// 测试用例
|
||||||
printfn $"%A{myList
|
printfn $"%A{myList
|
||||||
|> mergesort}"
|
|> selection_sort}"
|
||||||
|
|
|
@ -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) // 对剩余部分进行递归排序,将最小值附加在排序结果的首部
|
Loading…
Reference in New Issue