Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

c++ code for quicksort

#include<bits/stdc++.h>
using namespace std;
int partition(int a[],int low,int high)
{
    int pivot =a[high],i=low-1,j=low;
    for(j=low;j<=high-1;j++)
    {
        if(a[high]>=a[j])
        {
            i++;
            swap(a[i],a[j]);
        }
    }
    swap(a[i+1],a[high]);
    return i+1;
    
}
void quicksort(int a[],int low,int high)
{
    if(low<high)
    {
        int  t = partition(a,low,high);
        quicksort(a,low,t-1);
        quicksort(a,t+1,high);
    }
}
int main()
{
    //quick sort
    int a[8] = {3,10,8,51,92,6,2,1};
    quicksort(a,0,7);
    for(int i=0;i<8;i++)
    {
        cout<<a[i]<<" ";
    }
    
    return 0;
}
 
PREVIOUS NEXT
Tagged: #code #quicksort
ADD COMMENT
Topic
Name
4+3 =