local merge
This commit is contained in:
Harshil Darji 2016-09-06 17:54:31 +05:30
commit e1366ff292
5 changed files with 19 additions and 24 deletions

View File

@ -13,7 +13,7 @@ from __future__ import print_function
def linear_search(sequence, target): def linear_search(sequence, target):
"""Pure implementation of binary search algorithm in Python """Pure implementation of linear search algorithm in Python
:param sequence: some sorted collection with comparable items :param sequence: some sorted collection with comparable items
:param target: item value to search :param target: item value to search

View File

@ -30,7 +30,7 @@ def bubble_sort(collection):
[-45, -5, -2] [-45, -5, -2]
""" """
length = len(collection) length = len(collection)
for i in range(length): for i in range(length-1):
for j in range(length-1): for j in range(length-1):
if collection[j] > collection[j+1]: if collection[j] > collection[j+1]:
collection[j], collection[j+1] = collection[j+1], collection[j] collection[j], collection[j+1] = collection[j+1], collection[j]

View File

@ -7,7 +7,7 @@ or
python3 -m doctest -v heap_sort.py python3 -m doctest -v heap_sort.py
For manual testing run: For manual testing run:
python insertion_sort.py python heap_sort.py
''' '''
from __future__ import print_function from __future__ import print_function
@ -46,7 +46,7 @@ def heap_sort(unsorted):
n = len(unsorted) n = len(unsorted)
for i in range(n//2 - 1, -1, -1): for i in range(n//2 - 1, -1, -1):
heapify(unsorted, i, n) heapify(unsorted, i, n)
for i in range(n - 1, -1, -1): for i in range(n - 1, 0, -1):
unsorted[0], unsorted[i] = unsorted[i], unsorted[0] unsorted[0], unsorted[i] = unsorted[i], unsorted[0]
heapify(unsorted, 0, i) heapify(unsorted, 0, i)
return unsorted return unsorted

View File

@ -29,14 +29,10 @@ def insertion_sort(collection):
>>> insertion_sort([-2, -5, -45]) >>> insertion_sort([-2, -5, -45])
[-45, -5, -2] [-45, -5, -2]
""" """
length = len(collection) for index in range(1, len(collection)):
for i in range(length): while 0 < index and collection[index] < collection[index-1]:
current_item = collection[i] collection[index], collection[index-1] = collection[index-1], collection[index]
j = i - 1 index -= 1
while j >= 0 and current_item < collection[j]:
collection[j+1] = collection[j]
j -= 1
collection[j+1] = current_item
return collection return collection

View File

@ -35,21 +35,20 @@ def quick_sort(collection):
>>> quick_sort([-2, -5, -45]) >>> quick_sort([-2, -5, -45])
[-45, -5, -2] [-45, -5, -2]
""" """
if len(collection) <= 1:
return collection
less = [] less = []
equal = [] equal = []
greater = [] greater = []
if len(collection) > 1: pivot = collection[0]
pivot = collection[0] for x in collection:
for x in collection: if x < pivot:
if x < pivot: less.append(x)
less.append(x) elif x == pivot:
if x == pivot: equal.append(x)
equal.append(x) else:
if x > pivot: greater.append(x)
greater.append(x) return quick_sort(less) + equal + quick_sort(greater)
return quick_sort(less) + equal + quick_sort(greater)
else:
return collection
if __name__ == '__main__': if __name__ == '__main__':