Skip to content

Trying to get Visualizer to show up in C++ #233

Open
@hoaug-tran

Description

@hoaug-tran

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;
}

image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions