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 ...
随机推荐
- Servlet如何实现修改后不重启服务器而生效
只需在apache-tomcat-8.0.0-RC10\conf\servlet.xml中修改相关设置: 在<Host name="localhost" appBase ...
- Android之开发杂记(一)
1.cygwin环境变量设置 可在Cygwin.bat 中设置 set NDK_ROOT=P:/android/android-ndk-r8e 或者在home\Administrator\.bash_ ...
- 深入理解Java内存模型(七)——总结
处理器内存模型 顺序一致性内存模型是一个理论参考模型,JMM和处理器内存模型在设计时通常会把顺序一致性内存模型作为参照.JMM和处理器内存模型在设计时会对顺序一致性模型做一些放松,因为如果完全按照顺序 ...
- mysql高可用方案比较
详见:High Availability Database Tools http://www.acquia.com/blog/high-availability-database-tools
- leetcode:Reverse Bits
Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...
- listview android:cacheColorHint,android:listSelector属性作用
ListView是常用的显示控件,默认背景是和系统窗口一样的透明色,如果给ListView加上背景图片,或者背景颜色时,滚动时listView会黑掉, 原因是,滚动时,列表里面的view重绘时,用 ...
- MVC 简单发送邮件示例
没啥好说的 直接上代码 @{ try { WebMail.SmtpServer = "smtp.qq.com";//SMTP邮件服务器 WebMail.SmtpPort = ;// ...
- .net4.0下 解决asp.net中“从客户端中检测到有潜在危险的Request.Form值”的错误
asp.net 2.0 通常解决办法 方案一: 将.aspx文件中的page项添加ValidateRequest="false" ,如下: <%@ Page Validate ...
- UVA 11354 Bond 邦德 (RMQ,最小瓶颈MST)
题意: n个城市,m条路,每条路有个危险值,要使得从s走到t的危险值最小.回答q个询问,每个询问有s和t,要求输出从s到t最小的危险值.(5万个点,10万条边) 思路: 其实要求的是任意点对之间的最小 ...
- smarty 初始配置文件
<?php define("ROOT",str_replace("\\","/",dirname(__FILE__)).'/'); r ...