quick sort

Quick sort 




#include<stdio.h>

void quicksort(int a[],int lb,int ub);

int position(int a[],int lb,int ub);

void swap(int *a,int *b);

int main() 

{

    int n,i;

    printf("Enter number of elements you want to insert: ");

    scanf("%d",&n);

    int a[n];   

    printf("Enter the elements: ");

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

    {

        scanf("%d",&a[i]);

    }

    quicksort(a,0,n-1);

    printf("Sorted elements: ");

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

    {

        printf("%d ", a[i]);

    }

}

void quicksort(int a[],int lb,int ub) 

{

    if(lb<ub) 

    {

        int loc=position(a,lb,ub);

        quicksort(a,lb,loc-1);

        quicksort(a,loc+1,ub);

    }

}

int position(int a[],int lb,int ub) 

{

    int pivot=a[lb];

    int start=lb;

    int end=ub;

    while(start<end) 

    {

        while(a[start]<=pivot) 

        {

            start++;

        }

        while(a[end]>pivot) 

        {

            end--;

        }

        if(start<end) 

        {

            swap(&a[start],&a[end]);

        }

    }

    swap(&a[lb],&a[end]);

    return end;

}

void swap(int *a,int *b) 

{

    int temp=*a;

    *a=*b;

    *b=temp;

}

Comments

Popular posts from this blog

SINGLE LINKED LIST by smd

CLL by smd

QUEUE USING ARRAYS by smd