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 ...
随机推荐
- CDC ->> 在某个SQL Server中开启CDC功能
USE AdventureWorks;GO--开启某个数据库的CDC功能EXEC sys.sp_cdc_enable_db;GO--is_cdc_enabled栏位为1代表开启CDC功能了SELECT ...
- 通过从代码层面分析Linux内核启动来探知操作系统的启动过程
通过从代码层面分析Linux内核启动来探知操作系统的启动过程 前言说明 本篇为网易云课堂Linux内核分析课程的第三周作业,我将围绕Linux 3.18的内核中的start_kernel到init进程 ...
- echo "hello" | nc -4t -w1 localhost 8001
TCP4: echo "hello" | nc -4t -w1 localhost 8001 UDP4: echo "hello" | nc -4u -w1 l ...
- php去除数组中重复数据
<?php /** * 去除数组中重复数据 * by www.jbxue.com **/ $input = array("a" => "green" ...
- Oracle PO - 模块一揽子采购协议小结
本文总结oracle ebs采购订单(po)模块一揽子采购协议的相关知识,总结如下: 1.理论介绍 (1)名词术语 一揽子采购协议(Blanket Purchase Agreement,BPA)是指某 ...
- [51NOD1181]质数中的质数(质数筛法)(欧拉筛)
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1181 思路:欧拉筛出所有素数和一个数的判定,找到大于n的最小质 ...
- IE6下margin出现双边距
在IE6下,块元素有浮动和横向margin的时候,横向的margin值会被放大成两倍 解决方法:添加display:inline; eg:下面的例子在IE6下会有两倍边距 <style> ...
- 百度编辑器解决span被过滤, 自动加P标签
editor_all.js: 自动加P标签去除: enterTag: 'p', 改成: enterTag: '', span被过滤: //从编辑器出去的内容处理 me.addOutputR ...
- PHP基础 CGI,FastCGI,PHP-CGI与PHP-FPM
CGI CGI全称是“公共网关接口”(Common Gateway Interface),HTTP服务器与你的或其它机器上的程序进行“交谈”的一种工具,其程序须运行在网络服务器上. CGI可以用任何一 ...
- 【英语】Bingo口语笔记(79) - fish系列