一些排序算法
This commit is contained in:
commit
1b9a1a6faf
|
@ -0,0 +1,2 @@
|
||||||
|
/bin
|
||||||
|
/obj
|
|
@ -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)
|
|
@ -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
|
||||||
|
|
|
@ -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
|
||||||
|
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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))
|
|
@ -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
|
|
@ -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
|
|
@ -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>
|
Loading…
Reference in New Issue