0% found this document useful (0 votes)
5 views2 pages

Quick Sort Algorithm in C++ Code

The document contains a C++ implementation of the Quick Sort algorithm. It includes functions for partitioning the array, sorting it, and printing the array. The program prompts the user to enter the number of elements and the elements themselves, then displays the original and sorted arrays.

Uploaded by

Divye Kapoor
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views2 pages

Quick Sort Algorithm in C++ Code

The document contains a C++ implementation of the Quick Sort algorithm. It includes functions for partitioning the array, sorting it, and printing the array. The program prompts the user to enter the number of elements and the elements themselves, then displays the original and sorted arrays.

Uploaded by

Divye Kapoor
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Divye Kapoor IT – E

2300320130101
Quick Sort
#include <bits/stdc++.h>
using namespace std;

int partition(int A[],int p,int r) {


int x = A[r];
int i = p - 1;

for (int j=p;j<=r-1;j++) {


if (A[j]<=x) {
i++;
swap(A[i],A[j]);
}
}
swap(A[i+1],A[r]);
return i+1;
}

void quickSort(int A[],int p,int r) {


if (p<r) {
int q=partition(A,p,r);
quickSort(A,p,q-1);
quickSort(A,q+1,r);
}
}

void printArray(int A[],int n) {


for (int i=0; i<n;i++)
cout<<A[i] <<" ";
cout<<endl;
}

int main() {
int N;
cout<<"Enter number of elements: ";
cin>>N;
int A[N];

cout<<"Enter elements: ";


for (int i=0;i<N;i++)
cin>>A[i];

cout<<"Original Array: ";


printArray(A,N);

quickSort(A,0,N-1);

cout<<"Sorted Array using QuickSort: ";


printArray(A,N);

cout<<"Divye Kapoor IT - E 2300320130101"<<endl;

return 0;
}
Divye Kapoor IT – E
2300320130101

Output

You might also like