冒泡排序:

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. 如此清除sql server 2008 记住的用户名

    在C盘下搜索这个文件  SqlStudio.bin 搜索到后删除就可以,这样登录Sql server 时就不会有用户名密码之类的,相当于做了初始化操作,但里面的数据库什么的都是有的. 这只是个删除记住 ...

  2. XAF学习笔记之-多表头设计

    空闲之余,看了下全英文的DEV 的XAF帮助文档,一半的英文我认识他,一半的英文他认识我,反正拆开成26个字母我全认识 不那么啰嗦了,先看效果 如何做:分以下几步 第一步:打开这个文件,这个文件就是X ...

  3. 188. Best Time to Buy and Sell Stock IV leetcode解题笔记

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  4. oracle for loop循环以及游标循环

    1. for in loop形式 DECLARE    CURSOR c_sal IS SELECT employee_id, first_name || last_name ename, salar ...

  5. 总结30个CSS选择器

    或许大家平时总是在用的选择器都是:#id  .class  以及标签选择器.可是这些还远远不够,为了在开发中更加得心应手,本文总结了30个CSS3选择器,希望对大家有所帮助. 1 *:通用选择器 * ...

  6. 如何让你的UWP应用程序无缝调用几何作图

    有时候需要编辑一些几何图形,如三角形,圆锥曲线等,在UWP应用中加入这些几何作图功能是件费时间又很难做好的事.其实Windows 10 应用商店中已有一些专业的几何作图工具了,那么能借来一用吗?答案是 ...

  7. java的三元运算符

    1.三元运算符语法:判断表达式?表达式1:表达式2: (1)三元运算符适合于判断2个值到底使用哪一个! public static void mian(String[] args){ int sex= ...

  8. Spring 学习笔记 8. 尚硅谷_佟刚_Spring_使用外部属性文件

    1,配置数据源 (1)添加驱动 (2)编写spring配置文件 <bean id="dataSource" class="org.springframework.j ...

  9. 领域模型驱动设计(Domain Driven Design)入门概述

    软件开发要干什么: 反映真实世界要自动化的业务流程 解决现实问题 领域Domain Domain特指软件关注的领域 在不能充分了解业务领域的情况下是不可能做出一个好的软件 领域建模 领域模型驱动设计 ...

  10. 在c#中使用bitblt显示图片

    使用bitblt比DrawImage有更好的性能 using AForge.Video.DirectShow; using System; using System.Collections.Gener ...