mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-27 15:01:08 +00:00
Compare commits
6 Commits
4edd6bfbb7
...
b904cf0c4b
Author | SHA1 | Date | |
---|---|---|---|
|
b904cf0c4b | ||
|
f3f32ae3ca | ||
|
e3bd7721c8 | ||
|
e3f3d668be | ||
|
77efa8fa7c | ||
|
4bb056170f |
|
@ -16,7 +16,7 @@ repos:
|
||||||
- id: auto-walrus
|
- id: auto-walrus
|
||||||
|
|
||||||
- repo: https://github.com/astral-sh/ruff-pre-commit
|
- repo: https://github.com/astral-sh/ruff-pre-commit
|
||||||
rev: v0.7.2
|
rev: v0.7.4
|
||||||
hooks:
|
hooks:
|
||||||
- id: ruff
|
- id: ruff
|
||||||
- id: ruff-format
|
- id: ruff-format
|
||||||
|
@ -42,7 +42,7 @@ repos:
|
||||||
pass_filenames: false
|
pass_filenames: false
|
||||||
|
|
||||||
- repo: https://github.com/abravalheri/validate-pyproject
|
- repo: https://github.com/abravalheri/validate-pyproject
|
||||||
rev: v0.22
|
rev: v0.23
|
||||||
hooks:
|
hooks:
|
||||||
- id: validate-pyproject
|
- id: validate-pyproject
|
||||||
|
|
||||||
|
|
|
@ -172,7 +172,7 @@ def solved(values):
|
||||||
|
|
||||||
def from_file(filename, sep="\n"):
|
def from_file(filename, sep="\n"):
|
||||||
"Parse a file into a list of strings, separated by sep."
|
"Parse a file into a list of strings, separated by sep."
|
||||||
return open(filename).read().strip().split(sep) # noqa: SIM115
|
return open(filename).read().strip().split(sep)
|
||||||
|
|
||||||
|
|
||||||
def random_puzzle(assignments=17):
|
def random_puzzle(assignments=17):
|
||||||
|
|
53
divide_and_conquer/quick_sort_with_testing.py
Normal file
53
divide_and_conquer/quick_sort_with_testing.py
Normal file
|
@ -0,0 +1,53 @@
|
||||||
|
def partition(arr, low, high):
|
||||||
|
pivot = arr[high]
|
||||||
|
i = low - 1
|
||||||
|
|
||||||
|
for j in range(low, high):
|
||||||
|
if arr[j] <= pivot:
|
||||||
|
i += 1
|
||||||
|
arr[i], arr[j] = arr[j], arr[i]
|
||||||
|
|
||||||
|
arr[i + 1], arr[high] = arr[high], arr[i + 1]
|
||||||
|
return i + 1
|
||||||
|
|
||||||
|
|
||||||
|
def quick_sort(arr, low, high):
|
||||||
|
if low < high:
|
||||||
|
pi = partition(arr, low, high)
|
||||||
|
quick_sort(arr, low, pi - 1)
|
||||||
|
quick_sort(arr, pi + 1, high)
|
||||||
|
|
||||||
|
|
||||||
|
# Function to handle edge cases and testing
|
||||||
|
def test_quick_sort():
|
||||||
|
test_cases = [
|
||||||
|
# Regular case with positive integers
|
||||||
|
([10, 7, 8, 9, 1, 5], [1, 5, 7, 8, 9, 10]),
|
||||||
|
# Already sorted array
|
||||||
|
([1, 2, 3, 4, 5], [1, 2, 3, 4, 5]),
|
||||||
|
# Reverse sorted array
|
||||||
|
([5, 4, 3, 2, 1], [1, 2, 3, 4, 5]),
|
||||||
|
# Array with duplicates
|
||||||
|
([4, 2, 4, 1, 2, 1], [1, 1, 2, 2, 4, 4]),
|
||||||
|
# Array with negative and positive numbers
|
||||||
|
([-3, 5, -1, 7, 0, -2], [-3, -2, -1, 0, 5, 7]),
|
||||||
|
# Single element array
|
||||||
|
([42], [42]),
|
||||||
|
# Empty array
|
||||||
|
([], []),
|
||||||
|
# Array with all same elements
|
||||||
|
([1, 1, 1, 1], [1, 1, 1, 1]),
|
||||||
|
]
|
||||||
|
|
||||||
|
for i, (arr, expected) in enumerate(test_cases):
|
||||||
|
print(f"Test case {i+1}: Original array = {arr}")
|
||||||
|
quick_sort(arr, 0, len(arr) - 1)
|
||||||
|
assert (
|
||||||
|
arr == expected
|
||||||
|
), f"Test case {i+1} failed: Expected {expected}, but got {arr}"
|
||||||
|
print(f"Test case {i+1} passed: Sorted array = {arr}")
|
||||||
|
|
||||||
|
print("All test cases passed!")
|
||||||
|
|
||||||
|
|
||||||
|
test_quick_sort()
|
|
@ -1,4 +1,4 @@
|
||||||
#!/usr/bin/env python3
|
#!python
|
||||||
import os
|
import os
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
Loading…
Reference in New Issue
Block a user