快速排序与Python

编程入门 行业动态 更新时间:2024-10-27 12:33:54
本文介绍了快速排序与Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我完全新的Python,我想在这上面实现快速排序。 可能有人请帮助我完成我的code?

我不知道如何连接三个数组进行打印。

高清排序(数组= [12,4,5,6,7,3,1,15]):     少= []     等于= []     更大= []     如果len(阵列)> 1:         透视=阵列[0]         对于x数组:             如果x<枢:                 less.append(X)             如果x ==支点:                 equal.append(X)             如果x>枢:                 greater.append(X)             排序(少)             排序(支点)             排序(大于)

解决方案

高清排序(数组= [12,4,5,6,7,3,1,15]):     少= []     等于= []     更大= []     如果len(阵列)> 1:         透视=阵列[0]         对于x数组:             如果x<枢:                 less.append(X)             如果x ==支点:                 equal.append(X)             如果x>枢:                 greater.append(X)         #不要忘了返回的东西!         返回排序(少)+平等+排序(较大)#只需使用+运算符连接列表     要平等#注意^^^^^没有支点     其他:#你需要喊得部分在递归的终点 - 当你只有在你的数组的一个元素,就返回数组。         返回数组

I am totally new to python and I am trying to implement quick sort in it. Could someone please help me complete my code?

I do not know how to concatenate the three arrays and printing them.

def sort(array=[12,4,5,6,7,3,1,15]): less = [] equal = [] greater = [] if len(array) > 1: pivot = array[0] for x in array: if x < pivot: less.append(x) if x == pivot: equal.append(x) if x > pivot: greater.append(x) sort(less) sort(pivot) sort(greater)

解决方案

def sort(array=[12,4,5,6,7,3,1,15]): less = [] equal = [] greater = [] if len(array) > 1: pivot = array[0] for x in array: if x < pivot: less.append(x) if x == pivot: equal.append(x) if x > pivot: greater.append(x) # Don't forget to return something! return sort(less)+equal+sort(greater) # Just use the + operator to join lists # Note that you want equal ^^^^^ not pivot else: # You need to hande the part at the end of the recursion - when you only have one element in your array, just return the array. return array

更多推荐

快速排序与Python

本文发布于:2023-11-28 22:35:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1644209.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:快速   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!