QuickSort again
I wrote a blog about Quick Sort before. Let's talk more about it.
If you don't think that the implementation in the blog mentioned is easy to remember and understand.
You can send your implementation to me by E-mail(lxw.ucas@gmail.com). Now let's look at some other
implementations that are not so amazing:
A new implementation in python is shown here:
def qs(arr):
if not arr:
return []
low = []
high = []
for x in arr[1:]:
if x <= arr[0]:
low.append(x)
else:
high.append(x)
low = qs(low)
high = qs(high)
return low + arr[:1] + high # low + arr[0] + high is NOT OK! --> ERROR PROMPT: 'can only concatenate list(not int) to list'
It seemed not so amazing and easy to understand as the former one. But it's ok as well. : ) Let's look
at another new implementation in python:
def quickSort(arr, start, end):
if start >= end: # EXIT
return
i = start
j = end
target = arr[i]
while i < j:
while arr[i] <= target and i < end:
i += 1
while arr[j] > target and j > start:
j -= 1
if i < j:
arr[i], arr[j] = arr[j], arr[i]
arr[start] = arr[j]
arr[j] = target
quickSort(arr, start, j - 1)
quickSort(arr, j + 1, end)
Now, which way of implementation do you prefer? Do you think the implementation in the blog mentioned
is easy to understand and remember?
QuickSort again的更多相关文章
- quickSort算法导论版实现
本文主要实践一下算法导论上的快排算法,活动活动. 伪代码图来源于 http://www.cnblogs.com/dongkuo/p/4827281.html // imp the quicksort ...
- Javascript算法系列之快速排序(Quicksort)
原文出自: http://www.nczonline.net/blog/2012/11/27/computer-science-in-javascript-quicksort/ https://gis ...
- JavaScript 快速排序(Quicksort)
"快速排序"的思想很简单,整个排序过程只需要三步: (1)在数据集之中,选择一个元素作为"基准"(pivot). (2)所有小于"基准"的元 ...
- QuickSort 快速排序 基于伪代码实现
本文原创,转载请注明地址 http://www.cnblogs.com/baokang/p/4737492.html 伪代码 quicksort(A, lo, hi) if lo < hi p ...
- quicksort
快排.... void quicksort(int *a,int left,int right){ if(left >= right){ return ; } int i = left; int ...
- 随手编程---快速排序(QuickSort)-Java实现
背景 快速排序,是在上世纪60年代,由美国人东尼·霍尔提出的一种排序方法.这种排序方式,在当时已经是非常快的一种排序了.因此在命名上,才将之称为"快速排序".这个算法是二十世纪的七 ...
- 算法实例-C#-快速排序-QuickSort
算法实例 ##排序算法Sort## ### 快速排序QuickSort ### bing搜索结果 http://www.bing.com/knows/search?q=%E5%BF%AB%E9%80% ...
- python Quicksort demo
__author__ = 'student' ''' quicksort step 1, choose one pivot, such as pivot=la[0] step 2, scan the ...
- 63.如何对单链表进行快排?和数组快排的分析与对比[quicksort of array and linked list]
[本文链接] http://www.cnblogs.com/hellogiser/p/quick-sort-of-array-and-linked-list.html [题目] 单链表的特点是:单向. ...
- 这个代码怎么改??Help快速排序 quicksort
#include<stdio.h>int a[101],n;void quicksort(int left,int right){ int i,j,t,temp; if(l ...
随机推荐
- Pmon (LS1B)start.s
loongson ls1b FPGA验证 只有DDR flash UART pmon移植 和原版本相比,DDR controller和ls1b不一致. /* $Id: start.S,v 1.1.1. ...
- Python内置函数之open()
open(file, mode=, encoding=None, errors=None, newline=None, closefd=True, opener=None) 打开一个文件,返回一个对应 ...
- java - day05 - Array
/* 生成随机数组,寻找最大值 */ package day05; public class ArrayGuess { public static void main(String args[]) { ...
- 内核交互--debugfs_转
转载:Linux内核里的DebugFS DebugFS,顾名思义,是一种用于内核调试的虚拟文件系统,内核开发者通过debugfs和用户空间交换数据.类似的虚拟文件系统还有procfs和sysfs等,这 ...
- poj1273 Drainage Ditches Dinic最大流
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 76000 Accepted: 2953 ...
- jq中prop和attr的区别以及各自的使用
在高版本的jquery引入prop方法后,什么时候该用prop?什么时候用attr?它们两个之间有什么区别?这些问题就出现了. 关于它们两个的区别,网上的答案很多.这里谈谈我的心得,我的心得很简单: ...
- 机器学习之猫狗大战,解决image RGB values must be in the 0..1 range.
猫狗大战是比较经典的机器学习案例,前几天体验了一番,来记录一下 1.图片准备 首先是准备训练的图片 链接:https://pan.baidu.com/s/1ht1HIuw 密码:aw9s 2.开始训练 ...
- 安装使用yarn,使用国内镜像加速npm和yarn
安装yarn https://yarnpkg.com/lang/zh-hans/docs/install/ 使用国内镜像加速npm和yarn 1. npm config set registry=ht ...
- redis 底层数据结构 整数集合intset
整数集合是集合键的底层实现之一,当一个集合只包含整数值元素,并且这个集合的元素数量不多时Redis就会使用整数集合作为集合键的底层实现 整数集合是Redis用于保存整数值的集合抽象数据结构,它可以保存 ...
- python redis操作
import redis r = redis.Redis( host='1xx.x24.3xx.x0', #ip, password='xnxnxn&*',#密码 port=6379, #端口 ...