排序 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 命令可针对文本文件的内容,以行为单位进行排序.其基本语法格式为: ...
随机推荐
- Xcode7以后 使用空模板
Xcode7以后的版本没有空模板可以选择 习惯使用空模版的 可以自己拷贝空模版文件夹 放到模版的位置 重启就可以使用了 1:首先 需要有一个空模版 没有空模板的可以在下面路径下下载一个 并解压 htt ...
- rem字体响应式布局
引用js,自动算字体大小,响应式布局 <script> var iScale = 1; iScale = iScale / window.devicePixelRatio; documen ...
- silverLight--绑定数据dataGrid
后台代码编写 ,为表格绑定数据: using System; using System.Collections.Generic; using System.Linq; using System.Net ...
- 结构化查询语言(SQL)数据类型
简要描述一下结构化查询语言中的五种数据类型:字符型,文本型,数值型,逻辑型和日期型. 字符型 VARCHARVS CHAR VARCHAR型和CHAR型数据的这个差别是细微的,但是非常重要.他们都是用 ...
- 深入研究C语言 第一篇(续)
没有读过第一篇的读者,可以点击这里,阅读深入研究C语言的第一篇. 问题一:如何打印变量的地址? 我们用取地址符&,可以取到变量的偏移地址,用DS可以取到变量的段地址. 1.全局变量: 我们看到 ...
- MongoDB基本命令用
成功启动MongoDB后,再打开一个命令行窗口输入mongo,就可以进行数据库的一些操作. 输入help可以看到基本操作命令: show dbs:显示数据库列表 show collections:显 ...
- KMP详解
原文: http://blog.csdn.net/v_july_v/article/details/7041827 从头到尾彻底理解KMP 1. 引言 本KMP原文最初写于2年多前的2011年12月, ...
- PHP 定界符使用
在PHP代码中,如果不想一行一行的拼接HTML或者JS的话,那么使用定界符将是最好的帮手! 使用方法: <<<eof .......html/js..... eof; 注意事项:(别 ...
- 微信小程序免费SSL证书https、TLS版本问题的解决方案
微信小程序与第三方服务器通讯的域名5个必要条件1.一个已备案的域名,不是localhost.也不是127.0.0.1,域名不能加端口2.加ssl证书,也就是https://~~~4.HTTPS 服务器 ...
- <Oracle Database>数据字典
数据字典 数据字典是由Oracle服务器创建和维护的一组只读的系统表,它存放了有关数据库和数据库对象的信息,Oracle服务器依赖这些信息来管理和维护Oracle数据库. 数据字典分为两大类:一种是基 ...