Qucik Sort

#include<stdio.h>
#include<conio.h>
int partitions(int a[], int lb, int ub)
{
    int pivot=a[lb],start=lb, end=ub,temp;
    while(start<end)
    {
        while(a[start]<=pivot)
            start+=1;
        while(a[end]>pivot)
            end-=1;
        if(start<end)
        {
            temp=a[start];
            a[start]=a[end];
            a[end]=temp;
        }
    }
    temp=a[lb];
    a[lb]=a[end];
    a[end]=temp;
    return end;
}
void quick_sort(int a[], int lb, int ub)
{
    int loc;
    if(lb<ub)
    {
        loc=partitions(a,lb,ub);
        quick_sort(a,lb,loc-1);
        quick_sort(a,loc+1,ub);
    }
}

int main()
{
    int n,i,lb,ub;
    printf("Enter the size of array : ");
    scanf("%d",&n);
    printf("Enter the elements of array :");
    int a[n];
    for(i=0;i<n;i++)
    scanf("%d",&a[i]);
    lb=0;ub=n-1;
    quick_sort(a,lb,ub);
    printf("Sorted array is : \n");
    for(i=0;i<n;i++)
    printf("%d ",a[i]);
}

Comments

Popular posts from this blog

First_Come_First_Serve CPU Scheduling

Reversing stack Method 2 !! (One Helper Stack only)

Populating Next Right Pointers in Each Node in O(1) space (without queue and level order)

Calculate factorial of large numbers !! (Using Arrays)

Multiplication of large numbers (Given in string format)

Left View of Binary Tree (Method 1 using recursion)

Check Bracket Sequence

Image Multiplication

Boundary Traversal of binary tree

BST to greater sum tree