quick

#include <stdio.h>

int partition(int arr[], int low, int high)
{
    int pivot = arr[low]; // Choosing the pivot element
    int i = low + 1;
    int j = high;

    while (i <= j)
    {
        while (i <= high && arr[i] <= pivot)
        {
            i++;
        }
        while (j >= low && arr[j] > pivot)
        {
            j--;
        }
        if (i < j)
        {
            int temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }
    }
    int temp = arr[low];
    arr[low] = arr[j];
    arr[j] = temp;
    return j;
}

void quicksort(int arr[], int low, int high)
{
    if (low < high)
    {
        int p = partition(arr, low, high);
        quicksort(arr, low, p - 1);
        quicksort(arr, p + 1, high);
    }
}

int main()
{
    int size;
    printf("Enter the size of array: ");
    scanf("%d", &size);

    int arr[size];
    printf("Enter the %d elements of array: ", size);
    for (int i = 0; i < size; i++)
    {
        scanf("%d", &arr[i]);
    }

    quicksort(arr, 0, size - 1);

    printf("Array after sorting: ");
    for (int i = 0; i < size; i++)
    {
        printf("%d ", arr[i]);
    }
    printf("\n");

    return 0;
}

Comments

  1. #include
    using namespace std;
    class Complex {
    private:
    double real;
    double imag;

    public:
    Complex(double r = 0.0, double i = 0.0) : real(r), imag(i) {}

    // Overloaded insertion operator
    friend ostream& operator<<(ostream& os, const Complex& c) {
    os << c.real << " + " << c.imag << "i";
    return os;
    }

    // Overloaded extraction operator
    friend istream& operator>>(istream& is, Complex& c) {
    is >> c.real >> c.imag;
    return is;
    }
    };

    int main() {
    Complex c1, c2;
    cout << "Enter complex number 1 (real and imaginary parts): ";
    cin >> c1;
    cout << "Enter complex number 2 (real and imaginary parts): ";
    cin >> c2;
    cout << "Complex number 1: " << c1 << endl;
    cout << "Complex number 2: " << c2 << endl;
    return 0;
    }

    ReplyDelete

Post a Comment

Popular posts from this blog

employee