Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
hoaug-tran opened this issue Oct 20, 2024 · 0 comments
Open

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

hoaug-tran opened this issue Oct 20, 2024 · 0 comments

Comments

@hoaug-tran
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant