Open
Description
I tried installing Vscode and installing the Debug Visualizer Extension. However I can't get it to work properly. I tried to get it to print out a sorted array using Quick Sort but it failed. Can someone help me? Thanks
#include <iostream>
#include <vector>
#include <string>
using namespace std;
void Swap(int &a, int &b)
{
int tmp = a;
a = b;
b = tmp;
}
void QuickSort(vector<int> &a, int low, int high)
{
if (low < high)
{
int pivot = a[high];
int i = low - 1;
for (int j = low; j < high; j++)
{
if (a[j] < pivot)
{
i++;
Swap(a[i], a[j]);
}
}
Swap(a[i + 1], a[high]);
int pi = i + 1;
cout << "Array after partition with pivot " << pivot << ": ";
for (int k = 0; k < a.size(); k++)
{
cout << a[k] << " ";
}
cout << endl;
QuickSort(a, low, pi - 1);
QuickSort(a, pi + 1, high);
}
}
int main()
{
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
cout << "Array before sorting: ";
for (int i = 0; i < n; i++)
{
cout << a[i] << " ";
}
cout << endl;
QuickSort(a, 0, n - 1);
cout << "Array after sorting: ";
for (int i = 0; i < n; i++)
{
cout << a[i] << " ";
}
cout << endl;
return 0;
}
Metadata
Metadata
Assignees
Labels
No labels