From a09bdc929814853f112988cc53c5702e8c4295cf Mon Sep 17 00:00:00 2001 From: 2G-Afroz Date: Tue, 23 Jan 2024 20:14:19 +0530 Subject: [PATCH 1/4] Quick Sort: Added Pseudocode for Quick Sort Algorithm --- Algorithms/Sort/QuickSort/README.md | 56 +++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 Algorithms/Sort/QuickSort/README.md diff --git a/Algorithms/Sort/QuickSort/README.md b/Algorithms/Sort/QuickSort/README.md new file mode 100644 index 0000000..bc7e2e2 --- /dev/null +++ b/Algorithms/Sort/QuickSort/README.md @@ -0,0 +1,56 @@ +# Quick Sort +Quick Sort is a powerful sorting algorithm that efficiently sorts an array by dividing it into smaller segments. It's known for its speed and is often used in real-world applications. Unlike `Insertion Sort`, Quick Sort doesn't build the sorted array one element at a time; instead, it divides the array into smaller segments, sorts them independently, and then combines them. + +## Pseudocode +```plaintext +function quickSort(array, low, high): + if low < high: + # Partition the array into two segments + pivotIndex = partition(array, low, high) + + # Recursively sort the two segments + quickSort(array, low, pivotIndex - 1) + quickSort(array, pivotIndex + 1, high) + +function partition(array, low, high): + # Choose the rightmost element as the pivot + pivot = array[high] + i = low - 1 + + # Iterate through the array to rearrange elements + for j = low to high - 1: + if array[j] <= pivot: + i = i + 1 + swap(array, i, j) + + # Place the pivot in its correct position + swap(array, i + 1, high) + return i + 1 + +function swap(array, i, j): + temp = array[i] + array[i] = array[j] + array[j] = temp +``` + +## Time & Space Complexity +|Case |Time |Space | +|:-------|:---------------|:--------| +|Best |Ω(n log(n)) |Ω(log(n))| +|Average |Θ(n log(n)) |Θ(log(n))| +|Worst |O(n2)|O(log(n))| + +## Variants +- Randomized QuickSort +- Hybrid QuickSort (combining QuickSort with another sorting algorithm for small segments) + +## Use Cases +- Highly effective for sorting large datasets efficiently. +- Frequently used in various programming libraries and languages. +- Well-suited for scenarios where speed is crucial. + +## Best Practice +- Efficient for large datasets, outperforms algorithms like `Insertion Sort` for big tasks. +- Implementing randomized versions helps avoid worst-case scenarios. +- Consider the hybrid version for increased efficiency with smaller datasets. +- Well-suited for scenarios where elements are scattered throughout the array. \ No newline at end of file From 8eea0eb75dd672e2c46706d305e8f34508ae84c1 Mon Sep 17 00:00:00 2001 From: 2G-Afroz Date: Wed, 24 Jan 2024 21:26:07 +0530 Subject: [PATCH 2/4] Quick Sort Code: Added C++ implementation of Quick Sort Algorithm. --- Algorithms/Sort/QuickSort/QuickSort.cpp | 90 +++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 Algorithms/Sort/QuickSort/QuickSort.cpp diff --git a/Algorithms/Sort/QuickSort/QuickSort.cpp b/Algorithms/Sort/QuickSort/QuickSort.cpp new file mode 100644 index 0000000..e2a4775 --- /dev/null +++ b/Algorithms/Sort/QuickSort/QuickSort.cpp @@ -0,0 +1,90 @@ +#include +#include + +/** + * @brief Swap two elements in a vector. + * + * This function swaps the elements at positions `i` and `j` in the provided vector. + * + * @param i Index of the first element to be swapped. + * @param j Index of the second element to be swapped. + * @param array Reference to the vector containing elements. + */ +void swap(int i, int j, std::vector& array) { + int temp = array[i]; + array[i] = array[j]; + array[j] = temp; +} + +/** + * @brief Partition the elements into two parts around the pivot + * This function arrange the elements of array into tow parts around the pivot + * + * @param array Reference to the vector containing elements. + * @param low Lowest index of the array. + * @param high Highest index of the array. + * @return int + */ +int partition(std::vector& array, int low, int high) { + // Choose the rightmost element as the pivot + int pivot = array[high]; + int i = low - 1; + + // Iterate through the array to rearrange elements + for(int j = low; j <= high - 1; j++) { + if(array[j] <= pivot) { + i = i + 1; + swap(i, j, array); + } + } + + // Place the pivot in its correct position + swap(i + 1, high, array); + return i + 1; +} + +/** + * @brief Perform Quick Sort on a vector. + * + * This function sorts the elements of the vector in ascending order using the + * Quick Sort algorithm. + * + * @param array Reference to the vector to be sorted. + * @param low Reference to the lowest index of the array. + * @param high Reference to the highest index of the array. + */ +void quickSort(std::vector& array, int low, int high) { + if(low < high) { + // Partion the array into two segments + int pivotIndex = partition(array, low, high); + + // Recursively sort the two segments + quickSort(array, low, pivotIndex -1); + quickSort(array, pivotIndex + 1, high); + } +} + + +int main() { + // Example usage + std::vector myArray = {-1, -5, -7, 6, 1, 0, 0, 4, 1, 12, 100}; + + // Print the original array + std::cout << "Original array: "; + for (int elem : myArray) { + std::cout << elem << " "; + } + std::cout << std::endl; + + // Sort the array using bubble sort + quickSort(myArray, 0, myArray.size()-1); + + // Print the sorted array + std::cout << "Sorted array: "; + for (int elem : myArray) { + std::cout << elem << " "; + } + std::cout << std::endl; + + return 0; +} \ No newline at end of file From 1485fc19a33bf4f08bf16cc81a07a310ad95442f Mon Sep 17 00:00:00 2001 From: 2G-Afroz Date: Wed, 24 Jan 2024 21:27:18 +0530 Subject: [PATCH 3/4] Quick Sort Code: Fixed some typo. --- Algorithms/Sort/QuickSort/QuickSort.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Algorithms/Sort/QuickSort/QuickSort.cpp b/Algorithms/Sort/QuickSort/QuickSort.cpp index e2a4775..14dca1b 100644 --- a/Algorithms/Sort/QuickSort/QuickSort.cpp +++ b/Algorithms/Sort/QuickSort/QuickSort.cpp @@ -23,7 +23,7 @@ void swap(int i, int j, std::vector& array) { * @param array Reference to the vector containing elements. * @param low Lowest index of the array. * @param high Highest index of the array. - * @return int + * @return int The pivot around which the elements are partitioned. */ int partition(std::vector& array, int low, int high) { // Choose the rightmost element as the pivot From 3e5bc8ba49a986a0fcc6c847078c81055b7dd734 Mon Sep 17 00:00:00 2001 From: 2G-Afroz Date: Sun, 28 Jan 2024 18:42:16 +0530 Subject: [PATCH 4/4] Quick Sort Code: Removed some unwanted comments. --- Algorithms/Sort/QuickSort/QuickSort.cpp | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/Algorithms/Sort/QuickSort/QuickSort.cpp b/Algorithms/Sort/QuickSort/QuickSort.cpp index 14dca1b..e215899 100644 --- a/Algorithms/Sort/QuickSort/QuickSort.cpp +++ b/Algorithms/Sort/QuickSort/QuickSort.cpp @@ -30,7 +30,6 @@ int partition(std::vector& array, int low, int high) { int pivot = array[high]; int i = low - 1; - // Iterate through the array to rearrange elements for(int j = low; j <= high - 1; j++) { if(array[j] <= pivot) { i = i + 1; @@ -38,7 +37,7 @@ int partition(std::vector& array, int low, int high) { } } - // Place the pivot in its correct position + // Placing the pivot in its correct position swap(i + 1, high, array); return i + 1; } @@ -55,10 +54,8 @@ int partition(std::vector& array, int low, int high) { */ void quickSort(std::vector& array, int low, int high) { if(low < high) { - // Partion the array into two segments int pivotIndex = partition(array, low, high); - // Recursively sort the two segments quickSort(array, low, pivotIndex -1); quickSort(array, pivotIndex + 1, high); } @@ -66,22 +63,18 @@ void quickSort(std::vector& array, int low, int high) { int main() { - // Example usage - std::vector myArray = {-1, -5, -7, 6, 1, 0, 0, 4, 1, 12, 100}; + std::vector exampleArray = {-1, -5, -7, 6, 1, 0, 0, 4, 1, 12, 100}; - // Print the original array std::cout << "Original array: "; - for (int elem : myArray) { + for (int elem : exampleArray) { std::cout << elem << " "; } std::cout << std::endl; - // Sort the array using bubble sort - quickSort(myArray, 0, myArray.size()-1); + quickSort(exampleArray, 0, exampleArray.size()-1); - // Print the sorted array std::cout << "Sorted array: "; - for (int elem : myArray) { + for (int elem : exampleArray) { std::cout << elem << " "; } std::cout << std::endl;