programming-challenges/LEXISORT/LEXISORT-QUICK.py
2025-02-06 12:17:03 -06:00

54 lines
1.0 KiB
Python

def partition(arr: list, low: int, high: int) -> int:
pivot = arr[high]
i = low - 1
for j in range(low, high):
if arr[j] < pivot:
i += 1
swap(arr, i, j)
swap(arr, i + 1, high)
return i + 1
def swap(arr: list, i: int, j: int) -> None:
arr[i], arr[j] = arr[j], arr[i]
def quick_sort_h(arr: list, low: int, high: int) -> list:
# copy = arr.copy()
if low < high:
pi = partition(arr, low, high)
quick_sort_h(arr, low, pi - 1)
quick_sort_h(arr, pi + 1, high)
# return copy
# def quick_sort(arr: list) -> list:
# return quick_sort_h(arr, 0, len(arr) - 1)
def main():
lines: list[str] = []
test_cases = int(input())
while (test_cases != 0):
n = int(input())
while (n != 0):
lines.append(input())
n -= 1
quick_sort_h(lines, 0, len(lines) - 1)
for line in lines:
print(line)
lines = []
test_cases -= 1
if __name__ == "__main__":
main()