0% found this document useful (0 votes)
10 views

Quick Sort

This C++ code implements the quicksort algorithm for sorting an array of integers. It uses a partition method to choose a pivot element and rearrange the array such that all elements less than the pivot come before the pivot and all elements greater than the pivot come after it. The quicksort method then recursively calls itself on the subarrays partitioned by the pivot until the entire array is sorted.

Uploaded by

Mian Adnan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Quick Sort

This C++ code implements the quicksort algorithm for sorting an array of integers. It uses a partition method to choose a pivot element and rearrange the array such that all elements less than the pivot come before the pivot and all elements greater than the pivot come after it. The quicksort method then recursively calls itself on the subarrays partitioned by the pivot until the entire array is sorted.

Uploaded by

Mian Adnan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include<iostream>

using namespace std;

int partition(int *a,int start,int end)

int pivot=a[end];

//P-index indicates the pivot value index

int P_index=start;

int i,t;

for(i=start;i<end;i++)

if(a[i]<=pivot)

t=a[i];

a[i]=a[P_index];

a[P_index]=t;

P_index++;

t=a[end];

a[end]=a[P_index];

a[P_index]=t;

//at last returning the pivot value index

return P_index;

void Quicksort(int *a,int start,int end)

{
if(start<end)

int P_index=partition(a,start,end);

Quicksort(a,start,P_index-1);

Quicksort(a,P_index+1,end);

int main()

int n;

cout<<"Enter number of elements: ";

cin>>n;

int a[n];

cout<<"Enter the array elements:\n";

for(int i=0;i<n;i++)

cin>>a[i];

Quicksort(a,0,n-1);

cout<<"After Quick Sort the array is:\n";

for(int i=0;i<n;i++)

cout<<a[i]<<" ";

return 0;

You might also like