From 1b9a1a6faf5657c5016a53689bce5fb32e00e9c9 Mon Sep 17 00:00:00 2001 From: Sanchime Date: Tue, 5 Jul 2022 20:26:22 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=80=E4=BA=9B=E6=8E=92=E5=BA=8F=E7=AE=97?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 ++ Algrithms/Sorts/BubbleSort.fs | 16 ++++++++++++ Algrithms/Sorts/InsertSort.fs | 14 +++++++++++ Algrithms/Sorts/MergeSort.fs | 47 +++++++++++++++++++++++++++++++++++ Algrithms/Sorts/QuickSort.fs | 11 ++++++++ Algrithms/Sorts/SelectSort.fs | 17 +++++++++++++ Algrithms/Sorts/Test.fs | 24 ++++++++++++++++++ Program.fs | 10 ++++++++ Sanchime.Learn.fsproj | 15 +++++++++++ 9 files changed, 156 insertions(+) create mode 100644 .gitignore create mode 100644 Algrithms/Sorts/BubbleSort.fs create mode 100644 Algrithms/Sorts/InsertSort.fs create mode 100644 Algrithms/Sorts/MergeSort.fs create mode 100644 Algrithms/Sorts/QuickSort.fs create mode 100644 Algrithms/Sorts/SelectSort.fs create mode 100644 Algrithms/Sorts/Test.fs create mode 100644 Program.fs create mode 100644 Sanchime.Learn.fsproj diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0a079b6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/bin +/obj \ No newline at end of file diff --git a/Algrithms/Sorts/BubbleSort.fs b/Algrithms/Sorts/BubbleSort.fs new file mode 100644 index 0000000..b727ec3 --- /dev/null +++ b/Algrithms/Sorts/BubbleSort.fs @@ -0,0 +1,16 @@ +namespace Sanchime.Algrithm.Sort +[] +module Bubble = + + let rec bsort sorted = function + | [] -> [] + | [x] -> [x] + | xs -> + let sxs = xs |> swap sorted + if sxs = xs then xs else sxs |> bsort sorted + // 比较相邻元素,反复交换 + and swap sorted = function + | [] -> [] + | [x] -> [x] + | x::y::xs -> + if sorted x y then x::(y::xs |> swap sorted) else y::(x::xs |> swap sorted) \ No newline at end of file diff --git a/Algrithms/Sorts/InsertSort.fs b/Algrithms/Sorts/InsertSort.fs new file mode 100644 index 0000000..d53dde0 --- /dev/null +++ b/Algrithms/Sorts/InsertSort.fs @@ -0,0 +1,14 @@ +namespace Sanchime.Algrithm.Sort +[] +module Insert = + + let rec isort sorted = function + | [] -> [] + | [x] -> [x] + | x::xs -> xs |> isort sorted |> insert sorted x + + // 将一个元素插入至列表开头 + and insert sorted value = function + | [] -> [value] + | x::xs -> if sorted value x then value::x::xs else x :: insert sorted value xs + \ No newline at end of file diff --git a/Algrithms/Sorts/MergeSort.fs b/Algrithms/Sorts/MergeSort.fs new file mode 100644 index 0000000..ed9634e --- /dev/null +++ b/Algrithms/Sorts/MergeSort.fs @@ -0,0 +1,47 @@ +namespace Sanchime.Algrithm.Sort +[] +module Merge = + + let rec msort sorted = function + | [] -> [] + | [x] -> [x] + | list -> + let evens, odds = divde list + in merge sorted (msort sorted evens, msort sorted odds) + + and merge sorted = function + | xs, [] -> xs + | [], ys -> ys + | x::xs, y::ys -> + if sorted y x then y::(merge sorted (x::xs, ys)) + else x::(merge sorted (xs, y::ys)) + + + and divde = function + | [] -> [], [] + | x::xs -> + let odds, evens = divde xs + in x::evens, odds + + + // let rec msort sorted = function + // | [] -> [] + // | [x] -> [x] + // | xs -> + // let half = xs.Length / 2 + // let left, right = List.splitAt half xs + // merge sorted (msort sorted left) (msort sorted right) + + // and merge (sorted: 'a -> 'a -> bool) left right = + // let rec aux continuation left right = + // match left, right with + // | left, [] -> continuation left + // | [], right -> continuation right + // | x::xs, y::ys -> + // if sorted x y then + // aux (fun acc -> continuation (x::acc)) xs right + // else + // aux (fun acc -> continuation (y::acc)) left ys + // aux id left right + + \ No newline at end of file diff --git a/Algrithms/Sorts/QuickSort.fs b/Algrithms/Sorts/QuickSort.fs new file mode 100644 index 0000000..025f8ab --- /dev/null +++ b/Algrithms/Sorts/QuickSort.fs @@ -0,0 +1,11 @@ +namespace Sanchime.Algrithm.Sort +[] +module Quick = + + let rec qsort sorted = function + | [] -> [] + | [x] -> [x] + | x::xs -> + let smaller,larger = xs |> List.partition (fun i -> sorted i x) + (qsort sorted smaller) @ [x] @ qsort sorted larger + \ No newline at end of file diff --git a/Algrithms/Sorts/SelectSort.fs b/Algrithms/Sorts/SelectSort.fs new file mode 100644 index 0000000..8d99cd1 --- /dev/null +++ b/Algrithms/Sorts/SelectSort.fs @@ -0,0 +1,17 @@ +namespace Sanchime.Algrithm.Sort + +[] +module Select = + + let rec ssort sorted = function + | [] -> [] + | xs -> + let mini = xs |> List.min + let xs' = (mini, xs) |> select sorted + mini::(ssort sorted xs') + // 将最小值移至列表开头 + and select sorted = function + | _, [] -> [] + | x, y::ys -> + if sorted x y then ys + else y::(select sorted (x, ys)) \ No newline at end of file diff --git a/Algrithms/Sorts/Test.fs b/Algrithms/Sorts/Test.fs new file mode 100644 index 0000000..4a1ba4e --- /dev/null +++ b/Algrithms/Sorts/Test.fs @@ -0,0 +1,24 @@ +namespace Sanchime.Algrithm.Sort + +open System.Diagnostics + +module Test = + + + let test sort = + try + let rand = System.Random() in + let list = List.init 10000 (fun _ -> rand.Next(-10000,10000)) + + printfn "原序列:%A" list + let watch = new Stopwatch(); + watch.Start(); + + let res = list |> sort ( <= ) + + res |> printfn "排序后:%A" + watch.Stop(); + + printfn "耗时(毫秒):%A" watch.Elapsed.TotalMilliseconds + with + | ex -> printfn "%A" ex \ No newline at end of file diff --git a/Program.fs b/Program.fs new file mode 100644 index 0000000..7498240 --- /dev/null +++ b/Program.fs @@ -0,0 +1,10 @@ +open System.Diagnostics +open Sanchime.Algrithm.Sort.Test +open Sanchime.Algrithm.Sort.Bubble +open Sanchime.Algrithm.Sort.Insert +open Sanchime.Algrithm.Sort.Quick +open Sanchime.Algrithm.Sort.Merge +open Sanchime.Algrithm.Sort.Select + + +qsort |> test \ No newline at end of file diff --git a/Sanchime.Learn.fsproj b/Sanchime.Learn.fsproj new file mode 100644 index 0000000..7d33f6b --- /dev/null +++ b/Sanchime.Learn.fsproj @@ -0,0 +1,15 @@ + + + Exe + net6.0 + + + + + + + + + + + \ No newline at end of file