Simplest Python K-Way Merging Sort|最简单的Python k路归并排序
想做这个好长时间了,因为有一篇Dreamworks的论文《Coherent Out-of-Core Point-Based Global Illumination》提到了这个,一直没时间做,于是今天抽了十几分钟搞了一下,基于Python的,非常简单。由于基于点云的GI已经毫无疑问的不会使用了,所以Weta Digital的PantaRay和Dreamworks的这些方法都会直接宣布作废,权当一个过渡阶段(从REYES到纯RT)的折中方法了。
k是线程数目,n是每个线程处理的数字个数。可以直接替换为外部的IO访问代码,每一个线程读取外部待排序数据的索引,写出各自排序后的结果,主线程再来读取所有的文件做一次归并输出即可。这里用了Python的heapq,C++的话直接就是std::priority_queue,也非常方便。
import heapq
import random
import threading # k threads
k = # Generate n numbers for each thread to process.
n = def GenerateNumbers(a, b):
numbers = []
for i in xrange(n):
numbers.append(random.randint(a, b))
return numbers class SortingThread(threading.Thread):
def __init__(self):
super(SortingThread, self).__init__()
self.numbers = GenerateNumbers(, ) def run(self):
self.numbers.sort() if __name__ == '__main__':
# Parallel sort with multiple threads.
#
sortingThreads = []
for i in xrange(k):
sortingThreads.append(SortingThread())
for i in xrange(k):
sortingThreads[i].start()
for i in xrange(k):
sortingThreads[i].join()
print i , sortingThreads[i].numbers # Use heap to output the final sorted list.
# We fill the heap with several numbers firstly
#
theHeap = []
for i in xrange():
for t in xrange(len(sortingThreads)):
number = sortingThreads[t].numbers[i]
heapq.heappush(theHeap, number) sortedNumbers = [] # In the while loop, people may replace the code by reading file routines.
# Since IO is always the bottleneck, so single thread should be enough.
p =
while (len(theHeap) > ):
if p < len(sortingThreads[t].numbers):
for t in xrange(len(sortingThreads)):
number = sortingThreads[t].numbers[p]
heapq.heappush(theHeap, number)
p +=
sortedNumbers.append(heapq.heappop(theHeap)) # Done
print sortedNumbers
Simplest Python K-Way Merging Sort|最简单的Python k路归并排序的更多相关文章
- 【Python学习笔记三】一个简单的python爬虫
这里写爬虫用的requests插件 1.一般那3.x版本的python安装后都带有相应的安装文件,目录在python安装目录的Scripts中,如下: 2.将scripts的目录配置到环境变量pa ...
- python自学笔记(一)简单了解python
脚本解释型语言的内部机制 python先将脚本编译成字节码文件(pyc,pyo) python虚拟机解释并运行字节码文件 编译型语言的内部机制 先将源代码编译成机器码(机器能读懂的代码),生成可执行文 ...
- 完成一段简单的Python程序,使用函数实现用来判断输入数是偶数还是奇数
#!/bin/usr/env python#coding=utf-8'''完成一段简单的Python程序,使用函数实现用来判断偶数和奇数'''def number_deal(a): if a%2==0 ...
- 完成一段简单的Python程序,用于实现一个简单的加减乘除计算器功能
#!/bin/usr/env python#coding=utf-8'''完成一段简单的Python程序,用于实现一个简单的加减乘除计算器功能'''try: a=int(raw_input(" ...
- python 列表排序方法sort、sorted技巧篇
Python list内置sort()方法用来排序,也可以用python内置的全局sorted()方法来对可迭代的序列排序生成新的序列. 1)排序基础 简单的升序排序是非常容易的.只需要调用sorte ...
- 数据结构 - 归并排序(merging sort)
归并排序(merging sort): 包含2-路归并排序, 把数组拆分成两段, 使用递归, 将两个有序表合成一个新的有序表. 归并排序(merge sort)的时间复杂度是O(nlogn), 实际效 ...
- python中List的sort方法的用法
python列表排序 简单记一下python中List的sort方法(或者sorted内建函数)的用法. 关键字: python列表排序 python字典排序 sorted List的元素可以是各种东 ...
- 【转】python中List的sort方法(或者sorted内建函数)的用法
原始出处:http://gaopenghigh.iteye.com/blog/1483864 python列表排序 简单记一下python中List的sort方法(或者sorted内建函数)的用法. ...
- FZU 1919 -- K-way Merging sort(记忆化搜索)
题目链接 Problem Description As we all known, merge sort is an O(nlogn) comparison-based sorting algorit ...
随机推荐
- webpack vue-loader was used without the corresponding plugin. Make sure to include VueLoaderPlugin
当我们出现以下报错! 解决方案: // webpack配置文件 const path = require('path'); const htmlWebpackPlugin = require('htm ...
- vue 自学笔记记录
vue 自学笔记(一): 知识内容: 安装vue ,创建vue实例,安装第一个组件,单项数据流 https://www.cnblogs.com/baili-luoyun/p/10763163.htm ...
- 【spring boot】idea下springboot打包成jar包和war包,并且可以在外部tomcat下运行访问到(转)
转自:https://www.cnblogs.com/sxdcgaq8080/p/7727249.html 接着上一章走呗:http://www.cnblogs.com/sxdcgaq8080/p ...
- 关于sql和MySQL的语句执行顺序(必看!!!)
今天遇到一个问题就是mysql中insert into 和update以及delete语句中能使用as别名吗?目前还在查看,但是在查阅资料时发现了一些有益的知识,给大家分享一下,就是关于sql以及My ...
- GODOT 3.0 开发进度汇报 #7
由于原文采取了记流水账的方式,觉得没有必要照直翻译了,就只选取了其中的主要信息. GDNative C++ 语言绑定 进行了重写以便Godot更好的生成和处理脚本. D 语言绑定 也正在积极开发中. ...
- springboot 与 shiro 整合 (简洁版)
前言: 网上有很多springboot 与 shiro 整合的资料,有些确实写得很好, 对学习shiro和springboot 都有很大的帮助. 有些朋友比较省事, 直接转发或者复制粘贴.但是没有经过 ...
- Docker 构建映像
.用docker commit构建映像 .docker run -i -t centos /bin/bash //启动一个容器,启动后默认进入该窗口的bash进程 .yum install -y ep ...
- go基础系列:简介
1.Go简介 Go语言是编译型.静态类型的类C的语言,并带有GC(垃圾收集器,garbage collection).这意味着什么? 另外,Go是一种非常严格的语言,它几乎总是要求我们"以标 ...
- backbond Model实现
backbond中的M,指的是模型,即存放数据以及数据相关逻辑的单位.在分析其结构之前,先看一下其调用过程. <script> (function ($) { World = Backbo ...
- 跨域学习笔记1--跨域调用webapi
在做Web开发中,常常会遇到跨域的问题,到目前为止,已经有非常多的跨域解决方案. 通过自己的研究以及在网上看了一些大神的博客,写了一个Demo 首先新建一个webapi的程序,如下图所示: 由于微软已 ...