diff --git a/Algorithms/Sort/QuickSort/QuickSort.cpp b/Algorithms/Sort/QuickSort/QuickSort.cpp new file mode 100644 index 0000000..e215899 --- /dev/null +++ b/Algorithms/Sort/QuickSort/QuickSort.cpp @@ -0,0 +1,83 @@ +#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 The pivot around which the elements are partitioned. + */ +int partition(std::vector& array, int low, int high) { + // Choose the rightmost element as the pivot + int pivot = array[high]; + int i = low - 1; + + for(int j = low; j <= high - 1; j++) { + if(array[j] <= pivot) { + i = i + 1; + swap(i, j, array); + } + } + + // Placing 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) { + int pivotIndex = partition(array, low, high); + + quickSort(array, low, pivotIndex -1); + quickSort(array, pivotIndex + 1, high); + } +} + + +int main() { + std::vector exampleArray = {-1, -5, -7, 6, 1, 0, 0, 4, 1, 12, 100}; + + std::cout << "Original array: "; + for (int elem : exampleArray) { + std::cout << elem << " "; + } + std::cout << std::endl; + + quickSort(exampleArray, 0, exampleArray.size()-1); + + std::cout << "Sorted array: "; + for (int elem : exampleArray) { + std::cout << elem << " "; + } + std::cout << std::endl; + + return 0; +} \ No newline at end of file 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