施工阶段4:选择排序

This commit is contained in:
_Karasu_ 2022-08-14 12:34:14 +08:00
parent c1533114d4
commit 8ee64b0fa9
5 changed files with 45 additions and 20 deletions

3
.gitignore vendored
View File

@ -2,4 +2,5 @@ bin/
obj/
/packages/
riderModule.iml
/_ReSharper.Caches/
/_ReSharper.Caches/
.fake

View File

@ -1,17 +1,15 @@
<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>
<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>

View File

@ -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] // xselem
| y::ys -> // xsxsyys
match elem < y with // elemy
| true -> [x; elem; y] @ ys // 若x < elem < yxy
| 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 //

View File

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

View File

@ -37,6 +37,9 @@ let myList = [
512
652
111
233
233
233
851
98
277