Skip to content

Instantly share code, notes, and snippets.

@mekicha
Last active August 7, 2018 06:55
Show Gist options
  • Save mekicha/7ad490e0a2191b8184b9ff2fbb4cc4d3 to your computer and use it in GitHub Desktop.
Save mekicha/7ad490e0a2191b8184b9ff2fbb4cc4d3 to your computer and use it in GitHub Desktop.
def qsort(get_list):
if len(get_list) < 2:
return get_list
pivot = get_list.pop()
left = list(filter(lambda x: x <= pivot, get_list))
right = list(filter(lambda x: x > pivot, get_list))
return qsort(left) + [pivot] + qsort(right)
sample_list=[1,65,7,8,32,45,67,90,100,201,12,56,73,2]
print(qsort(sample_list))
def merge_sort(x):
if len(x) < 2:return x
result,mid = [],int(len(x)/2)
y = merge_sort(x[:mid])
z = merge_sort(x[mid:])
while (len(y) > 0) and (len(z) > 0):
if y[0] > z[0]:result.append(z.pop(0))
else:result.append(y.pop(0))
result.extend(y+z)
return result
def bubble(arr):
l = len(arr)
for a in range(l):
for b in range(l-1):
if (arr[a] < arr[b]):
arr[a], arr[b] = arr[b], arr[a]
return arr
qsort_url = 'https://stackoverflow.com/questions/18262306/quicksort-with-python'
mergesort_url = 'https://stackoverflow.com/questions/18761766/mergesort-python'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment