This commit is contained in:
bt3gl 2023-07-30 21:40:09 -07:00
parent 1d44d182e2
commit a85ed914d3
320 changed files with 0 additions and 0 deletions

37
sorting/README.md Normal file
View file

@ -0,0 +1,37 @@
## sorting and searching
<br>
* **inversions** in a sequence is a pair of elements that are out of order with respect to the ordering relation. a sorting algorithm is a sequence of operations that reduces inversions to zero.
* a **topological sort** of a diretect graph is a way of ordering the list of nodes such that if `(a, b)` is a edge of the graph, then `a` appears before `b`. it does not work if a graph has cycles or is not directed.
<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
```

View 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)}')