Algorithm: quick sort implemented in python 算法导论 快速排序
import random def partition(A, lo, hi):
pivot_index = random.randint(lo, hi)
pivot = A[pivot_index]
A[pivot_index], A[hi] = A[hi], A[pivot_index]
store_index = lo
for i in range(lo, hi):
if A[i] < pivot:
A[i], A[store_index] = A[store_index], A[i]
store_index = store_index + 1
A[store_index], A[hi] = A[hi], A[store_index]
return store_index def quicksort(A, lo, hi):
if lo < hi:
p = partition(A, lo, hi)
quicksort(A, lo, p - 1)
quicksort(A, p + 1, hi) if __name__ == '__main__':
l = [random.randint(1, 113) for i in range(18)]
print l
quicksort(l, 0, len(l) - 1)
print l
Algorithm: quick sort implemented in python 算法导论 快速排序的更多相关文章
- algorithm: heap sort in python 算法导论 堆排序
An Python implementation of heap-sort based on the detailed algorithm description in Introduction to ...
- python算法之快速排序
快速排序 快速排序(英语:Quicksort),又称划分交换排序(partition-exchange sort),通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所 ...
- [算法导论]quicksort algorithm @ Python
算法导论上面快速排序的实现. 代码: def partition(array, left, right): i = left-1 for j in range(left, right): if arr ...
- [Algorithm] 如何正确撸<算法导论>CLRS
其实算法本身不难,第一遍可以只看伪代码和算法思路.如果想进一步理解的话,第三章那些标记法是非常重要的,就算要花费大量时间才能理解,也不要马马虎虎略过.因为以后的每一章,讲完算法就是这样的分析,精通的话 ...
- 基础排序算法之快速排序(Quick Sort)
快速排序(Quick Sort)同样是使用了分治法的思想,相比于其他的排序方法,它所用到的空间更少,因为其可以实现原地排序.同时如果随机选取中心枢(pivot),它也是一个随机算法.最重要的是,快速排 ...
- What does Quick Sort look like in Python?
Let's talk about something funny at first. Have you ever implemented the Quick Sort algorithm all by ...
- 2.1 insertion sort 《算法导论》答案
2.1 insertion sort <算法导论>答案 答案索引帖 2.1-1 Using Figure 2.2 as a model, illustrate the operation ...
- [算法]——快速排序(Quick Sort)
顾名思义,快速排序(quick sort)速度十分快,时间复杂度为O(nlogn).虽然从此角度讲,也有很多排序算法如归并排序.堆排序甚至希尔排序等,都能达到如此快速,但是快速排序使用更加广泛,以至于 ...
- 快速排序算法 quick sort的理解
最近做了一下算法的一些练习,感觉基础薄弱了,只是用一些已经有的东西来完成练习如quickSort(c++使用的时候是sort(起始位置,终止位置,比较函数),这个需要加头文件),但是不知道怎么推出来, ...
随机推荐
- SpringMVC注解@RequestParam(转)
鸣谢:http://shawnccx.iteye.com/blog/730239 -------------------------------------------------- 在SpringM ...
- sqlmap映射继承机制及映射字段顺序与SQL查询字段顺序无关
<typeAlias alias="TblSpPartsinfo" type="com.bn.car.biz.supply.dao.po.PartsInfoPO&q ...
- poj 3318 Matrix Multiplication 随机化算法
方法1:暴力法 矩阵乘法+优化可以卡时间过的. 方法2:随机化 随机构造向量x[1..n],则有xAB=xC;这样可以将小运算至O(n^2). 代码如下: #include<iostream&g ...
- IOS 系统API---NSJSONSerialization四个枚举什么意思
IOS 系统API---NSJSONSerialization四个枚举什么意思 NSJSONReadingMutableContainers:返回可变容器,NSMutableDictionary或NS ...
- cocos2d-x 常规库的图文件配置
LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS) LOCAL_MODULE := cocos_lua_static LOCAL_MODULE_FILE ...
- thinkphp多表关联并且分页
$db_prefix = C('DB_PREFIX'); $Model = new Model(); $data = $Model->table("{$db_prefix}ordern ...
- linux配置防火墙详细步骤(iptables命令使用方法)
通过本教程操作,请确认您能使用linux本机.如果您使用的是ssh远程,而又不能直接操作本机,那么建议您慎重,慎重,再慎重! 通过iptables我们可以为我们的Linux服务器配置有动态的防火墙,能 ...
- IntelliJ IDEA 添加jar包的三种方式
一.直接复制:(不推荐)方法:直接将硬盘上的jar包复制粘贴到项目的lib目录下.
- POJ3080——Blue Jeans(暴力+字符串匹配)
Blue Jeans DescriptionThe Genographic Project is a research partnership between IBM and The National ...
- HASH暴力破解工具-Hashcat
乌云网看到一篇文章讲述hashcat的使用简介(戳这里),对使用字典破解MD5内容 简单在kali上尝试了一下. (1)首先查看了下hashcat的帮助文档,简单截取了其中的部分常用说明. hashc ...