Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 11 additions & 19 deletions quickSort.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,27 @@ using namespace std;
// -------------------------------------------------------------------------

// Partition function
int partition(vector <int> &v, int start, int end) {
int partition(vector <int> &a, int start, int end) {

int pivot = a[end]; // Take the last element as a pivot
int i = start - 1;

int pivot = start; // Take the first element as a pivot
int i = start + 1;
int j = end;

while (i <= j) {

// If element at the left is bigger than the pivot and
// element at the right is smaller, swap elements

if (v[i] > v[pivot] && v[j] < v[pivot]) {
swap(v[i], v[j]);
}
else if (v[i] <= v[pivot]) { i++; }
else if (v[j] >= v[pivot]) { j--; }
for(int j=start;j<end;j++)
{
i++;
swap(a[i],a[j]);
}

// we swap the pivot into the right position
swap(v[j], v[pivot]);
return j;
swap(a[i+1],a[end]);
return i+1;
}

// QuickSort
void quickSort(vector <int> &v, int start, int end) {

if (start < end) {
int pivot = partition(v, start, end);
quickSort(v, start, pivot);
quickSort(v, start, pivot-1);
quickSort(v, pivot + 1, end);
}
}
Expand Down