mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-01-31 14:43:43 +00:00
Merge branch 'master' of https://github.com/TheAlgorithms/Python
local merge
This commit is contained in:
commit
e1366ff292
|
@ -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
|
||||||
|
|
|
@ -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]
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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
|
||||||
|
|
||||||
|
|
|
@ -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)
|
||||||
if x == pivot:
|
elif x == pivot:
|
||||||
equal.append(x)
|
equal.append(x)
|
||||||
if x > pivot:
|
else:
|
||||||
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__':
|
||||||
|
|
Loading…
Reference in New Issue
Block a user