简单易懂的快速排序

news/2024/5/19 22:35:40 标签: 排序算法, 快速排序

快速排序

基于:分治法,如同归并排序,在归并的基础上增加了一个排序的功能(根据中值把序列分为两半),并在子序列中进行分而治之,也可以用二叉递归树来模拟,
主要思想:使用一个简单的递归方法将序列 S 排序,通过分治法把S分解为子序列,递归于每一个子序列进行快速排序,然后合并这些子序列
主要分为3个步骤 :分解,递归,合并

一)分解

从S中选择一个特点的元素x,作为序列的中值,将S中的所有元素分解为

left_mark 储存所有小于中值x的元素
equal_mark 储存所有等于中值x的元素
right_mark储存所有大于中值x的元素 如果
equal_mark只有一个元素就是-基准值自己x

如果对中值不想有额外的开销,可以选取S序列只能的第一或者最后一个进行利用

如下代码


def partition(alist,first,last):
	'''选取中值点'''
	pivotvalue = alist[first]        #中值

	leftmark = first+1   #左有初始值
	rightmark = last
	print('中值,左右初始值',pivotvalue,leftmark,rightmark)

	done = False
	while not done:

		while leftmark <= rightmark and alist[leftmark] <= pivotvalue:
			#左标向右移动,当中值小于数据项时,停止
			leftmark = leftmark + 1

		while alist[rightmark] >= pivotvalue and rightmark >= leftmark:
			# 右标向左移动,当中值大于数据项时,停止
			rightmark = rightmark -1

		if rightmark < leftmark:
			'''两值交错就结束移动'''
			done = True
		else:
			#左右值交换
			temp = alist[leftmark]
			alist[leftmark] = alist[rightmark]
			alist[rightmark] = temp

二) 解决子问题 :递归排序left_mark序列和right_mark序列


def quickSort(alist,first,last):
	if first<last:   #基本结束条件

		splitpoint = partition(alist,first,last)  #分裂
		print('splitpoint:',splitpoint)
		print('alist:',alist)
		quickSort(alist,first,splitpoint-1) #函数内部通过把列表进行了分裂处理
		print('left:',alist,splitpoint)
		quickSort(alist,splitpoint+1,last) #
		print('right:',alist,splitpoint)

三)合并子序列

完整代码:


def quickSort(alist,first,last):
	if first<last:   #基本结束条件

		splitpoint = partition(alist,first,last)  #分裂
		print('splitpoint:',splitpoint)
		print('alist:',alist)
		quickSort(alist,first,splitpoint-1)
		print('left:',alist,splitpoint)
		quickSort(alist,splitpoint+1,last)
		print('right:',alist,splitpoint)


def partition(alist,first,last):
	'''选取中值点'''
	pivotvalue = alist[first]        #中值

	leftmark = first+1   #左有初始值
	rightmark = last
	print('中值,左右初始值',pivotvalue,leftmark,rightmark)

	done = False
	while not done:

		while leftmark <= rightmark and alist[leftmark] <= pivotvalue:
			#左标向右移动,当中值小于数据项时,停止
			leftmark = leftmark + 1

		while alist[rightmark] >= pivotvalue and rightmark >= leftmark:
			# 右标向左移动,当中值大于数据项时,停止
			rightmark = rightmark -1

		if rightmark < leftmark:
			'''两值交错就结束移动'''
			done = True
		else:
			#左右值交换
			temp = alist[leftmark]
			alist[leftmark] = alist[rightmark]
			alist[rightmark] = temp
	#中值处理
	temp = alist[first]
	print('temp:',temp)
	alist[first] = alist[rightmark]  
	print('alist[first]:',alist[first])
	alist[rightmark] = temp
	print('alist[rightmark]:',alist[rightmark])


	return rightmark

alist = [54,26,93,17,77,31,44,55,20]
quickSort(alist,0,len(alist)-1)
print(alist)

如同归并排序一样,排序树的高度是O(log n),所有快速排序的时间就是O(n log n),
如果分裂能把数据表分为相同的两半,那么就是O(log n),
最坏的情况下,选取中值是序列中的最大的,那么他的运行时间就是O(n**2)

因为快速排序方法划分步骤的目的是吧序列,分解得足够平衡,我们还可以为它引速随机数为基准值。


http://www.niftyadmin.cn/n/1095724.html

相关文章

spring接口用户超时验证

目前APP调用java后端接口的开发屡见不鲜。然而Http协议是短连接&#xff0c;APP和java后端不可能在一个容器中运行&#xff0c;所以就造成了会话无法记录。 通过服务端生成token&#xff0c;服务端把token放入容器中&#xff0c;APP访问接口传入token到服务端验证是否过期&…

[转]IE和FireFox中JS兼容之event .

转载于&#xff1a;http://blog.csdn.net/jiachunfeng/article/details/6448186 http://justcoding.iteye.com/blog/587876 event对象 IE 中可以直接使用 event 对象&#xff0c;而 FF 中则不可以&#xff0c;解决方法之一如下&#xff1a;var theEvent window.event || argu…

sql语句的基本使用

sql语句的基本操作 文章目录sql语句的基本操作一、了解sql二、基本操作1.增删改查1,数据表2,数据三、常用关键字1. as关键字2. distinct关键字四、sql 的运行过程这里以mysql为主&#xff0c;使用的是类似 MySQL、Oracle 这种的数据库管理系统&#xff0c;实际上这些数据库管理…

尼康D7100是否是非全画幅单反最佳选择?

尼康D7000是一款长期热卖的机型&#xff0c;而作为升级型号&#xff0c;尼康D7100自然也受到广泛的关注。相对于对手品牌小打小闹的升级&#xff0c;D7100显得更有诚意。2410万像素无低通滤镜APS-C CMOS、51点自动对焦系统、与D800/D300s同级的防尘机身&#xff0c;还有各种相对…

r.js 配置文件 example.build.js 不完整注释

/** This is an example build file that demonstrates how to use the build system for* require.js.** THIS BUILD FILE WILL NOT WORK. It is referencing paths that probably* do not exist on your machine. Just use it as a guide.***/({// app顶级目录&#xff0c;非…

第七天

单向链表 <>链表是一种在呢存空间中不连续的数据结构&#xff0c;因此不可以用下标或者迭代器访问&#xff0c;在访问数据时比较麻烦&#xff1b;链表在内存中不连续&#xff0c;所以可以有效地解决内存碎片化的问题&#xff0c;同时他相比传统的数组来说更加灵活。 <…

vs 2019安装使用教程

c最流行之Visual Studio一、Visual Studio的介绍简介二、下载和安装1、下载完成后&#xff0c;运行我们继续&#xff0c;等待完成2、选择适合的版本进行安装3、这里选择开发组件&#xff0c;根据自己需要的环境进行选择&#xff08;这里以c进行实例&#xff09;二、基本使用步骤…

jQuery性能最佳实践

一、使用最新的jquery版本二、用对选择器1、最快选择器&#xff1a;id选择器和标签选择器 $(#id),$(form),$(p) 2、较慢选择器&#xff1a;class选择器1 $(.className) 3、最慢选择器&#xff1a;伪类选择器和属性选择器1 $(:hidden),$([attributevalue]) 三、理解子元素和…