当前位置:网站首页>Quick sort
Quick sort
2022-07-26 06:36:00 【fiveym】
Quick sort ideas :
- Go to an element p( The first element in the list ), Is element homing
- The list is divided into two parts , It's all on the left p Small , On the right, it's more than p Big
- Recursive completion sorting

partition function : Homing function , Calculate the subscript position of the intermediate value
every last partition The time complexity of the function is logn, Altogether n Time
So the time complexity of quick sort is O(nlogn)( At best )
def partition(li, left, right):
tmp = li[left]
while left < right:
while left < right and li[right] >= tmp: # Look for... From the right tmp Small number
right -= 1 # Take a step to the left
li[left] = li[right] # Write the value on the right in the space on the left
print(li, 'right')
while left < right and li[left] <= tmp:
left += 1
li[right] = li[left] # Write the value on the left in the space on the right
print(li, 'left')
li[left] = tmp # After the two values are equal tmp place
return left # return mid Value
def quick_sort(li, left, right):
if left < right: # At least two elements
mid = partition(li, left, right)
quick_sort(li, left, mid-1)
quick_sort(li, mid+1, right)
li=[5,7,4,6,3,1,2,9]
quick_sort(li, 0, len(li)-1)
print(li)
# result
[2, 7, 4, 6, 3, 1, 2, 9, 8] right
[2, 7, 4, 6, 3, 1, 7, 9, 8] left
[2, 1, 4, 6, 3, 1, 7, 9, 8] right
[2, 1, 4, 6, 3, 6, 7, 9, 8] left
[2, 1, 4, 3, 3, 6, 7, 9, 8] right
[2, 1, 4, 3, 3, 6, 7, 9, 8] left
[1, 1, 4, 3, 5, 6, 7, 9, 8] right
[1, 1, 4, 3, 5, 6, 7, 9, 8] left
[1, 2, 3, 3, 5, 6, 7, 9, 8] right
[1, 2, 3, 3, 5, 6, 7, 9, 8] left
[1, 2, 3, 4, 5, 6, 7, 9, 8] right
[1, 2, 3, 4, 5, 6, 7, 9, 8] left
[1, 2, 3, 4, 5, 6, 7, 9, 8] right
[1, 2, 3, 4, 5, 6, 7, 9, 8] left
[1, 2, 3, 4, 5, 6, 7, 8, 8] right
[1, 2, 3, 4, 5, 6, 7, 8, 8] left
[1, 2, 3, 4, 5, 6, 7, 8, 9]
边栏推荐
- Basis of multimodal semantic segmentation
- 【Web3 系列开发教程——创建你的第一个 NFT(4)】NFTs 可以给你带来什么
- Use scanner to get multiple data types from the keyboard
- Advanced C language - archived address book (file)
- 09 eth smart contract
- Go 的通道channel
- [day02_0419] C language multiple choice questions
- [day_070425] Fibonacci series
- 【Day_04 0421】进制转换
- Find the original root
猜你喜欢

『HarmonyOS』探索HarmonyOS应用

『牛客|每日一题』有效括号序列

【pytorch】微调技术

信号处理系统综合设计-求解器函数的设计(连续和离散时间系统)

@ConstructorProperties注解理解以及其对应使用方式

Upgrade appium automation framework to the latest 2.0

Markdown add Emoji expression

C语言进阶——可存档通讯录(文件)

【图像隐藏】基于混合 DWT-HD-SVD 的数字图像水印方法技术附matlab代码
![[day04_0421] C language multiple choice questions](/img/18/42924b5994b385a3cf132742141d88.png)
[day04_0421] C language multiple choice questions
随机推荐
[day03_0420] C language multiple choice questions
『牛客|每日一题』点击消除
Show you the principle of IO multiplexing (select, poll and epoll)
【无标题】
【Day06_0423】C语言选择题
Regular expressions and calling related functions in C language
A tool for quickly switching local host files -- switchhosts
IP day 10 notes - BGP
『牛客|每日一题』逆波兰表达式
【Web3 系列开发教程——创建你的第一个 NFT(4)】NFTs 可以给你带来什么
TPS Motion(CVPR2022)视频生成论文解读
[day_030420] find the longest consecutive number string in the string
[C language] file operation
输入5个学生的记录(每条记录包括学号和成绩), 组成记录数组, 然后按照成绩由高到低的次序输出. 排序方法采用选择排序
PG vacuum auto vacuum
使用Scanner从键盘获取多种数据类型
[1] Basic knowledge of mathematical modeling
BigDecimal变为负数
TCP protocol -- message format, connection establishment, reliable transmission, congestion control
【故障诊断】基于贝叶斯优化支持向量机的轴承故障诊断附matlab代码