mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-05-22 08:21:12 -04:00
Add some cool queue, stacks, strings, math, bit manipulation examples (#35)
This commit is contained in:
parent
f3ee2cdf52
commit
0f455a0322
24 changed files with 932 additions and 13 deletions
|
@ -0,0 +1,29 @@
|
|||
## sorting and searching
|
||||
|
||||
<br>
|
||||
|
||||
### `sorting_algorithms.py`
|
||||
|
||||
<br>
|
||||
|
||||
```python
|
||||
> python3 sorting_algorithms.py
|
||||
|
||||
|
||||
Array: [3, 5, 1, 2, 10, 6]
|
||||
Testing merge sort: [1, 2, 3, 5, 6, 10]
|
||||
Testing quick sort: [1, 2, 3, 5, 6, 10]
|
||||
```
|
||||
|
||||
<br>
|
||||
|
||||
### `binary_search.py`
|
||||
|
||||
<br>
|
||||
|
||||
```python
|
||||
> python binary_search.py
|
||||
Recursive: 6
|
||||
Iterative: 6
|
||||
```
|
||||
|
68
sorting_and_searching/binary_search.py
Normal file
68
sorting_and_searching/binary_search.py
Normal file
|
@ -0,0 +1,68 @@
|
|||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
# author: bt3gl
|
||||
|
||||
def binary_search_recursive(array, item, higher=None, lower=0):
|
||||
|
||||
higher = higher or len(array)
|
||||
if higher < lower:
|
||||
return False
|
||||
|
||||
mid = (higher + lower)//2
|
||||
if item == array[mid]:
|
||||
return mid
|
||||
elif item < array[mid]:
|
||||
return binary_search_recursive(array, item, higher=mid-1, lower=lower)
|
||||
else:
|
||||
return binary_search_recursive(array, item, higher=higher, lower=mid+1)
|
||||
|
||||
|
||||
def binary_search_iterative(array, item):
|
||||
lower, higher = 0, len(array)
|
||||
|
||||
while lower < higher:
|
||||
mid = (higher+lower)//2
|
||||
if array[mid] == item:
|
||||
return mid
|
||||
elif array[mid] > item:
|
||||
higher = mid
|
||||
else:
|
||||
lower=mid+1
|
||||
return False
|
||||
|
||||
def binary_search_matrix(matrix, item, lower=0, higher=None):
|
||||
""" Binary search in a matrix """
|
||||
|
||||
if not matrix:
|
||||
return None
|
||||
|
||||
rows = len(matrix)
|
||||
cols = len(matrix[0])
|
||||
higher = higher or rows*cols
|
||||
|
||||
if higher > lower:
|
||||
mid = (higher + lower)//2
|
||||
row = mid//cols
|
||||
col = mid%cols
|
||||
item = matrix[row][col]
|
||||
|
||||
if item == item:
|
||||
return row, col
|
||||
elif item < item:
|
||||
return binary_search_matrix(matrix, item, lower, mid-1)
|
||||
else:
|
||||
return binary_search_matrix(matrix, item, mid+1, higher)
|
||||
|
||||
return None
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
array = [2, 3, 5, 6, 8, 10, 15, 23]
|
||||
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
|
||||
item = 15
|
||||
|
||||
print('Recursive: ', binary_search_recursive(array, item))
|
||||
print('Iterative: ', binary_search_iterative(array, item))
|
||||
print('Matrix: ', binary_search_matrix(matrix, item))
|
||||
|
64
sorting_and_searching/sorting_algorithms.py
Normal file
64
sorting_and_searching/sorting_algorithms.py
Normal file
|
@ -0,0 +1,64 @@
|
|||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
# author: bt3gl
|
||||
|
||||
|
||||
def merge_sort(array):
|
||||
"""Sort an array using merge sort"""
|
||||
|
||||
# part 1: recursively divide the array into subarrays
|
||||
if len(array) < 2:
|
||||
return array
|
||||
|
||||
mid = len(array) // 2
|
||||
left = merge_sort(array[:mid])
|
||||
right = merge_sort(array[mid:])
|
||||
|
||||
# part 2: merge the subarrays
|
||||
result = []
|
||||
i, j = 0, 0
|
||||
|
||||
while i < len(left) and j < len(right):
|
||||
if left[i] <= right[j]:
|
||||
result.append(left[i])
|
||||
i +=1
|
||||
else:
|
||||
result.append(right[j])
|
||||
j +=1
|
||||
|
||||
if left[i:]:
|
||||
result.extend(left[i:])
|
||||
if right[j:]:
|
||||
result.extend(right[j:])
|
||||
|
||||
return result
|
||||
|
||||
|
||||
def quick_sort_partition(array):
|
||||
"""Sort an array using quick sort"""
|
||||
|
||||
pivot, array = array[0], array[1:]
|
||||
|
||||
lower = [i for i in array if i <= pivot]
|
||||
higher = [i for i in array if i > pivot]
|
||||
|
||||
return lower, pivot, higher
|
||||
|
||||
|
||||
def quick_sort_divided(array):
|
||||
"""Sort an array using quick sort"""
|
||||
|
||||
if len(array) < 2:
|
||||
return array
|
||||
|
||||
lower, pivot, higher = quick_sort_partition(array)
|
||||
return quick_sort_divided(lower) + [pivot] + quick_sort_divided(higher)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
array = [3, 5, 1, 2, 10, 6]
|
||||
print(f'Array: {array}')
|
||||
|
||||
print(f'Testing merge sort: {merge_sort(array)}')
|
||||
print(f'Testing quick sort: {quick_sort_divided(array)}')
|
Loading…
Add table
Add a link
Reference in a new issue