冒泡排序:

let rec bsort_asc s =
let rec _bsort_asc = function
|x1::x2::xs when x1 > x2 ->
match _bsort_asc (x1::xs) with
|None -> Some(x2::x1::xs)
|Some xs2 -> Some(x2::xs2)
|x1::x2::xs ->
match _bsort_asc (x2::xs) with
|None -> None
|Some xs2 -> Some(x1::xs2)
|_ -> None
match _bsort_asc s with
|None -> s
|Some s2 -> bsort_asc s2

快速排序:

let rec quicksort =
function
[] -> []
|x::xs ->
quicksort [for i in xs do if i < x then yield i]@
x:: quicksort [for i in xs do if i >= x then yield i]

插入排序

let rec insert x = function
|[] -> [x]
|y::tl when x > y -> y::(insert x tl)
|l -> x::l
let rec insertion_sort alist =
match alist with
|[] -> []
|x::tl -> insert x (insertion_sort tl)

希尔排序

let shellsort f a =
let N = Array.length a - 1
let mutable width = 1
while width <= N do
width <- 3 * width + 1
width <- width / 3
while width >= 1 do
for i = width to N do
let v = a.[i]
let mutable j = i
while j >= width && (f a.[j - width] v) > 0 do
a.[j] <- a.[j-width]
j <- j - width
a.[j] <- v
let asc a b = (a - b)
let dec a b = (b - a)

直接选择排序

let rec ssort_asc alist =
match alist with
|[] -> []
|l ->
let minrest l =
let rec aux list min acc =
match list with
[] -> min, acc
|h::t ->
if h < min then aux t h (min::acc)
else aux t min (h::acc)
aux (List.tail l)(List.head l)[]
let (min, rest) = minrest l
min :: (ssort_asc rest)

堆排序

let swap (a:_ []) i j =
let temp = a.[i]
a.[i] <- a.[j]
a.[j] <- temp
let sift cmp (a:_ []) start count =
let rec loop root child =
if root * 2 + 1 < count then
let p = child < count - 1 && cmp a.[child] a.[child + 1] < 0
let child = if p then child + 1 else child
if cmp a.[root] a.[child] < 0 then
swap a root child
loop child (child * 2 + 1)
loop start (start * 2 + 1)
let heapsort cmp (a:_ []) =
let n = a.Length
for start = n/2 - 1 downto 0 do
sift cmp a start n
for term = n - 1 downto 1 do
swap a term 0
sift cmp a 0 term
let asc a b = (a - b)
let dec a b = (b - a)

归并排序

let split list =
let rec aux l acc1 acc2 =
match l with
|[] -> (acc1, acc2)
|[x] -> (x::acc1, acc2)
|x::y::tail -> aux tail (x::acc1) (y::acc2)
aux list [] []
let rec merge l1 l2 =
match (l1, l2) with
|(x, []) -> x
|([], y) -> y
|(x::tx, y::ty) ->
if x < y then x::merge tx l2
else y::merge l1 ty
let rec mergesort list =
match list with
|[] -> []
|[x] -> [x]
|_ ->
let (l1, l2) = split list
merge (mergesort l1) (mergesort l2)

基数排序

let radixSort (list:int array) : int array =
let largestInt = Array.maxBy abs list
let largestIntLen = if largestInt = 0 then 1 else (int(log10 (abs(largestInt |> double))) + 1)
for i = 0 to largestIntLen - 1 do
let mutable buckets = Array.create 19 (List.empty<int>)
for number in list do
let nthDigit nth number = int(abs(number) / int(10. ** (float(nth)))) % 10
let nth = nthDigit i number
if number < 0 then
buckets.[9 - nth] <- (List.append buckets.[9 - nth][number])
elif number = 0 then
buckets.[9 + nth] <- (List.append buckets.[9 + nth][number])
let mutable listIndex = 0
for bucket in buckets do
for num in bucket do
list.[listIndex] <- num
listIndex <- listIndex + 1
list

排序 via F#的更多相关文章

  1. Codeforces Round #541 (Div. 2) D(并查集+拓扑排序) F (并查集)

    D. Gourmet choice 链接:http://codeforces.com/contest/1131/problem/D 思路: =  的情况我们用并查集把他们扔到一个集合,然后根据 > ...

  2. 常用算法——排序(三)

    希尔排序法 希尔排序又称为缩小增量排序,也属于插入排序类的算法,是对直接插入排序的一种改进. 基本思想就是:将需要排序的序列划分为若干个较小的序列,对这些序列进行直接插入排序,通过这样的操作可使用需要 ...

  3. 非常强大的table根据表头排序,点击表头名称,对其内容排序

    js代码: /** * 通过表头对表列进行排序 * * @param sTableID * 要处理的表ID<table id=''> * @param iCol * 字段列id eg: 0 ...

  4. Java面试宝典系列之基础排序算法

    本文就是介绍一些常见的排序算法.排序是一个非常常见的应用场景,很多时候,我们需要根据自己需要排序的数据类型,来自定义排序算法,但是,在这里,我们只介绍这些基础排序算法,包括:插入排序.选择排序.冒泡排 ...

  5. Java常见的几种排序算法-插入、选择、冒泡、快排、堆排等

    本文就是介绍一些常见的排序算法.排序是一个非常常见的应用场景,很多时候,我们需要根据自己需要排序的数据类型,来自定义排序算法,但是,在这里,我们只介绍这些基础排序算法,包括:插入排序.选择排序.冒泡排 ...

  6. 线性时间的排序算法--桶排序(以leetcode164. Maximum Gap为例讲解)

    前言 在比较排序的算法中,快速排序的性能最佳,时间复杂度是O(N*logN).因此,在使用比较排序时,时间复杂度的下限就是O(N*logN).而桶排序的时间复杂度是O(N+C),因为它的实现并不是基于 ...

  7. ptyhon 编程基础之函数篇(二)-----返回函数,自定义排序函数,闭包,匿名函数

    一.自定义排序函数 在Python中可以使用内置函数sorted(list)进行排序: 结果如下图所示: 但sorted也是一个高阶函数,可以接受两个参数来实现自定义排序函数,第一个参数为要排序的集合 ...

  8. Django F()表达式

    Django F()表达式 一个F()对象代表一个模型字段的值或注释列.使用它可以直接引用模型字段的值并执行数据库操作而不用把它们导入到python的内存中. 相反,Django使用F()对象生成一个 ...

  9. linux命令(47):Linux下对文件进行按行排序,去除重复行

    Linux下对文件进行按行排序:sort 与 uniq 命令简介 Linux | May 24, 2015 | linux sort 命令可针对文本文件的内容,以行为单位进行排序.其基本语法格式为: ...

随机推荐

  1. POJ 3469 Dual Core CPU 最大流

    划分成两个集合使费用最小,可以转成最小割,既最大流. //#pragma comment(linker, "/STACK:1024000000,1024000000") #incl ...

  2. 调用 WebService 浏览器提示 500 (Internal Server Error) 的原因及解决办法

    在 ASP.NET 开发中,WebService部署成站点之后,如果在本地测试WebService可以运行,在远程却显示“测试窗体只能用于来自本地计算机的请求”或 者"The test fo ...

  3. PHP中spl_autoload_register函数的用法

    spl_autoload_register (PHP 5 >= 5.1.2) spl_autoload_register — 注册__autoload()函数 说明bool spl_autolo ...

  4. axure设置变量值

    以登录框为例设置axure变量值 1.打开axure,打开新页面命名为login,拖入一个矩形背景,命名:登录背景图 2.拖入标签控件和输入框控件分别命名为用户名:.userName.密码:.pass ...

  5. 一个sendMessage

    Message 1.判断是否同意协议.2.验证验证码是否正确.3.验证手机是否符合规则,符合规则就用message()发送短信,验证码的有效期以及使用的短信模板,在配置文件中进行管理.返回值下标为st ...

  6. Android 图片添加水印图片或者文字

    给图片添加水印的基本思路都是载入原图,添加文字或者载入水印图片,保存图片这三个部分 添加水印图片: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ...

  7. linux下的触控板手势xSwipe and tag

    这个最初是采用的touchegg.开始没有效果,后来网上发现是因为需要禁用系统的2指3指操作参考 http://askubuntu.com/questions/266057/cant-get-touc ...

  8. 1154. Easy sort

    #include<iostream>#include<cmath>#include<iomanip>#include<algorithm>using n ...

  9. Method not found : Void System.Data.Objects.ObjectContextOptions.set_UseConsistentNullReferenceBehavior(Boolean)

    找不到方法:“Void System.Data.Objects.ObjectContextOptions.set_UseConsistentNullReferenceBehavior(Boolean) ...

  10. 基于Django的web开发

    github地址:https://github.com/shirleyandgithub/PythonWeb