#include<iostream>usingnamespace std;// Swap two elements - Utility function voidswap(int* a,int* b){int t =*a;*a =*b;*b = t;}// partition the array using last element as pivotintpartition(int arr[],int low,int high){int pivot = arr[high];// pivot int i =(low -1);for(int j = low; j <= high-1; j++){//if current element is smaller than pivot, increment the low element//swap elements at i and jif(arr[j]<= pivot){
i++;// increment index of smaller element swap(&arr[i],&arr[j]);}}swap(&arr[i +1],&arr[high]);return(i +1);}//quicksort algorithmvoidquickSort(int arr[],int low,int high){if(low < high){//partition the array int pivot =partition(arr, low, high);//sort the sub arrays independently quickSort(arr, low, pivot -1);quickSort(arr, pivot +1, high);}}voiddisplayArray(int arr[],int size){int i;for(i=0; i < size; i++)
cout<<arr[i]<<" ";}intmain(){int arr[]={12,23,3,43,51,35,19,45};int n =sizeof(arr)/sizeof(arr[0]);
cout<<"Input array"<<endl;displayArray(arr,n);
cout<<endl;quickSort(arr,0, n-1);
cout<<"Array sorted with quick sort"<<endl;displayArray(arr,n);return0;}
intpartition(int arr[],int start,int end){int pivot = arr[start];int count =0;for(int i = start +1; i <= end; i++){if(arr[i]<= pivot)
count++;}// Giving pivot element its correct positionint pivotIndex = start + count;swap(arr[pivotIndex], arr[start]);// Sorting left and right parts of the pivot elementint i = start, j = end;while(i < pivotIndex && j > pivotIndex){while(arr[i]<= pivot){
i++;}while(arr[j]> pivot){
j--;}if(i < pivotIndex && j > pivotIndex){swap(arr[i++], arr[j--]);}}return pivotIndex;}voidquickSort(int arr[],int start,int end){// base caseif(start >= end)return;// partitioning the arrayint p =partition(arr, start, end);// Sorting the left partquickSort(arr, start, p -1);// Sorting the right partquickSort(arr, p +1, end);}
// C++ Implementation of the Quick Sort Algorithm.#include<iostream>usingnamespace std;intpartition(int arr[],int start,int end){int pivot = arr[start];int count =0;for(int i = start +1; i <= end; i++){if(arr[i]<= pivot)
count++;}// Giving pivot element its correct positionint pivotIndex = start + count;swap(arr[pivotIndex], arr[start]);// Sorting left and right parts of the pivot elementint i = start, j = end;while(i < pivotIndex && j > pivotIndex){while(arr[i]<= pivot){
i++;}while(arr[j]> pivot){
j--;}if(i < pivotIndex && j > pivotIndex){swap(arr[i++], arr[j--]);}}return pivotIndex;}voidquickSort(int arr[],int start,int end){// base caseif(start >= end)return;// partitioning the arrayint p =partition(arr, start, end);// Sorting the left partquickSort(arr, start, p -1);// Sorting the right partquickSort(arr, p +1, end);}intmain(){int arr[]={9,3,4,2,1,8};int n =6;quickSort(arr,0, n -1);for(int i =0; i < n; i++){
cout << arr[i]<<" ";}return0;}
/* C++ implementation of QuickSort */#include<bits/stdc++.h>usingnamespace std;// A utility function to swap two elements voidswap(int* a,int* b){int t =*a;*a =*b;*b = t;}/* This function takes last element as pivot, places
the pivot element at its correct position in sorted
array, and places all smaller (smaller than pivot)
to left of pivot and all greater elements to right
of pivot */intpartition(int arr[],int low,int high){int pivot = arr[high];// pivot int i =(low -1);// Index of smaller element and indicates the right position of pivot found so farfor(int j = low; j <= high -1; j++){// If current element is smaller than the pivot if(arr[j]< pivot){
i++;// increment index of smaller element swap(&arr[i],&arr[j]);}}swap(&arr[i +1],&arr[high]);return(i +1);}/* The main function that implements QuickSort
arr[] --> Array to be sorted,
low --> Starting index,
high --> Ending index */voidquickSort(int arr[],int low,int high){if(low < high){/* pi is partitioning index, arr[p] is now
at right place */int pi =partition(arr, low, high);// Separately sort elements before // partition and after partition quickSort(arr, low, pi -1);quickSort(arr, pi +1, high);}}/* Function to print an array */voidprintArray(int arr[],int size){int i;for(i =0; i < size; i++)
cout << arr[i]<<" ";
cout << endl;}// Driver Codeintmain(){int arr[]={10,7,8,9,1,5};int n =sizeof(arr)/sizeof(arr[0]);quickSort(arr,0, n -1);
cout << "Sorted array:
";printArray(arr, n);return0;}// This code is contributed by rathbhupendra
#include<iostream>usingnamespace std;voidswap(int j ,int k){int temp;
temp = j;
j = k;
k = temp;}intpartition(int arr[],int s ,int e){int pivot = arr[e];// last element as pivotint pindex = s;// initially 0for(int i=s ; i<e; i++){if(arr[i]<pivot){int temp = arr[i];
arr[i]= arr[pindex];
arr[pindex]= temp;
pindex++;}}int temp = arr[e];
arr[e]= arr[pindex];
arr[pindex]= temp;return pindex;}voidQuickSort(int arr[],int s ,int e ){if(s<e){int p =partition(arr , s , e );QuickSort(arr , s ,(p-1));QuickSort(arr ,(p+1), e );}}intmain(){int i,n;
cout<<" enter the number of elements in the array :"<<endl;
cin>>n;int a[n];
cout<<" enter the elements of the array :"<<endl;for(i=0; i<n; i++){
cin>>a[i];}QuickSort(a ,0, n-1);
cout<<" sorted array :"<<endl;for(i=0; i<n; i++){
cout<<a[i]<<endl;}return0;}
/* low --> Starting index, high --> Ending index */quickSort(arr[], low, high){if(low < high){/* pi is partitioning index, arr[p] is now
at right place */
pi =partition(arr, low, high);quickSort(arr, low, pi -1);// Before piquickSort(arr, pi +1, high);// After pi}}