diff --git a/README.md b/README.md index d5d0be9..8b9d292 100644 --- a/README.md +++ b/README.md @@ -8,16 +8,17 @@ A repo for storing Python code and tracking learning progress. All programs are . ├── README.md ├── algorithms -│ ├── binary_search.py # interactive -| ├── bubble_sort.py # visual demo -│ └── quicksort.py +│ ├── binary_search.py +| ├── bubble_sort.py +| ├── quicksort.py +│ └── selection_sort.py ├── asynchronous_programming │ ├── dependency_scheduler.py │ ├── taskgroup_error_handling.py │ └── worker_pipeline.py └── data_structures - ├── doubly_linked_list.py # interactive - └── singly_linked_list.py # interactive + ├── doubly_linked_list.py + └── singly_linked_list.py ``` ### GitHub Actions diff --git a/algorithms/binary_search.py b/algorithms/binary_search.py index dbbbbd1..00f74d5 100644 --- a/algorithms/binary_search.py +++ b/algorithms/binary_search.py @@ -1,8 +1,4 @@ -"""An interactive binary search implementation.""" - - def binary_search(numbers: list[int], target: int) -> int | None: - """Return the index of target in a sorted list, or None if not found.""" left = 0 right = len(numbers) - 1 while left <= right: @@ -16,25 +12,16 @@ def binary_search(numbers: list[int], target: int) -> int | None: return None -def get_integer_input(prompt: str) -> int: - while True: - try: - return int(input(prompt)) - except ValueError: - print("Please enter a valid integer.") - - def main() -> None: numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9] - print("Sorted list:", *numbers) - - target = get_integer_input("Enter item to find: ") - index_found = binary_search(numbers, target) - - if index_found is not None: - print(f"Item was found at index {index_found}.") + target = 7 + print("Sorted list: ", numbers) + print("Target item: ", target) + found = binary_search(numbers, target) + if found is not None: + print(f"- Item {target} was found at index {found}.") else: - print("Item was not found in the list.") + print(f"- Item {target} was not found.") if __name__ == "__main__": diff --git a/algorithms/bubble_sort.py b/algorithms/bubble_sort.py index 625d89c..14146bd 100644 --- a/algorithms/bubble_sort.py +++ b/algorithms/bubble_sort.py @@ -1,23 +1,3 @@ -"""A bubble sort implementation with an animated demo.""" - - -import os -import time - - -def clear_screen() -> None: - os.system("cls" if os.name == "nt" else "clear") - - -def show_step(numbers: list[int], index: int, should_swap: bool) -> None: - clear_screen() - spacing = " " * (3 * index) - print(numbers) - print(spacing + " ^ ^") - print(spacing + " swap" if should_swap else "") - time.sleep(0.5) - - def bubble_sort(numbers: list[int]) -> None: swapped = True while swapped: @@ -27,15 +7,13 @@ def bubble_sort(numbers: list[int]) -> None: if should_swap: numbers[i], numbers[i + 1] = numbers[i + 1], numbers[i] swapped = True - show_step(numbers, i, should_swap) - clear_screen() - print(numbers) - print("List sorted!\n") + return numbers def main() -> None: numbers = [4, 2, 7, 1, 9, 6, 5, 8, 3] - bubble_sort(numbers) + print("Unsorted list:", numbers) + print("Sorted list: ", bubble_sort(numbers)) if __name__ == "__main__": diff --git a/algorithms/quicksort.py b/algorithms/quicksort.py index 055d02e..4853a94 100644 --- a/algorithms/quicksort.py +++ b/algorithms/quicksort.py @@ -13,7 +13,7 @@ def _partition(numbers: list[int], low: int, high: int) -> int: pivot = numbers[high] i = low for j in range(low, high): - if numbers[j] <= pivot: + if numbers[j] < pivot: numbers[i], numbers[j] = numbers[j], numbers[i] i += 1 numbers[i], numbers[high] = numbers[high], numbers[i] @@ -22,8 +22,8 @@ def _partition(numbers: list[int], low: int, high: int) -> int: def main() -> None: numbers = [4, 2, 7, 1, 9, 6, 5, 8, 3] - print("Unsorted:", numbers) - print("Sorted: ", quicksort(numbers)) + print("Unsorted list:", numbers) + print("Sorted list: ", quicksort(numbers)) if __name__ == "__main__": diff --git a/algorithms/selection_sort.py b/algorithms/selection_sort.py new file mode 100644 index 0000000..d734167 --- /dev/null +++ b/algorithms/selection_sort.py @@ -0,0 +1,18 @@ +def selection_sort(numbers: list[int]) -> list[int]: + for i in range(len(numbers) - 1): + min_index = i + for j in range(i + 1, len(numbers)): + if numbers[j] < numbers[min_index]: + min_index = j + numbers[i], numbers[min_index] = numbers[min_index], numbers[i] + return numbers + + +def main() -> None: + numbers = [4, 2, 7, 1, 9, 6, 5, 8, 3] + print("Unsorted list:", numbers) + print("Sorted list: ", selection_sort(numbers)) + + +if __name__ == "__main__": + main() \ No newline at end of file