Basic Sorting Algorithms
*稳定指原本数列中相同的元素的相对前后位置在排序后不会被打乱
快速排序(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的更多相关文章
- Basic Sort Algorithms
1. Bubble Sort public void bubbleSort(int[] arr) { boolean swapped = true; int j = 0; int tmp; while ...
- JavaScript 排序算法(JavaScript sorting algorithms)
JavaScrip 排序算法(JavaScript Sorting Algorithms) 基础构造函数 以下几种排序算法做为方法放在构造函数里. function ArrayList () { va ...
- [Algorithms] Sorting Algorithms (Insertion Sort, Bubble Sort, Merge Sort and Quicksort)
Recently I systematicall review some sorting algorithms, including insertion sort, bubble sort, merg ...
- Summary: sorting Algorithms
Insertion sort is a simple sorting algorithm that builds the final sorted array (or list) one item a ...
- 算法的稳定性(Stability of Sorting Algorithms)
如果具有同样关键字的纪录的在排序前和排序后相对位置保持不变.一些算法本身就是稳定的,如插入排序,归并排序,冒泡排序等,不稳定的算法有堆排序,快速排序等. 然而,一个本身不稳定的算法通过一点修正也能变成 ...
- Sorting Algorithms
Merge sort by using recursive strategy, i.e. divide and conquer. def merge(left,right): result = [] ...
- 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 ...
- 转:Top 10 Algorithms for Coding Interview
The following are top 10 algorithms related concepts in coding interview. I will try to illustrate t ...
- 1306. Sorting Algorithm 2016 12 30
1306. Sorting Algorithm Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description One of the f ...
随机推荐
- 生产环境的redis高可用集群搭建
这里只是总结一下安装步骤 如果要了解redis集群高可用的原理,推荐仔细看一遍配置文件示例http://download.redis.io/redis-stable/redis.conf,源码包里也有 ...
- spring3定时器简单配置
最近在做oa项目中写到一个功能,就是员工每天的签到和签退.当时想了很久都没有想出来,后来自己上网查了一下spring的定时器,然后就有了思路. 下面我贴上自己用到的这个定时器的配置.希望能够和大家一起 ...
- poj -3262 Protecting the Flowers (贪心)
http://poj.org/problem?id=3262 开始一直是理解错题意了!!导致不停wa. 这题是农夫有n头牛在花园里啃花朵,然后农夫要把它们赶回棚子,每次只能赶一头牛,并且给出赶回每头牛 ...
- NDK(6)eclipse下断点调试ndk代码
Using the NDK Plugin 1. First set the path to SDK and NDK: Eclipse -> Window -> Preferences -& ...
- Is valid identifier?
Given a string, determine if it's a valid identifier. Here is the syntax for valid identifiers: Each ...
- Spring Transaction + MyBatis SqlSession事务管理机制[marked]
- Oracle数据库之二
SELECT查询 函数分为: 单行函数 -- 一条记录进入,一条记录输出 多行函数(分组函数)-- 多条记录进入,按组输出 单行函数: select id,first_name,nvl(commiss ...
- 51nod1431 快乐排队
神???.我们可以发现无论怎么交换ai+i都是不变的.那么这样就可以了 #include<cstdio> #include<cstring> #include<cctyp ...
- java MVC设计模式
MVC(Model View Control)模型-视图-控制器 一.MVC与模板概念的理解 MVC本来是存在于Desktop程序中的,M是指数据模型,V是指用户界面,C则是控制器.使用MVC的目的是 ...
- ucosII移植
移植ucos II 到一个芯片上,只需要修改下面三个文件:OS_CPU.H,OS_CPU_C.C,OS_CPU_A.ASM. 具体来说,移植主要包括以下几项内容 (1).OS_CPU.H :用#def ...