C Program to implement quick sort. Quick sort algorithm is based on divide and conquer strategy. In a quick sort we take the one element called as pivot,then we list all the smaller elements than pivot, and greater than pivot. after partitioning we have pivot in the final position. After recursively sorting the partition array, we get the sorted elements. Read more about C Programming Language .

/***********************************************************

* You can use all the programs on www.c-program-example.com

* for personal and learning purposes. For permissions to use the

* programs for commercial purposes,

* contact info@c-program-example.com

* To find more C programs, do visit www.c-program-example.com

* and browse!

*

* Happy Coding

***********************************************************/

#include<stdio.h>

void swap (int a[], int left, int right)

{

int temp;

temp=a[left];

a[left]=a[right];

a[right]=temp;

}//end swap

void quicksort( int a[], int low, int high )

{

int pivot;

// Termination condition!

if ( high > low )

{

pivot = partition( a, low, high );

quicksort( a, low, pivot-1 );

quicksort( a, pivot+1, high );

}

} //end quicksort

int partition( int a[], int low, int high )

{

int left, right;

int pivot_item;

int pivot = left = low;

pivot_item = a[low];

right = high;

while ( left < right )

{

// Move left while item < pivot

while( a[left] <= pivot_item )

left++;

// Move right while item > pivot

while( a[right] > pivot_item )

right--;

if ( left < right )

swap(a,left,right);

}

// right is final position for the pivot

a[low] = a[right];

a[right] = pivot_item;

return right;

}//end partition

// void quicksort(int a[], int, int);

void printarray(int a[], int);

int main()

{

int a[50], i, n;

printf("nEnter no. of elements: ");

scanf("%d", &n);

printf("nEnter the elements: n");

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

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

printf("nUnsorted elements: n");

printarray(a,n);

quicksort(a,0,n-1);

printf("nSorted elements: n");

printarray(a,n);

}//end main

void printarray(int a[], int n)

{

int i;

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

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

printf("n");

}//end printarray

Read more Similar C Programs

Data Structures

C Sorting

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

(c) www.c-program-example.com

## 4 comments on “C Program to implement quick sort”

Hey admin,

Excellent post for the quick short program.

of course this blog is doing a very good job of serving useful information. I'm proud to be a part of its Readers community.

Thing is, I have just moved to WordPress. However , Excellent number of people that have subscribed to my bottles from my blogger blog page. What should I do to make sure that they right now receive improvements from my WordPress blog?.. FYI, I have a feedburner account..

Hi Lorrie,

I can understand your problem. Even I moved recently. I have similar problem and planning to follow the steps from https://www.wprssaggregator.com/rss-tricks-redirect-wordpress-feeds-feedburner-feeds/

I will be doing it in next few days and will be happy to share results with you. Connect of FB, Twitter or email if you need help with it.

Happy writing!

-SN

Thanks!