From 7417932ba465610d2b7f6770253230975188deb4 Mon Sep 17 00:00:00 2001 From: Canule <160742623+Raunak-2005@users.noreply.github.com> Date: Tue, 15 Oct 2024 13:51:42 +0530 Subject: [PATCH 1/2] Added Quick Sort Program --- divide_and_conquer/quicksort.py | 69 +++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 divide_and_conquer/quicksort.py diff --git a/divide_and_conquer/quicksort.py b/divide_and_conquer/quicksort.py new file mode 100644 index 000000000000..d40a905a258b --- /dev/null +++ b/divide_and_conquer/quicksort.py @@ -0,0 +1,69 @@ +from __future__ import annotations + +def partition(array: list, start: int, end: int) -> int: + """Helper function for Quick Sort. + + >>> array = [5, 8, 3, 2, 9, 6] + >>> start = 0 + >>> end = 5 + >>> index = partition(array, start, end) + >>> index # The pivot index + 3 + >>> sorted(array[:index]) # Elements less than pivot + [2, 3, 5] + >>> sorted(array[index+1:]) # Elements greater than pivot + [8, 9] + + >>> array = [] + >>> start = 0 + >>> end = 0 + >>> partition(array, start, end) + 0 + >>> array + [] + """ + if not array: + return 0 + pivot = array[end] + index = start - 1 + for j in range(start, end): + if array[j] <= pivot: + index += 1 + array[index], array[j] = array[j], array[index] + array[index+1], array[end] = array[end], array[index+1] + return index+1 + +def quicksort(array, start, end): + """Returns a list of sorted array elements using Quick Sort. + + >>> from random import shuffle + >>> array = [5, 8, 3, 2, 9, 6] + >>> start = 0 + >>> end = 5 + >>> quicksort(array, start, end) + >>> array + [2, 3, 5, 6, 8, 9] + + >>> shuffle(array) + >>> quicksort(array, start, end) + >>> array + [2, 3, 5, 6, 8, 9] + + >>> array = [-100] + >>> quicksort(array, 0, len(array) - 1) + >>> array + [-100] + + >>> array = [] + >>> quicksort(array, 0, 0) + >>> array + [] + """ + if start < end: + partition_index = partition(array, start, end) + quicksort(array, start, partition_index-1) + quicksort(array, partition_index+1, end) + +if __name__ == "__main__": + import doctest + doctest.testmod() From 7299b9c866faa6b25c6ffe4239fcca2430d3a884 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 15 Oct 2024 08:33:24 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- divide_and_conquer/quicksort.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/divide_and_conquer/quicksort.py b/divide_and_conquer/quicksort.py index d40a905a258b..87ccad4c9969 100644 --- a/divide_and_conquer/quicksort.py +++ b/divide_and_conquer/quicksort.py @@ -1,5 +1,6 @@ from __future__ import annotations + def partition(array: list, start: int, end: int) -> int: """Helper function for Quick Sort. @@ -22,7 +23,7 @@ def partition(array: list, start: int, end: int) -> int: >>> array [] """ - if not array: + if not array: return 0 pivot = array[end] index = start - 1 @@ -30,8 +31,9 @@ def partition(array: list, start: int, end: int) -> int: if array[j] <= pivot: index += 1 array[index], array[j] = array[j], array[index] - array[index+1], array[end] = array[end], array[index+1] - return index+1 + array[index + 1], array[end] = array[end], array[index + 1] + return index + 1 + def quicksort(array, start, end): """Returns a list of sorted array elements using Quick Sort. @@ -61,9 +63,11 @@ def quicksort(array, start, end): """ if start < end: partition_index = partition(array, start, end) - quicksort(array, start, partition_index-1) - quicksort(array, partition_index+1, end) + quicksort(array, start, partition_index - 1) + quicksort(array, partition_index + 1, end) + if __name__ == "__main__": import doctest + doctest.testmod()