排序 via F#
冒泡排序:
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#的更多相关文章
- Codeforces Round #541 (Div. 2) D(并查集+拓扑排序) F (并查集)
D. Gourmet choice 链接:http://codeforces.com/contest/1131/problem/D 思路: = 的情况我们用并查集把他们扔到一个集合,然后根据 > ...
- 常用算法——排序(三)
希尔排序法 希尔排序又称为缩小增量排序,也属于插入排序类的算法,是对直接插入排序的一种改进. 基本思想就是:将需要排序的序列划分为若干个较小的序列,对这些序列进行直接插入排序,通过这样的操作可使用需要 ...
- 非常强大的table根据表头排序,点击表头名称,对其内容排序
js代码: /** * 通过表头对表列进行排序 * * @param sTableID * 要处理的表ID<table id=''> * @param iCol * 字段列id eg: 0 ...
- Java面试宝典系列之基础排序算法
本文就是介绍一些常见的排序算法.排序是一个非常常见的应用场景,很多时候,我们需要根据自己需要排序的数据类型,来自定义排序算法,但是,在这里,我们只介绍这些基础排序算法,包括:插入排序.选择排序.冒泡排 ...
- Java常见的几种排序算法-插入、选择、冒泡、快排、堆排等
本文就是介绍一些常见的排序算法.排序是一个非常常见的应用场景,很多时候,我们需要根据自己需要排序的数据类型,来自定义排序算法,但是,在这里,我们只介绍这些基础排序算法,包括:插入排序.选择排序.冒泡排 ...
- 线性时间的排序算法--桶排序(以leetcode164. Maximum Gap为例讲解)
前言 在比较排序的算法中,快速排序的性能最佳,时间复杂度是O(N*logN).因此,在使用比较排序时,时间复杂度的下限就是O(N*logN).而桶排序的时间复杂度是O(N+C),因为它的实现并不是基于 ...
- ptyhon 编程基础之函数篇(二)-----返回函数,自定义排序函数,闭包,匿名函数
一.自定义排序函数 在Python中可以使用内置函数sorted(list)进行排序: 结果如下图所示: 但sorted也是一个高阶函数,可以接受两个参数来实现自定义排序函数,第一个参数为要排序的集合 ...
- Django F()表达式
Django F()表达式 一个F()对象代表一个模型字段的值或注释列.使用它可以直接引用模型字段的值并执行数据库操作而不用把它们导入到python的内存中. 相反,Django使用F()对象生成一个 ...
- linux命令(47):Linux下对文件进行按行排序,去除重复行
Linux下对文件进行按行排序:sort 与 uniq 命令简介 Linux | May 24, 2015 | linux sort 命令可针对文本文件的内容,以行为单位进行排序.其基本语法格式为: ...
随机推荐
- Robot Framework入门学习2 创建第一个测试用例
本文章部分内容引自以下网址,感谢作者的辛苦分享 http://www.cnblogs.com/fnng/p/3871712.html http://blog.csdn.net/tulituqi/art ...
- [Linux] 结构化命令 if
语法结构如下: 1. if-then语句 # if-then语句 if command #根据conmmand的退出状态码,选择执行语句 then commands fi e.g. #!usr/bin ...
- 什么是publickeytoken及publickeytoken的作用
什么是publickeytoken及publickeytoken的作用 dll的publickeytoken的作用.
- DHCP配置
DHCP服务器IP:192.168.1.10 一,安装dhcp [root@localhost ~]# yum install dhcp Loaded plugins: product-id, sub ...
- eclipse在光标停留在同一对象的背景色提示,开启与关闭
eclipse在光标停留在变量上的时候,同一变量能够提示相同的背景色.这个功能感觉不起眼,但是实在是很好用啊.如果不小心点消失了会很麻烦. 这里留个记录,如果关闭了记得开启: 开启关闭的位置在工具栏上 ...
- cassandra指定数据库路径
参考 https://docs.datastax.com/en/cassandra/2.1/cassandra/configuration/configCassandra_yaml_r.html 我们 ...
- ubuntu与win10互换硬盘
实例:将sdb上的ubuntu转移至sda,将sda上的win转移至sdb1. 备份资料2. 制作老毛桃PE盘3. 格式化sda4. dd if=/dev/sdb of=/dev/sda ,将sdb克 ...
- 当我我们用new操作符创建对象的时候,都发生了些什么?
//下面这段代码是javascript设计模式与开发实践上的一段代码 function Person( name ){ this.name = name; }; ...
- Java使用Scanner接收中文并输出时出现乱码
Java中使用Scanner接收输入的中文并输出时会出现乱码现象,怎么解决此问题呢? 1.方法一 在声明Scanner时添加对应的编码格式就可以了,如下所示: Scanner sc = new Sca ...
- C# Susan边缘检测(Susan Edge Detection)
Susan边缘检测,方法简单,效率高,具体参照 The SUSAN Edge Detector in Detail, 修改dThreshold值,可以改变检测效果,用参照提供的重心法.力矩法可得到边缘 ...