From da53b26bde8f2faf7104ef39ca591b94306a1a83 Mon Sep 17 00:00:00 2001 From: jiya <122276932+jiya10208@users.noreply.github.com> Date: Mon, 19 May 2025 19:52:44 +0530 Subject: [PATCH] docs: add time and space complexity to quick_sort.cpp (#2819) * Complexity quick_sort.cpp Space Complexity Time complexity * Update sorting/quick_sort.cpp Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com> * Update sorting/quick_sort.cpp Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com> --------- Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com> --- sorting/quick_sort.cpp | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/sorting/quick_sort.cpp b/sorting/quick_sort.cpp index 8a582790817..df4a31259c8 100644 --- a/sorting/quick_sort.cpp +++ b/sorting/quick_sort.cpp @@ -54,7 +54,18 @@ namespace quick_sort { * @param low first point of the array (starting index) * @param high last point of the array (ending index) * @returns index of the smaller element - */ + * + * ### Time Complexity + * best case, average Case: O(nlog(n)) + * Worst Case: O(n^2) (Worst case occur when the partition + * is consistently unbalanced.) + + * ### Space Complexity + * average Case: O(log(n)) + * Worst Case: O(n) + * It's space complexity is due to the recursive function calls and partitioning process. + */ + template int partition(std::vector *arr, const int &low, const int &high) { T pivot = (*arr)[high]; // taking the last element as pivot