반응형
나는 완전히 파이썬을 처음 접했고 그것에 퀵 정렬을 구현하려고 노력하고 있습니다. 누군가 내 코드를 완성하도록 도와 주시겠습니까?
세 배열을 연결하고 인쇄하는 방법을 모르겠습니다.
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]):
"""Sort the array by using quicksort."""
less = []
equal = []
greater = []
if len(array) > 1:
pivot = array[0]
for x in array:
if x < pivot:
less.append(x)
elif x == pivot:
equal.append(x)
elif 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 handle the part at the end of the recursion - when you only have one element in your array, just return the array.
return array
참조 페이지 https://stackoverflow.com/questions/18262306
반응형
'파이썬' 카테고리의 다른 글
파이썬 절인 개체를로드 할 수 없습니다. (0) | 2021.01.10 |
---|---|
파이썬 폴더의 모든 파일을 여는 방법은 무엇입니까? (0) | 2021.01.10 |
파이썬 file.write에서 줄 바꿈을 억제합니다. (0) | 2021.01.10 |
파이썬 Python-두 값 사이의 숫자로 목록을 만드시겠습니까? (0) | 2021.01.10 |
파이썬 ImportError : apiclient.discovery라는 모듈이 없습니다. (0) | 2021.01.10 |
댓글