排序 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 命令可针对文本文件的内容,以行为单位进行排序.其基本语法格式为: ...
随机推荐
- 关于本地缓存localStorage
localStorage的优势 1.localStorage拓展了cookie的4K限制 2.localStorage会可以将第一次请求的数据直接存储到本地,这个相当于一个5M大小的针对于前端页面的数 ...
- dedecms内容管理系统学习
在复习完基础知识和学习了tp3.2之后,我们开始学习一些开源的产品,如dedecms:
- boot from volume
nova boot --flavor 1 --block-device source=image,id=<image_id>,dest=volume,size=5,shutdown=pre ...
- 4. Prototype(原型)
意图: 用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象. 适用性: 当要实例化的类是在运行时刻指定时,例如,通过动态装载:或者 为了避免创建一个与产品类层次平行的工厂类层次时:或者 当 ...
- SUSE查看版本号
suse:~ # lsb_release -dDescription: SUSE Linux Enterprise Server 10 (x86_64) suse:~ # cat /etc/SuSE- ...
- Sql Server 常用操作2
FOR XML PATH应用 stuID学生编号,sName代表学生姓名,hobby列存学生的爱好! SELECT B.sName,LEFT(StuList,LEN(StuList)-1) as ho ...
- java开发常用工具
1.eclipse3.6 +浏览器插件+findbug+checkstyle+pmd+svn 2.plsql8.0对数据库的操作,存储过程的调试 3.Securecrt对linux服务器的操作 4.e ...
- 【Java】:googleSearch
google custom search是一个基于google的搜索引擎api,可以请求谷歌的搜索数据 pala pala pala ... 实现: 1.注册谷歌账号 2.创建google项目 1 ...
- MVC的增删改和Razor
ASP.NET MVC中的增删改查 基本都要使用C控制器中的两个action来完成操作,一个用于从主界面跳转到新页面.同时将所需操作的数据传到新界面,另一个则对应新界面的按钮,用于完成操作.将数据传回 ...
- Python和C扩展实现方法
一.Python和C扩展 cPython是C编写的,python的扩展可以用C来写,也便于移植到C++. 编写的Python扩展,需要编译成一个.so的共享库. Python程序中. 官方文档:htt ...