GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — python-sorting-algorithms/main.py at main · ergon73/python-sorting-algorithms Expand file tree/ Copy pathmain.pyMore file actions 72 lines (57 loc) · 2.65 KB / Copy pathmain.pyFile metadata and controls72 lines (57 loc) · 2.65 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 """Демонстрация алгоритмов сортировки из урока OD03.""" from sorting import ( bubble_sort, quick_sort, selection_sort, insertion_sort, merge_sort, ) def _read_array_from_stdin() -> list: """Читает массив целых чисел из ввода пользователя.""" raw = input("Введите числа через пробел: ") try: return [int(item) for item in raw.split()] except ValueError: print("Ошибка: нужно вводить только целые числа через пробел.") return [] def _read_array_from_file() -> list: """Читает массив целых чисел из текстового файла.""" path = input("Введите путь к файлу .txt: ").strip() try: with open(path, "r", encoding="utf-8") as file: content = file.read() except OSError: print("Ошибка: не удалось открыть файл. Проверьте путь и попробуйте снова.") return [] try: return [int(item) for item in content.split()] except ValueError: print("Ошибка: файл должен содержать только целые числа, разделенные пробелами.") return [] def main() -> None: """Точка входа для демонстрации работы алгоритмов сортировки.""" print("Выберите источник данных для массива:") print("1 — ввод чисел через пробел") print("2 — чтение из файла .txt") choice = input("Ваш выбор (1/2): ").strip() if choice == "2": test_array = _read_array_from_file() else: test_array = _read_array_from_stdin() if not test_array: print("Массив пуст или введены некорректные данные. Завершение работы.") return print(f"Исходный массив: {test_array}") print() algorithms = [ ("Пузырьковая сортировка (Bubble Sort)", bubble_sort), ("Быстрая сортировка (Quick Sort)", quick_sort), ("Сортировка выбором (Selection Sort)", selection_sort), ("Сортировка вставками (Insertion Sort)", insertion_sort), ("Сортировка слиянием (Merge Sort)", merge_sort), ] for name, func in algorithms: result = func(test_array) print(f"{name}: {result}") if __name__ == "__main__": main() |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |