diff --git a/C++_Problems/quick_sort.cpp b/C++_Problems/quick_sort.cpp new file mode 100644 index 00000000..b746218b --- /dev/null +++ b/C++_Problems/quick_sort.cpp @@ -0,0 +1,33 @@ +#include +using namespace std; + +// Function to swap two elements +void swap(int& a, int& b) { + int temp = a; + a = b; + b = temp; +} + +// Function to partition the array +int partition(int arr[], int low, int high) { + int pivot = arr[high]; // Choose the last element as the pivot + int i = low - 1; // Pointer for the smaller element + + for (int j = low; j < high; j++) { + // If the current element is smaller than or equal to the pivot + if (arr[j] <= pivot) { + i++; // Increment the index of the smaller element + swap(arr[i], arr[j]); // Swap the elements + } + } + swap(arr[i + 1], arr[high]); // Swap the pivot element with the element at i + 1 + return i + 1; // Return the partition index +} + +// Quick Sort function +void quickSort(int arr[], int low, int high) { + if (low < high) { + // Partition the array and get the pivot index + int pi = partition(arr, low, high); + +