一些排序算法

This commit is contained in:
Sanchime 2022-07-05 20:26:22 +08:00
commit 1b9a1a6faf
9 changed files with 156 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
/bin
/obj

View File

@ -0,0 +1,16 @@
namespace Sanchime.Algrithm.Sort
[<AutoOpen>]
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)

View File

@ -0,0 +1,14 @@
namespace Sanchime.Algrithm.Sort
[<AutoOpen>]
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

View File

@ -0,0 +1,47 @@
namespace Sanchime.Algrithm.Sort
[<AutoOpen>]
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

View File

@ -0,0 +1,11 @@
namespace Sanchime.Algrithm.Sort
[<AutoOpen>]
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

View File

@ -0,0 +1,17 @@
namespace Sanchime.Algrithm.Sort
[<AutoOpen>]
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))

24
Algrithms/Sorts/Test.fs Normal file
View File

@ -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

10
Program.fs Normal file
View File

@ -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

15
Sanchime.Learn.fsproj Normal file
View File

@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="Algrithms/Sorts/SelectSort.fs" />
<Compile Include="Algrithms/Sorts/MergeSort.fs" />
<Compile Include="Algrithms/Sorts/QuickSort.fs" />
<Compile Include="Algrithms/Sorts/InsertSort.fs" />
<Compile Include="Algrithms/Sorts/BubbleSort.fs" />
<Compile Include="Algrithms/Sorts/Test.fs" />
<Compile Include="Program.fs" />
</ItemGroup>
</Project>