0% found this document useful (0 votes)
17 views2 pages

Quick Sort

The document contains a C program that implements the Quick Sort algorithm to sort an array of integers. It prompts the user to enter the size of the array and its elements, then sorts the array using the quicksort function and displays the sorted output. The program includes functions for partitioning the array and exchanging elements.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views2 pages

Quick Sort

The document contains a C program that implements the Quick Sort algorithm to sort an array of integers. It prompts the user to enter the size of the array and its elements, then sorts the array using the quicksort function and displays the sorted output. The program includes functions for partitioning the array and exchanging elements.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <stdio.

h>
#include <stdlib.h>
int main()
{
int n, p, q, r, *a, i;
printf("Enter size of array:\n");
scanf("%d",&n);
a = malloc(sizeof(n));
printf("\n Enter %d element :\n", n );

for(i=0;i<n;i++)
scanf("%d",a+i);

p = 0; //starting index
r = n-1; //ending index
printf("\n Elements Before sorting:\n");

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


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

quicksort(a, p, r );
printf("\n The sorted output is:\n");
for( i = 0; i < n ; i++)
printf( " %d\t ", a[i] );
return 0;
}

void quicksort(int a[], int p, int r)


{

if(p<r)
{

int q = PARTITION(a, p, r);


//printf("q=%d",q);
quicksort(a, p, q-1);
quicksort(a, q+1, r);
}
}

int PARTITION(int a[], int p, int r)


{
int pivot = a[r];
int i = p - 1 , j ;

for(j = p ; j < r ; j++ )


{

if(a[j] < pivot)


{
i=i+1;
exchange(a,i,j);
}
}
exchange(a,i+1,r);
return (i+1);
}

//exchange a[p] with a[r]


void exchange(int a[] , int p, int r)
{
int temp = a[ p];
a [ p] = a [ r ];
a [ r ] = temp;
}

You might also like