parent
8ee64b0fa9
commit
df29cfc676
|
@ -2,5 +2,4 @@ bin/
|
|||
obj/
|
||||
/packages/
|
||||
riderModule.iml
|
||||
/_ReSharper.Caches/
|
||||
.fake
|
||||
/_ReSharper.Caches/
|
|
@ -1,15 +1,17 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<RootNamespace>Algorithm_FSharp</RootNamespace>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="IntersectionSort.fs" />
|
||||
<Compile Include="TestList.fs" />
|
||||
<Compile Include="SelectionSort.fs" />
|
||||
<Compile Include="MergeSort.fs" />
|
||||
<Compile Include="QuickSort.fs" />
|
||||
<Compile Include="Program.fs" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<RootNamespace>Algorithm_FSharp</RootNamespace>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Include="TestList.fs" />
|
||||
<Compile Include="SelectionSort.fs" />
|
||||
<Compile Include="MergeSort.fs" />
|
||||
<Compile Include="QuickSort.fs" />
|
||||
<Compile Include="Program.fs" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
|
|
@ -1,23 +0,0 @@
|
|||
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 // 然后将第一个元素插入到序列中
|
|
@ -1,8 +1,8 @@
|
|||
// For more information see https://aka.ms/fsharp-console-apps
|
||||
module Algorithm_FSharp.Program
|
||||
open Algorithm_FSharp.IntersectionSort
|
||||
open Algorithm_FSharp.QuickSort
|
||||
open Algorithm_FSharp.TestList
|
||||
|
||||
// 测试用例
|
||||
printfn $"%A{myList
|
||||
|> intersection_sort}"
|
||||
|> quicksort}"
|
||||
|
|
|
@ -37,9 +37,6 @@ let myList = [
|
|||
512
|
||||
652
|
||||
111
|
||||
233
|
||||
233
|
||||
233
|
||||
851
|
||||
98
|
||||
277
|
||||
|
|
Loading…
Reference in New Issue