From 8ee64b0fa9d04e47aaaf3af05a29d0dfff2cdf09 Mon Sep 17 00:00:00 2001 From: lucas8485 <1443937075@qq.com> Date: Sun, 14 Aug 2022 12:34:14 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=BD=E5=B7=A5=E9=98=B6=E6=AE=B54=EF=BC=9A?= =?UTF-8?q?=E9=80=89=E6=8B=A9=E6=8E=92=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 ++- Algorithm-FSharp/Algorithm-FSharp.fsproj | 32 +++++++++++------------- Algorithm-FSharp/IntersectionSort.fs | 23 +++++++++++++++++ Algorithm-FSharp/Program.fs | 4 +-- Algorithm-FSharp/TestList.fs | 3 +++ 5 files changed, 45 insertions(+), 20 deletions(-) create mode 100644 Algorithm-FSharp/IntersectionSort.fs diff --git a/.gitignore b/.gitignore index add57be..7c4f36a 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ bin/ obj/ /packages/ riderModule.iml -/_ReSharper.Caches/ \ No newline at end of file +/_ReSharper.Caches/ +.fake \ No newline at end of file diff --git a/Algorithm-FSharp/Algorithm-FSharp.fsproj b/Algorithm-FSharp/Algorithm-FSharp.fsproj index fe7fd8a..a8cb50f 100644 --- a/Algorithm-FSharp/Algorithm-FSharp.fsproj +++ b/Algorithm-FSharp/Algorithm-FSharp.fsproj @@ -1,17 +1,15 @@ - - - - Exe - net6.0 - Algorithm_FSharp - - - - - - - - - - - + + + Exe + net6.0 + Algorithm_FSharp + + + + + + + + + + \ No newline at end of file diff --git a/Algorithm-FSharp/IntersectionSort.fs b/Algorithm-FSharp/IntersectionSort.fs new file mode 100644 index 0000000..28e1b8f --- /dev/null +++ b/Algorithm-FSharp/IntersectionSort.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 // 然后将第一个元素插入到序列中 \ No newline at end of file diff --git a/Algorithm-FSharp/Program.fs b/Algorithm-FSharp/Program.fs index 932d55d..6ba9a3d 100644 --- a/Algorithm-FSharp/Program.fs +++ b/Algorithm-FSharp/Program.fs @@ -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}" diff --git a/Algorithm-FSharp/TestList.fs b/Algorithm-FSharp/TestList.fs index 9416c1a..8f4d968 100644 --- a/Algorithm-FSharp/TestList.fs +++ b/Algorithm-FSharp/TestList.fs @@ -37,6 +37,9 @@ let myList = [ 512 652 111 + 233 + 233 + 233 851 98 277