施工阶段4:插入排序
This commit is contained in:
parent
50961ca46e
commit
dbd410feb8
|
@ -7,10 +7,11 @@
|
|||
<option name="autoReloadType" value="SELECTIVE" />
|
||||
</component>
|
||||
<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="施工阶段4:插入排序">
|
||||
<change afterPath="$PROJECT_DIR$/Algorithm-FSharp/IntersectionSort.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/TestList.fs" beforeDir="false" afterPath="$PROJECT_DIR$/Algorithm-FSharp/TestList.fs" afterDir="false" />
|
||||
</list>
|
||||
<option name="SHOW_DIALOG" value="false" />
|
||||
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
||||
|
@ -98,6 +99,7 @@
|
|||
<workItem from="1660231505344" duration="3491000" />
|
||||
<workItem from="1660267756436" duration="7666000" />
|
||||
<workItem from="1660313412199" duration="3066000" />
|
||||
<workItem from="1660475772512" duration="451000" />
|
||||
</task>
|
||||
<task id="LOCAL-00001" summary="施工阶段1 快速排序">
|
||||
<created>1660059459924</created>
|
||||
|
@ -141,7 +143,14 @@
|
|||
<option name="project" value="LOCAL" />
|
||||
<updated>1660279013996</updated>
|
||||
</task>
|
||||
<option name="localTasksCounter" value="7" />
|
||||
<task id="LOCAL-00007" summary="施工阶段4:插入排序">
|
||||
<created>1660475901956</created>
|
||||
<option name="number" value="00007" />
|
||||
<option name="presentableId" value="LOCAL-00007" />
|
||||
<option name="project" value="LOCAL" />
|
||||
<updated>1660475901956</updated>
|
||||
</task>
|
||||
<option name="localTasksCounter" value="8" />
|
||||
<servers />
|
||||
</component>
|
||||
<component name="TypeScriptGeneratedFilesManager">
|
||||
|
@ -169,6 +178,7 @@
|
|||
<MESSAGE value="施工阶段2:归并排序" />
|
||||
<MESSAGE value="施工阶段3:选择排序" />
|
||||
<MESSAGE value="修正冗余语法,对应修改了相关注释" />
|
||||
<option name="LAST_COMMIT_MESSAGE" value="修正冗余语法,对应修改了相关注释" />
|
||||
<MESSAGE value="施工阶段4:插入排序" />
|
||||
<option name="LAST_COMMIT_MESSAGE" value="施工阶段4:插入排序" />
|
||||
</component>
|
||||
</project>
|
|
@ -0,0 +1,2 @@
|
|||
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
|
||||
<s:String x:Key="/Default/CodeInspection/PencilsConfiguration/ActualSeverity/@EntryValue">INFO</s:String></wpf:ResourceDictionary>
|
|
@ -8,6 +8,7 @@
|
|||
|
||||
<ItemGroup>
|
||||
<Compile Include="TestList.fs" />
|
||||
<Compile Include="IntersectionSort.fs" />
|
||||
<Compile Include="SelectionSort.fs" />
|
||||
<Compile Include="MergeSort.fs" />
|
||||
<Compile Include="QuickSort.fs" />
|
||||
|
|
|
@ -0,0 +1,23 @@
|
|||
module Algorithm_FSharp.IntersectionSort
|
||||
// 将一个元素插入到有序序列中
|
||||
// intersect : 'a -> 'a list -> 'a list
|
||||
let rec intersect elem = function
|
||||
| [] -> [elem] // 若序列为空,则无需比较,直接将元素插入到空序列中
|
||||
| x::xs -> // 若序列不为空
|
||||
match x < elem with // 取出序列中的第一个元素x,并将其与待插入元素比较
|
||||
| true -> // 若x小于待插入元素
|
||||
match xs with // 判断xs是否为空
|
||||
| [] -> [elem; x] // 若xs为空,则直接插入到elem后部即可
|
||||
| y::ys -> // 若xs不为空,则切割xs为有序序列的第二个元素y和剩余元素ys
|
||||
match elem < y with // 再比较elem和y
|
||||
| true -> [x; elem; y] @ ys // 若x < elem < y,则插入位点位于x、y之间
|
||||
| false -> x::(xs |> intersect elem) // 若elem > y,则插入位点无法决议,递归插入
|
||||
| false -> elem::x::xs // 若待插入元素小于序列中第一个元素,则直接插入到序列头部即可
|
||||
// 插入排序的F#实现
|
||||
// intersection_sort : 'a list -> 'a list
|
||||
let rec intersection_sort = function
|
||||
| [] -> [] // 若待排序序列为空或只有一个元素,则它已经有序,直接返回
|
||||
| [single] -> [single]
|
||||
| x::xs -> xs // 若待排序序列有多个元素,则取出第一个元素
|
||||
|> intersection_sort // 递归对后续序列进行排序
|
||||
|> intersect x // 然后将第一个元素插入到序列中
|
|
@ -37,6 +37,9 @@ let myList = [
|
|||
512
|
||||
652
|
||||
111
|
||||
233
|
||||
233
|
||||
233
|
||||
851
|
||||
98
|
||||
277
|
||||
|
|
Loading…
Reference in New Issue