Skip to content

Instantly share code, notes, and snippets.

@DongguemYoo
Created June 28, 2020 12:54
Show Gist options
  • Save DongguemYoo/47eadf69b3785ff55f12451a612a463f to your computer and use it in GitHub Desktop.
Save DongguemYoo/47eadf69b3785ff55f12451a612a463f to your computer and use it in GitHub Desktop.
[코드잇] partition python
#파티션 함수 구현!!
# 로직은 이렇다. 일단 피벗을 기준으로 왼쪽은작은것 오른쪽은 큰것을 모으는 로직이다
# 겁나 신기함
# 두 요소의 위치를 바꿔주는 helper function
def swap_elements(my_list, index1, index2):
# 코드를 작성하세요.
tmp = my_list[index1]
my_list[index1] = my_list[index2]
my_list[index2] =tmp
return my_list
# 퀵 정렬에서 사용되는 partition 함수
def partition(my_list, start, end):
# 코드를 작성하세요.
b=0
i=0
p=end
for i in range(0,p):
if my_list[p] < my_list[i]:
pass
else:
swap_elements(my_list,b,i)
b+=1
i+=1
swap_elements(my_list, b, p)
return b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment