- 浏览: 2649824 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
80后的童年2:
深入浅出MongoDB应用实战开发网盘地址:https://p ...
MongoDB入门教程 -
shliujing:
楼主在不是精通java和php的前提下,请不要妄下结论。
PHP、CakePHP哪凉快哪呆着去 -
安静听歌:
希望可以一给一点点注释
MySQL存储过程之代码块、条件控制、迭代 -
qq287767957:
PHP是全宇宙最强的语言!
PHP、CakePHP哪凉快哪呆着去 -
rryymmoK:
深入浅出MongoDB应用实战开发百度网盘下载:链接:http ...
MongoDB入门教程
Θ(n^2)
1, Bubble sort
2, Selection sort
3, Insertion sort
4, Shell sort
Θ(n*logn)
1, Merge sort
2, Heap sort
3, Quick sort
Θ(n)
1, Counting sort
2, Radix sort
3, Bucket sort
1, Bubble sort
def bubble_sort(a) (a.size-2).downto(0) do |i| (0..i).each do |j| a[j], a[j+1] = a[j+1], a[j] if a[j] > a[j+1] end end return a end
2, Selection sort
def selection_sort(a) b = [] a.size.times do |i| min = a.min b << min a.delete_at(a.index(min)) end return b end
3, Insertion sort
def insertion_sort(a) a.each_with_index do |el,i| j = i - 1 while j >= 0 break if a[j] <= el a[j + 1] = a[j] j -= 1 end a[j + 1] = el end return a end
4, Shell sort
def shell_sort(a) gap = a.size while(gap > 1) gap = gap / 2 (gap..a.size-1).each do |i| j = i while(j > 0) a[j], a[j-gap] = a[j-gap], a[j] if a[j] <= a[j-gap] j = j - gap end end end return a end
Θ(n*logn)
1, Merge sort
def merge(l, r) result = [] while l.size > 0 and r.size > 0 do if l.first < r.first result << l.shift else result << r.shift end end if l.size > 0 result += l end if r.size > 0 result += r end return result end def merge_sort(a) return a if a.size <= 1 middle = a.size / 2 left = merge_sort(a[0, middle]) right = merge_sort(a[middle, a.size - middle]) merge(left, right) end
2, Heap sort
def heapify(a, idx, size) left_idx = 2 * idx + 1 right_idx = 2 * idx + 2 bigger_idx = idx bigger_idx = left_idx if left_idx < size && a[left_idx] > a[idx] bigger_idx = right_idx if right_idx < size && a[right_idx] > a[bigger_idx] if bigger_idx != idx a[idx], a[bigger_idx] = a[bigger_idx], a[idx] heapify(a, bigger_idx, size) end end def build_heap(a) last_parent_idx = a.length / 2 - 1 i = last_parent_idx while i >= 0 heapify(a, i, a.size) i = i - 1 end end def heap_sort(a) return a if a.size <= 1 size = a.size build_heap(a) while size > 0 a[0], a[size-1] = a[size-1], a[0] size = size - 1 heapify(a, 0, size) end return a end
3, Quick sort
def quick_sort(a) (x=a.pop) ? quick_sort(a.select{|i| i <= x}) + [x] + quick_sort(a.select{|i| i > x}) : [] end
Θ(n)
1, Counting sort
def counting_sort(a) min = a.min max = a.max counts = Array.new(max-min+1, 0) a.each do |n| counts[n-min] += 1 end (0...counts.size).map{|i| [i+min]*counts[i]}.flatten end
2, Radix sort
def kth_digit(n, i) while(i > 1) n = n / 10 i = i - 1 end n % 10 end def radix_sort(a) max = a.max d = Math.log10(max).floor + 1 (1..d).each do |i| tmp = [] (0..9).each do |j| tmp[j] = [] end a.each do |n| kth = kth_digit(n, i) tmp[kth] << n end a = tmp.flatten end return a end
3, Bucket sort
def quick_sort(a) (x=a.pop) ? quick_sort(a.select{|i| i <= x}) + [x] + quick_sort(a.select{|i| i > x}) : [] end def first_number(n) (n * 10).to_i end def bucket_sort(a) tmp = [] (0..9).each do |j| tmp[j] = [] end a.each do |n| k = first_number(n) tmp[k] << n end (0..9).each do |j| tmp[j] = quick_sort(tmp[j]) end tmp.flatten end a = [0.75, 0.13, 0, 0.44, 0.55, 0.01, 0.98, 0.1234567] p bucket_sort(a) # Result: [0, 0.01, 0.1234567, 0.13, 0.44, 0.55, 0.75, 0.98]
评论
2 楼
q1241312
2012-10-20
厉害,快速排序用循环实现了,不知道时间复杂度是多少。
1 楼
gemstone
2008-11-27
很不错, 这里也有个实现。
Source: http://github.com/kanwei/algorithms/tree/master
require 'containers/heap' # for heapsort =begin rdoc This module implements sorting algorithms. Documentation is provided for each algorithm. =end module Algorithms::Sort # Bubble sort: A very naive sort that keeps swapping elements until the container is sorted. # Requirements: Needs to be able to compare elements with <=>, and the [] []= methods should # be implemented for the container. # Time Complexity: О(n^2) # Space Complexity: О(n) total, O(1) auxiliary # Stable: Yes # # Algorithms::Sort.bubble_sort [5, 4, 3, 1, 2] => [1, 2, 3, 4, 5] def self.bubble_sort(container) loop do swapped = false (container.size-1).times do |i| if (container[i] <=> container[i+1]) == 1 container[i], container[i+1] = container[i+1], container[i] # Swap swapped = true end end break unless swapped end container end # Comb sort: A variation on bubble sort that dramatically improves performance. # Source: http://yagni.com/combsort/ # Requirements: Needs to be able to compare elements with <=>, and the [] []= methods should # be implemented for the container. # Time Complexity: О(n^2) # Space Complexity: О(n) total, O(1) auxiliary # Stable: Yes # # Algorithms::Sort.comb_sort [5, 4, 3, 1, 2] => [1, 2, 3, 4, 5] def self.comb_sort(container) container gap = container.size loop do gap = gap * 10/13 gap = 11 if gap == 9 || gap == 10 gap = 1 if gap < 1 swapped = false (container.size - gap).times do |i| if (container[i] <=> container[i + gap]) == 1 container[i], container[i+gap] = container[i+gap], container[i] # Swap swapped = true end end break if !swapped && gap == 1 end container end # Selection sort: A naive sort that goes through the container and selects the smallest element, # putting it at the beginning. Repeat until the end is reached. # Requirements: Needs to be able to compare elements with <=>, and the [] []= methods should # be implemented for the container. # Time Complexity: О(n^2) # Space Complexity: О(n) total, O(1) auxiliary # Stable: Yes # # Algorithms::Sort.selection_sort [5, 4, 3, 1, 2] => [1, 2, 3, 4, 5] def self.selection_sort(container) 0.upto(container.size-1) do |i| min = i (i+1).upto(container.size-1) do |j| min = j if (container[j] <=> container[min]) == -1 end container[i], container[min] = container[min], container[i] # Swap end container end # Heap sort: Uses a heap (implemented by the Containers module) to sort the collection. # Requirements: Needs to be able to compare elements with <=> # Time Complexity: О(n^2) # Space Complexity: О(n) total, O(1) auxiliary # Stable: Yes # # Algorithms::Sort.heapsort [5, 4, 3, 1, 2] => [1, 2, 3, 4, 5] def self.heapsort(container) heap = Containers::Heap.new(container) ary = [] ary << heap.pop until heap.empty? ary end # Insertion sort: Elements are inserted sequentially into the right position. # Requirements: Needs to be able to compare elements with <=>, and the [] []= methods should # be implemented for the container. # Time Complexity: О(n^2) # Space Complexity: О(n) total, O(1) auxiliary # Stable: Yes # # Algorithms::Sort.insertion_sort [5, 4, 3, 1, 2] => [1, 2, 3, 4, 5] def self.insertion_sort(container) return container if container.size < 2 (1..container.size-1).each do |i| value = container[i] j = i-1 while j >= 0 and container[j] > value do container[j+1] = container[j] j = j-1 end container[j+1] = value end container end # Shell sort: Similar approach as insertion sort but slightly better. # Requirements: Needs to be able to compare elements with <=>, and the [] []= methods should # be implemented for the container. # Time Complexity: О(n^2) # Space Complexity: О(n) total, O(1) auxiliary # Stable: Yes # # Algorithms::Sort.shell_sort [5, 4, 3, 1, 2] => [1, 2, 3, 4, 5] def self.shell_sort(container) increment = container.size/2 while increment > 0 do (increment..container.size-1).each do |i| temp = container[i] j = i while j >= increment && container[j - increment] > temp do container[j] = container[j-increment] j -= increment end container[j] = temp end increment = (increment == 2 ? 1 : (increment / 2.2).round) end container end # Quicksort: A divide-and-conquer sort that recursively partitions a container until it is sorted. # Requirements: Container should implement #pop and include the Enumerable module. # Time Complexity: О(n log n) average, O(n^2) worst-case # Space Complexity: О(n) auxiliary # Stable: No # # Algorithms::Sort.quicksort [5, 4, 3, 1, 2] => [1, 2, 3, 4, 5] # def self.quicksort(container) # return [] if container.empty? # # x, *xs = container # # quicksort(xs.select { |i| i < x }) + [x] + quicksort(xs.select { |i| i >= x }) # end def self.partition(data, left, right) pivot = data[front] left += 1 while left <= right do if data[frontUnknown] < pivot back += 1 data[frontUnknown], data[back] = data[back], data[frontUnknown] # Swap end frontUnknown += 1 end data[front], data[back] = data[back], data[front] # Swap back end # def self.quicksort(container, left = 0, right = container.size - 1) # if left < right # middle = partition(container, left, right) # quicksort(container, left, middle - 1) # quicksort(container, middle + 1, right) # end # end def self.quicksort(container) bottom, top = [], [] top[0] = 0 bottom[0] = container.size i = 0 while i >= 0 do l = top[i] r = bottom[i] - 1; if l < r pivot = container[l] while l < r do r -= 1 while (container[r] >= pivot && l < r) if (l < r) container[l] = container[r] l += 1 end l += 1 while (container[l] <= pivot && l < r) if (l < r) container[r] = container[l] r -= 1 end end container[l] = pivot top[i+1] = l + 1 bottom[i+1] = bottom[i] bottom[i] = l i += 1 else i -= 1 end end container end # Mergesort: A stable divide-and-conquer sort that sorts small chunks of the container and then merges them together. # Returns an array of the sorted elements. # Requirements: Container should implement [] # Time Complexity: О(n log n) average and worst-case # Space Complexity: О(n) auxiliary # Stable: Yes # # Algorithms::Sort.mergesort [5, 4, 3, 1, 2] => [1, 2, 3, 4, 5] def self.mergesort(container) return container if container.size <= 1 mid = container.size / 2 left = container[0...mid] right = container[mid...container.size] merge(mergesort(left), mergesort(right)) end def self.merge(left, right) sorted = [] until left.empty? or right.empty? left.first <= right.first ? sorted << left.shift : sorted << right.shift end sorted + left + right end end
Source: http://github.com/kanwei/algorithms/tree/master
发表评论
-
用了TextMate才知道什么叫神级Editor
2011-03-09 04:51 57856一直用Eclipse作为开发Ruby和Java项目的IDE,但 ... -
Ruby使用OAuth登录新浪微博和豆瓣
2011-01-09 12:49 4298首先需要安装oauth这个gem包 gem install ... -
使用Passenger+nginx部署Rails
2010-12-28 15:12 49131. Install Passender gem instal ... -
markItUp+rdiscount搭建Rails下可视化Markdown编辑器
2010-12-21 17:48 5367markItUp是基于jQuery的可视化编辑器,支持Html ... -
Rails3 and MongoDB Quick Guide
2010-12-10 14:13 2710Install MongoDB Download: http: ... -
基于ruby-protobuf的rpc示例
2009-08-11 11:51 41041, 安装ruby-protobuf gem instal ... -
Ruby导出xls和csv的utf-8问题的解决
2009-02-04 15:05 6744数据库数据为utf-8格式,包括中文和拉丁文等等 导出文件xl ... -
URL/HTML/JavaScript的encode/escape
2009-01-04 13:03 9231最近经常被URL、HTML、JavaScript的encode ... -
12月5日北京RoR活动!
2008-11-26 18:38 2962又是一年过去了,Rails在国内的发展势态良好,很多使用RoR ... -
Rails程序开发的最大问题是代码规范
2008-08-28 11:56 5261使用Rails开发大型复杂B2B应用一年了,这个项目目前开发人 ... -
Web开发大全:ROR版——推荐序
2008-07-09 00:39 2371来自http://www.beyondrails.com/bl ... -
深入ActionMailer,使用Sendmail发邮件
2008-07-03 11:41 3362来自: http://www.beyondrails.com/ ... -
Rails里如何结合ExceptionNotification配置gmail账户发邮件
2008-06-19 19:56 30081,安装ExceptionNotification rub ... -
使用coderay和railscasts样式进行代码高亮
2008-06-17 00:16 2352CodeRay是一个语法高亮的Ruby库,效率很不错。 Cod ... -
Capistrano试用
2008-06-16 19:05 18961,客户端机器安装Capistrano gem insta ... -
lighttpd真垃圾啊
2008-06-04 18:38 2457使用lighttpd+fcgi跑Rails程序,文件上传会si ... -
将gem变成plugin
2008-06-04 11:27 1756有什么样的需求就有什么样的对策 当vhost上的帐号没有ge ... -
在Rails里使用ReCaptcha添加验证码
2008-06-03 15:51 42061,去http://recaptcha.net/sign up ... -
Rails里给文件上传添加progress_bar
2008-05-27 17:00 2046文件上传很慢时,UI没有什么用户提示,这样让人很费解,所以我们 ... -
attachment_fu的一个bug
2008-05-27 16:25 1730上传文件的size经常结果为0,让人很费解 解决办法,atta ...
相关推荐
ruby_使用ruby实现的排序算法_sorting
ruby_使用ruby实现的算法之冒泡排序
主要介绍了Ruby实现的各种排序算法,本文给出了Bubble sort、Insertion sort、Selection sort、Shell sort等排序的实现方法,需要的朋友可以参考下
主要介绍了Ruby实现的3种快速排序算法,本文给出了快速排序的普通版本、快速排序的随机化版本、快速排序的利用了Ruby的语法糖的随机化版本三个版本,需要的朋友可以参考下
#Data Structures 这个仓库充满了我对各种数据结构的实现。 虽然这是一个 ruby 存储库,但我尽量不以 Ruby 方式实现这些结构,而是更规范的实现。 ##Array 使用内部数组并提供以下方法<< [] []= 推流行...
主要介绍了Ruby实现的合并排序算法,本文直接给出实现代码,需要的朋友可以参考下
用ruby语言实现的几个小算法,排序和查找
插入排序即是把已有的有序序列从后向前扫描插入元素,数值大的向后移动,这里我们就来看一下使用Ruby实现插入排序算法及进阶的二路插入排序代码示例
ser.rb 代码如下:require ‘socket’s = TCPServer.new 3333conn = s.acceptloop do puts ... 您可能感兴趣的文章:Ruby实现的各种排序算法Ruby实现的合并排序算法Ruby实现的3种快速排序算法Ruby一行代码实现的快速
Ruby是一种优雅且富有表现力的编程语言,它拥有丰富的算法资源,为开发者提供了解决各种问题的强大工具。Ruby的算法库涵盖了排序、搜索、图论、密码学等多个领域,这些算法经过优化,可在实际项目中高效运行。 在...
1、插入排序 代码如下:seq = [3,4,9,0,2,5,9,7,1] 1.upto(seq.length-1) do |i| if seq[i] < seq xss=removed xss=removed>=0 && tmp<seq[j]) do seq[j+1] = seq[j] j=j-1 end seq[j+1]=tmp endend seq....
冒泡排序为最基本的排序算法之一,其时间复杂度为O(n^2),这里我们就来简单看一下冒泡排序算法及Ruby版的简单实现,首先还是先来了解算法原理:
Ruby的通用唯一词典可排序标识符实现 官方规格页面: : 通用唯一词典分类标识符 对于许多用例,UUID可能不是最佳的,因为: 这不是编码128位随机性的最有效字符方式 字符串格式本身显然基于原始MAC和时间版本...
主要介绍了Ruby一行代码实现的快速排序,本文直接给出实现代码,超级简洁的一个的方法,需要的朋友可以参考下
Ruby中的算法和数据结构精选在超和该存储库包含各种算法和数据结构的Ruby实现,以及和的许多挑战的解决方案内容: 基于二分搜索的问题阵列旋转算法阵列旋转的块交换算法子数组问题(Kadane算法)改组数组在数组中...