*稳定指原本数列中相同的元素的相对前后位置在排序后不会被打乱

快速排序(n*lgn 不稳定):数组中随机选取一个数x(这里选择最后一个),将数组按比x大的和x小的分成两部分,再对剩余两部分重复这个算法直到结束。但在数据量小的时候表现差。

def quick_sort(a)
(x = a.pop) ? quick_sort(a.select{|i| i <= x}) + [x] + quick_sort(a.select{|i| i > x}) : []
end

冒泡排序(n^2 稳定):如果a[n] > a[n+1] 则调换,冒泡排序一遍后的最大的值会被放在最后,然后依次对前n-1项再进行冒泡排序。

def bubble_sort(a)
(a.length-2).downto(0) {|i| i.times{|j| a[j],a[j+1] = a[j+1],a[j] if a[j]>a[j+1]}}
end

选择排序(n^2 不稳定):在n中选择最小值插入数组前端,然后再在后n-1项中找最小值放在数组第二个。

def selection_sort(a)
a.length.times do |i|
t = a.index(a[i..-1].min) #select the index of the next smallest element
a[i], a[t] = a[t], a[i] #swap
end
end

插入排序 (n^2 稳定):在数组开始处新建一个排序好的数组,然后对于之后的每个新扫描的值,插入之前排序好的数组的相应位置。

def insertion_sort(a)
(1...a.length).each do |i|
if a[i-1] > a[i]
temp = a[i] #store the number to be inserted
j = i
while j > 0 and a[j-1] > temp
a[j] = a[j-1] #move every elements in the array which is greater than the inserted number forward
j -= 1
end
a[j] = temp #insert the number
end
end
end

Basic Sorting Algorithms的更多相关文章

  1. Basic Sort Algorithms

    1. Bubble Sort public void bubbleSort(int[] arr) { boolean swapped = true; int j = 0; int tmp; while ...

  2. JavaScript 排序算法(JavaScript sorting algorithms)

    JavaScrip 排序算法(JavaScript Sorting Algorithms) 基础构造函数 以下几种排序算法做为方法放在构造函数里. function ArrayList () { va ...

  3. [Algorithms] Sorting Algorithms (Insertion Sort, Bubble Sort, Merge Sort and Quicksort)

    Recently I systematicall review some sorting algorithms, including insertion sort, bubble sort, merg ...

  4. Summary: sorting Algorithms

    Insertion sort is a simple sorting algorithm that builds the final sorted array (or list) one item a ...

  5. 算法的稳定性(Stability of Sorting Algorithms)

    如果具有同样关键字的纪录的在排序前和排序后相对位置保持不变.一些算法本身就是稳定的,如插入排序,归并排序,冒泡排序等,不稳定的算法有堆排序,快速排序等. 然而,一个本身不稳定的算法通过一点修正也能变成 ...

  6. Sorting Algorithms

    Merge sort by using recursive strategy, i.e. divide and conquer. def merge(left,right): result = [] ...

  7. Top 10 Algorithms for Coding Interview--reference

    By X Wang Update History:Web Version latest update: 4/6/2014PDF Version latest update: 1/16/2014 The ...

  8. 转:Top 10 Algorithms for Coding Interview

    The following are top 10 algorithms related concepts in coding interview. I will try to illustrate t ...

  9. 1306. Sorting Algorithm 2016 12 30

    1306. Sorting Algorithm Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description One of the f ...

随机推荐

  1. 解决COS、FileUpload上传文件时中文文件名乱码问题

    方法: MultipartParser mp = new MultipartParser(request, 10*1024*1024); mp.setEncoding("GBK") ...

  2. github简单使用教程(转)

    github是一个基于git的代码托管平台,付费用户可以建私人仓库,我们一般的免费用户只能使用公共仓库,也就是代码要公开.对于一般人来说公共仓库就已经足够了,而且我们也没多少代码来管理,O(∩_∩)O ...

  3. android ProgressBar 样式讲解

    转载自:eoe社区,可惜没找到源地址... 多式样ProgressBar 普通圆形ProgressBar 该类型进度条也就是一个表示运转的过程,例如发送短信,连接网络等等,表示一个过程正在执行中. 一 ...

  4. ubuntu下设置环境变量的三种方法【转】

    转自:http://blog.chinaunix.net/uid-26916352-id-3208366.html 通常设置环境变量有三种方法: 一.临时设置 export PATH=/home/ya ...

  5. Codeforces Round #362 (Div. 2) A.B.C

    A. Pineapple Incident time limit per test 1 second memory limit per test 256 megabytes input standar ...

  6. An AnnotationConfiguration instance is required to use

    An AnnotationConfiguration instance is required to use <mapping class="jebe7282/study/hibern ...

  7. [ionic开源项目教程] - 第1讲 前言,技术储备,环境搭建,常用命令

    前言 这是一个系列文章,将持续更新到项目完结,从环境搭建开始讲解,包括实战开发中遇到的各种问题的解决方案,都将毫无保留的分享给大家. 技术储备 开始本项目之前,请确保自己对以下技术点都有所了解. ht ...

  8. 生成并返回 json 结果文件

    #region 生成并返回 json 结果文件 /// <summary> /// 生成并返回 json 结果文件 /// </summary> /// <param n ...

  9. 盒模型padding和margin对滚动条位置的影响

    前置条件:盒模型样式overflow-y:scroll; ①padding-right:15px的效果: padding对于滚动条的位置显然是没有影响的,这也不是我们要的结果(这样很难看!) ②mar ...

  10. [转载]mysql的binlog安全删除

    [转载]mysql的binlog安全删除  理论上,应该在配置文件/etc/my.cnf中加上binlog过期时间的配置项,expire_logs_days = 10 但是如果没有加这一项,随着产生越 ...