You are on page 1of 6

 

» C Algorithms » C Quicksort Algorithm

Quicksort Algorithm
Quicksort sorts a list based on the divide and conquer strategy. In quicksort algorithm we divide the list into two sub-lists, sort these sub-lists and
recursively until the list is sorted; The basic steps of quicksort algorithm are as follows:
1. Choose a key element in the list which is called a pivot.
2. Reorder the list with the rule that all elements which are less than the pivot come before the pivot and so that all elements greater than the
pivot come after it. After the partitioning, the pivot is in its final position.
3. Recursively reorder two sub-lists: the sub-list of lesser elements and the sub-list of greater elements.
Here is the animation to visualize how quicksort algorithm works

Here is source code which is implemented in C programming language to demonstrate how quicksort algorithm works:
view source
print?
01.#include <stdio.h>
02.#include <stdlib.h>
03. 
04.void swap(int *x,int *y)
05.{
06.int temp;
07.temp = *x;
08.*x = *y;
09.*y = temp;
10.}
11. 
12.int choose_pivot(int i,int j )
13.{
14.return((i+j) /2);
15.}
16. 
17.void quicksort(int list[],int m,int n)
18.{
19.int key,i,j,k;
20.if( m < n)
21.{
22.k = choose_pivot(m,n);
23.swap(&list[m],&list[k]);
24.key = list[m];
25.i = m+1;
26.j = n;
27.while(i <= j)
28.{
29.while((i <= n) && (list[i] <= key))
30.i++;
31.while((j >= m) && (list[j] > key))
32.j--;
33.if( i < j)
34.swap(&list[i],&list[j]);
35.}
36.// swap two elements
37.swap(&list[m],&list[j]);
38.// recursively sort the lesser list
39.quicksort(list,m,j-1);
40.quicksort(list,j+1,n);
41.}
42.}
43.void printlist(int list[],int n)
44.{
45.int i;
46.for(i=0;i<n;i++)
47.printf("%d\t",list[i]);
48.}
49. 
50.void main()
51.{
52.const int MAX_ELEMENTS = 10;
53.int list[MAX_ELEMENTS];
54. 
55.int i = 0;
56. 
57.// generate random numbers and fill them to the list
58.for(i = 0; i < MAX_ELEMENTS; i++ ){
59.list[i] = rand();
60.}
61.printf("The list before sorting is:\n");
62.printlist(list,MAX_ELEMENTS);
63. 
64.// sort the list using quicksort
65.quicksort(list,0,MAX_ELEMENTS-1);
66. 
67.// print the result
68.printf("The list after sorting using quicksort algorithm:\n");
69.printlist(list,MAX_ELEMENTS);
70.}
Qcksort, quick sort [array]

#include <stdio.h>

#define MAXARRAY 10

void quicksort(int arr[], int low, int high);

int main(void) {

int array[MAXARRAY] = {0};

int i = 0;

/* load some random values into the array */

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

array[i] = rand() % 100;

/* print the original array */

printf("Before quicksort: ");

for(i = 0; i < MAXARRAY; i++) {

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

printf("\n");

quicksort(array, 0, (MAXARRAY - 1));


/* print the `quicksorted' array */

printf("After quicksort: ");

for(i = 0; i < MAXARRAY; i++) {

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

printf("\n");

return 0;

/* sort everything inbetween `low' <-> `high' */

void quicksort(int arr[], int low, int high) {

int i = low;

int j = high;

int y = 0;

/* compare value */

int z = arr[(low + high) / 2];

/* partition */

do {

/* find member above ... */

while(arr[i] < z) i++;

/* find element below ... */

while(arr[j] > z) j--;


if(i <= j) {

/* swap two elements */

y = arr[i];

arr[i] = arr[j];

arr[j] = y;

i++;

j--;

} while(i <= j);

/* recurse */

if(low < j)

quicksort(arr, low, j);

if(i < high)

quicksort(arr, i, high);

You might also like