#include <bits/stdc++.h> using namespace std; int partition(int arr[],int s,int e) { int pivot=arr[s]; int cnt=0; for(int i=s+1;i<=e;i++) { if(arr[i]<=pivot) { cnt++; } } //place pivot at right position int pivotindex = s+cnt; swap(arr[pivotindex],arr[s]); //left and right wala part sambhal lete sambhal int i=s, j=e; 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; } void quicksort(int arr[], int s,int e) { //base case if(s>=e) return; //partition int p=partition(arr,s,e); //recursion //left part ko sort karo quicksort(arr,s,p-1); //right part ko sort karo quicksort(arr,p+1,e); } int main() { int a[8]={9,78,6,23,14,2,8,1}; int n=8; quicksort(a,0,n-1); for(int i=0;i<n;i++) { cout<<a[i]<<" "; } return 0; }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter