Skip to content

Commit

Permalink
Merge pull request #133 from arjunjain8887/patch-1
Browse files Browse the repository at this point in the history
Create quick_sort.cpp
  • Loading branch information
Ayu-hack authored Oct 8, 2024
2 parents 4436051 + 82217e8 commit 9117876
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions C++_Problems/quick_sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <iostream>
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);


0 comments on commit 9117876

Please sign in to comment.