内容简介:F# 插入排序 和归并排序
插入排序 insertSort
let insertSort array = let length = Array.length array for i in [1..(length-1)] do let key = array.[i] //insert key to sub array[0..i-1] let mutable j = i-1 while (j>=0 && array.[j]>key) do array.[j+1] <- array.[j] j <- j-1 array.[j+1]<-key let a=[|6;7;1;3;2;9;8;18;18;12|] a |> insertSort
归并排序 mergesort
let merge array lo mid hi = let mutable i = lo let mutable j = mid let length = Array.length array let tempArray = Array.create length 0 let mutable index =0 while (i<mid && j<hi) do if array.[i]<array.[j] then tempArray.[index]<-array.[i] i<-i+1 index<-index+1 if array.[i]>=array.[j] then tempArray.[index]< -array.[j] j<-j+1 index<-index+1 //check if there's any remaining let if i<mid then for k in [i .. (mid-1)] do tempArray.[index] <- array.[k] index<-index+1 if j<hi then for k in [j .. (hi-1)] do tempArray.[index] <- array.[k] index<-index+1 for i in [lo..(hi-1)] do array.[i] <- tempArray.[i-lo] let rec mergeSort' array lo hi = let length = hi - lo if length>1 then let mid = max ((hi - lo)/2) (lo+1) mergeSort' array lo mid mergeSort' array mid hi merge array lo mid hi let mergeSort array = mergeSort' array 0 (Array.length array) let array =[|4;1;5;6;7;8;13;19;12|] array |> mergeSort
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:- MergeSort归并排序和利用归并排序计算出数组中的逆序对
- 归并排序与快速排序
- F# 插入排序 和归并排序
- 算法之常见排序算法-冒泡排序、归并排序、快速排序
- 深入浅出排序算法(2)-归并排序
- 归并排序求逆序数
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Software Paradigms
Stephen H. Kaisler / Wiley-Interscience / 2005-03-17 / USD 93.95
Software Paradigms provides the first complete compilation of software paradigms commonly used to develop large software applications, with coverage ranging from discrete problems to full-scale applic......一起来看看 《Software Paradigms》 这本书的介绍吧!