Skip to content

Instantly share code, notes, and snippets.

@anirudhjayaraman
Last active June 19, 2024 23:58
Show Gist options
  • Select an option

  • Save anirudhjayaraman/897ca0d97a249180a48b50d62c87f239 to your computer and use it in GitHub Desktop.

Select an option

Save anirudhjayaraman/897ca0d97a249180a48b50d62c87f239 to your computer and use it in GitHub Desktop.
Python code for the Quick Sort Algorithm
def quicksort(x):
if len(x) == 1 or len(x) == 0:
return x
else:
pivot = x[0]
i = 0
for j in range(len(x)-1):
if x[j+1] < pivot:
x[j+1],x[i+1] = x[i+1], x[j+1]
i += 1
x[0],x[i] = x[i],x[0]
first_part = quicksort(x[:i])
second_part = quicksort(x[i+1:])
first_part.append(x[i])
return first_part + second_part
alist = [54,26,93,17,77,31,44,55,20]
quicksort(alist)
print(alist)
@ykemer

ykemer commented Jun 29, 2019

Copy link
Copy Markdown

quicksort([8, 3, 1, 7, 0, 10, 2]) and it doesn't work

@CcGaviria

CcGaviria commented Jun 30, 2019

Copy link
Copy Markdown

Bueno pues la razón de que no funcione es que no estamos teniendo en cuenta el tipo de dato que comparamos:
The data type is not integer...

def sort(num):
if len(num) < 2:
return num
else:
piv = int(num[0])
less = [x for x in num[1:] if int(x) < piv]
print("less",less)
higher = [x for x in num[1:] if int(x) > piv]
print(higher)
return sort(higher) + [piv] + sort(less)
for y in range(0, input()):
list = raw_input()
alist = list.split()
listOrg = sort(alist)
print(listOrg)

@anandrajB

Copy link
Copy Markdown

simple method

a= [4,7,1,2,3,9,7,0,4,56,3]
for i in range(len(a)):
for j in range(len(a)):
if a[i]<a[j]:
a[i],a[j]=a[j],a[i]
print("The sorted list is ",a)

@RaviPabari

Copy link
Copy Markdown

@pete312 use pypy3 instead of python to see some BIG RUNNING TIME DIFFRENCE

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment