mirror of
https://github.com/TheAlgorithms/Python.git
synced 2026-03-13 09:50:19 +08:00
PEP style and tree traversals
This commit is contained in:
@@ -11,6 +11,7 @@ python bogosort.py
|
||||
from __future__ import print_function
|
||||
import random
|
||||
|
||||
|
||||
def bogosort(collection):
|
||||
"""Pure implementation of the bogosort algorithm in Python
|
||||
:param collection: some mutable ordered collection with heterogeneous
|
||||
@@ -28,13 +29,13 @@ def bogosort(collection):
|
||||
def isSorted(collection):
|
||||
if len(collection) < 2:
|
||||
return True
|
||||
for i in range(len(collection)-1):
|
||||
if collection[i] > collection[i+1]:
|
||||
for i in range(len(collection) - 1):
|
||||
if collection[i] > collection[i + 1]:
|
||||
return False
|
||||
return True
|
||||
|
||||
while not isSorted(collection):
|
||||
random.shuffle(collection)
|
||||
random.shuffle(collection)
|
||||
return collection
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
@@ -9,6 +9,7 @@ python3 -m doctest -v bubble_sort.py
|
||||
For manual testing run:
|
||||
python bubble_sort.py
|
||||
"""
|
||||
|
||||
from __future__ import print_function
|
||||
|
||||
|
||||
|
||||
@@ -12,6 +12,7 @@ python heap_sort.py
|
||||
|
||||
from __future__ import print_function
|
||||
|
||||
|
||||
def heapify(unsorted, index, heap_size):
|
||||
largest = index
|
||||
left_index = 2 * index + 1
|
||||
@@ -26,6 +27,7 @@ def heapify(unsorted, index, heap_size):
|
||||
unsorted[largest], unsorted[index] = unsorted[index], unsorted[largest]
|
||||
heapify(unsorted, largest, heap_size)
|
||||
|
||||
|
||||
def heap_sort(unsorted):
|
||||
'''
|
||||
Pure implementation of the heap sort algorithm in Python
|
||||
@@ -44,7 +46,7 @@ def heap_sort(unsorted):
|
||||
[-45, -5, -2]
|
||||
'''
|
||||
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)
|
||||
for i in range(n - 1, 0, -1):
|
||||
unsorted[0], unsorted[i] = unsorted[i], unsorted[0]
|
||||
|
||||
@@ -30,8 +30,9 @@ def insertion_sort(collection):
|
||||
[-45, -5, -2]
|
||||
"""
|
||||
for index in range(1, len(collection)):
|
||||
while 0 < index and collection[index] < collection[index-1]:
|
||||
collection[index], collection[index-1] = collection[index-1], collection[index]
|
||||
while 0 < index and collection[index] < collection[index - 1]:
|
||||
collection[index], collection[
|
||||
index - 1] = collection[index - 1], collection[index]
|
||||
index -= 1
|
||||
|
||||
return collection
|
||||
|
||||
@@ -17,7 +17,7 @@ def selection_sort(collection):
|
||||
:param collection: some mutable ordered collection with heterogeneous
|
||||
comparable items inside
|
||||
:return: the same collection ordered by ascending
|
||||
|
||||
|
||||
|
||||
Examples:
|
||||
>>> selection_sort([0, 5, 3, 2, 2])
|
||||
@@ -29,7 +29,7 @@ def selection_sort(collection):
|
||||
>>> selection_sort([-2, -5, -45])
|
||||
[-45, -5, -2]
|
||||
"""
|
||||
|
||||
|
||||
length = len(collection)
|
||||
for i in range(length):
|
||||
least = i
|
||||
|
||||
@@ -17,31 +17,30 @@ def shell_sort(collection):
|
||||
:param collection: Some mutable ordered collection with heterogeneous
|
||||
comparable items inside
|
||||
:return: the same collection ordered by ascending
|
||||
|
||||
|
||||
>>> shell_sort([0, 5, 3, 2, 2])
|
||||
[0, 2, 2, 3, 5]
|
||||
|
||||
>>> shell_sort([])
|
||||
[]
|
||||
|
||||
>>> shell_sort([-2, -5, -45])
|
||||
|
||||
>>> shell_sort([-2, -5, -45])
|
||||
[-45, -5, -2]
|
||||
"""
|
||||
# Marcin Ciura's gap sequence
|
||||
gaps = [701, 301, 132, 57, 23, 10, 4, 1]
|
||||
|
||||
|
||||
for gap in gaps:
|
||||
i = gap
|
||||
while i < len(collection):
|
||||
temp = collection[i]
|
||||
j = i
|
||||
while j >= gap and collection[j-gap] > temp:
|
||||
while j >= gap and collection[j - gap] > temp:
|
||||
collection[j] = collection[j - gap]
|
||||
j -= gap
|
||||
collection[j] = temp
|
||||
i += 1
|
||||
|
||||
|
||||
|
||||
return collection
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
Reference in New Issue
Block a user