Write a C Program to implement Quick Sort using recursion

By | 27.11.2016

Quick Sort using recursion


Write a C Program to implement Quick Sort using recursion. Here’s simple Program to implement Quick Sort using recursion in C Programming Language.


Recursion : :


  • Recursion is the process of repeating items in a self-similar way. In programming languages, if a program allows you to call a function inside the same function, then it is called a recursive call of the function.
  • The C programming language supports recursion, i.e., a function to call itself. But while using recursion, programmers need to be careful to define an exit condition from the function, otherwise it will go into an infinite loop.
  • Recursive functions are very useful to solve many mathematical problems, such as calculating the factorial of a number, generating Fibonacci series, etc.

Problem : :


The following C program, using recursion, performs quick sort. A quick sort is a sorting algorithm with complexity of O( nlogn ). It is used for sorting numbers, structure, files.

Here is the source code of the C Program to implement Quick Sort using recursion. The C Program is successfully compiled and run on a Windows system. The program output is also shown below.


SOURCE CODE : :


/*  C Program to implement Quick Sort using recursion  */

#include <stdio.h>

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

int main()
{
    int list[50];
    int size, i;

    printf("How many elements u want to Sort :: ");
    scanf("%d", &size);

    printf("\nEnter the elements below to be sorted :: \n");

    for (i = 0; i < size; i++)
    {
         printf("\nEnter [ %d ] element :: ",i+1);
        scanf("%d", &list[i]);
    }

    quicksort(list, 0, size - 1);

    printf("\nAfter implementing Quick sort, Sorted List is :: \n\n");

    for (i = 0; i < size; i++)
    {
        printf("%d  ", list[i]);
    }

    printf("\n");

    return 0;
}

void quicksort(int list[], int low, int high)
{
    int pivot, i, j, temp;
    if (low < high)
    {
        pivot = low;
        i = low;
        j = high;
        while (i < j)
        {
            while (list[i] <= list[pivot] && i <= high)
            {
                i++;
            }
            while (list[j] > list[pivot] && j >= low)
            {
                j--;
            }
            if (i < j)
            {
                temp = list[i];
                list[i] = list[j];
                list[j] = temp;
            }
        }
        temp = list[j];
        list[j] = list[pivot];
        list[pivot] = temp;
        quicksort(list, low, j - 1);
        quicksort(list, j + 1, high);
    }
}

Output : : 


/*  C Program to implement Quick Sort using recursion  */

How many elements u want to Sort :: 8

Enter the elements below to be sorted ::

Enter [ 1 ] element :: 3

Enter [ 2 ] element :: 1

Enter [ 3 ] element :: 5

Enter [ 4 ] element :: 2

Enter [ 5 ] element :: 8

Enter [ 6 ] element :: 0

Enter [ 7 ] element :: 6

Enter [ 8 ] element :: 9

After implementing Quick sort, Sorted List is ::

0  1  2  3  5  6  8  9

Process returned 0

Above is the source code for C Program to implement Quick Sort using recursion which is successfully compiled and run on Windows System.The Output of the program is shown above .

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach up to you in short interval.


Thanks for reading the post….

5 1 vote
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments