QuickSort again
2024-08-31 12:59:34
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?
最新文章
- Git同步原始仓库到Fork仓库中
- Linux的NTP配置总结
- 写日志 log 到文件夹
- char wchar 互转 多字符 宽字符 的N种方式
- css小知识之伪元素
- centos 7.0最小化安装 查看yum 所有安装的软件包~
- 客户端请求、服务器响应及其HTTP状态码
- ORACLE在存储过程中记录日志的处理包
- EF6多线程与分库架构设计之Repository
- jqueryui sortable拖拽后保存位置
- vxworks for x86读取bios时间的解决方法
- 15、TypeScript-函数
- 开发第一个flutter程序 hello world
- AtCoder Grand Contest 031 简要题解
- AOP 如果被代理对象的方法设置了参数 而代理对象的前置方法没有设置参数 则无法拦截到
- [Spring Boot] Singleton and Prototype
- android图片等比例缩放 填充屏幕
- CAN网要不要共地?
- iOS开发之JSONKit
- 在VS2015中用C++编写可被其它语言调用的动态库DLL
热门文章
- C语言基础(16)-指针
- 分分钟学会一门语言之Python篇
- mock实例方法
- java中的static方法和实例方法区别
- flume 中的 hdfs sink round 和roll
- Java并发编程(十三)在现有的线程安全类中添加功能
- csla 与高cpu
- Emoji表情图标在iOS与PHP之间通信及MySQL存储
- [已解决]windows下python3.x与python2.7共存版本pip使用报错问题
- Spring4 MVC+Hibernate4 Many-to-many连接表+MySQL+Maven实例