arr = [10, 80, 30, 90, 40, 50, 70]
def partition(l, r):
pivot = nums[(left + right) // 2]
while l <= r:
while arr[l] < pivot:
l += 1
while arr[r] > pivot:
r -= 1
if l <= r:
arr[l], arr[r] = arr[r], arr[l]
l, r = l + 1, r - 1
return l
def quickSort(l, r):
if l >= r:
return
pivot = partition(l, r)
quickSort(l, pivot - 1)
quickSort(pivot, r)
quickSort(0, len(arr)-1)
print(arr)
arr = [10, 80, 30, 90, 40, 50, 70]
def partition(start, end):
pivot_idx = start
pivot = arr[start]
while start < end:
while start < len(arr) and arr[start] <= pivot:
start += 1
while arr[end] > pivot:
end -= 1
if start < end:
arr[start], arr[end] = arr[end], arr[start]
arr[end], arr[pivot_idx] = arr[pivot_idx], arr[end]
return end
def quick_sort(start, end):
if start < end:
p = partition(start, end)
quick_sort(start, p - 1)
quick_sort(p + 1, end)
quick_sort(0, len(arr) - 1)
print(arr)
arr = [10, 80, 30, 90, 40, 50, 70]
def partition(start, end):
pivot = arr[end]
i = start - 1
for j in range(start, end):
if arr[j] <= pivot:
i += 1
arr[i], arr[j] = arr[j], arr[i]
arr[i + 1], arr[end] = arr[end], arr[i + 1]
return i + 1
def quickSort(start, end):
if start < end:
pi = partition(start, end)
quickSort(start, pi - 1)
quickSort(pi + 1, end)
quickSort(0, len(arr) - 1)
print(arr)
'컴퓨터공학 > 알고리즘 공부' 카테고리의 다른 글
230. Kth Smallest Element in a BST Leet Code 문제 풀이 (0) | 2019.06.17 |
---|---|
1949. [모의 SW 역량테스트] 등산로 조성 (SwExpertAcademy) 삼성 모의 테스트 (2) | 2019.06.16 |
벡터 Vector C++ (0) | 2019.06.16 |
[BOJ] 13565 침투 알고리즘 스터디 11월 21일 (0) | 2018.11.21 |
[CodeForce] Water The Garden .A 알고리즘 스터디 (0) | 2018.11.21 |