C program to sort a given array using Quick Sort algorithm



#include<stdio.h>
#define swap(a,b,c,temp1) {temp1=a[b];a[b]=a[c];a[c]=temp1;}
//Sorting an array with QUICKSORT
void sort(int *arr,int p,int q);
int partition(int *arr,int p,int q);


int main()
{
    int arr[]={3,7,4,2,6,0,5};
    int i;
    
    sort(arr,0,6);
    
    for(i=0;i<7;i++)
    printf("%d",arr[i]);
    
    return 0;
}


void sort(int *arr,int p,int q)
{
    int temp;
    if(p<q)
    {
        temp=partition(arr,p,q);
        sort(arr,p,temp-1);
        sort(arr,temp+1,q);
    }
}


int partition(int *arr,int p, int q)
{
    int pivot,i,b=p-1,temp;
    pivot=arr[q];
    for(i=p;i<=q-1;i++)
    {
        if(arr[i]<=pivot)
        {
            b=b+1;
            swap(arr,i,b,temp);
        }
    }
    swap(arr,b+1,q,temp);
    return b+1;
}

Comments

Popular posts from this blog

C Graph implementation with adjacency list representation using structures: Data Structure Tutorial 1

Accessing DynamoDB from Spring Java application - Walkthrough tutorial

Interview Of An Indian Top Coder : Rudradev Basak