施工阶段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/ obj/
/packages/ /packages/
riderModule.iml riderModule.iml
/_ReSharper.Caches/ /_ReSharper.Caches/
.fake

View File

@ -1,17 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<PropertyGroup> <OutputType>Exe</OutputType>
<OutputType>Exe</OutputType> <TargetFramework>net6.0</TargetFramework>
<TargetFramework>net6.0</TargetFramework> <RootNamespace>Algorithm_FSharp</RootNamespace>
<RootNamespace>Algorithm_FSharp</RootNamespace> </PropertyGroup>
</PropertyGroup> <ItemGroup>
<Compile Include="IntersectionSort.fs" />
<ItemGroup> <Compile Include="TestList.fs" />
<Compile Include="TestList.fs" /> <Compile Include="SelectionSort.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" /> </ItemGroup>
</ItemGroup> </Project>
</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 // For more information see https://aka.ms/fsharp-console-apps
module Algorithm_FSharp.Program module Algorithm_FSharp.Program
open Algorithm_FSharp.QuickSort open Algorithm_FSharp.IntersectionSort
open Algorithm_FSharp.TestList open Algorithm_FSharp.TestList
// //
printfn $"%A{myList printfn $"%A{myList
|> quicksort}" |> intersection_sort}"

View File

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