C tutorial - Quick sort in C programming language

Quick Sort:

   Quicksort is a highly efficient sorting algorithm.
    A large array is divided or partitioned into Two arrays. one of each holde value smaller than the specified value say pivot based on which the partition is made and another array holds values greater than pivot value.
      Quick sort is the fastest internal sorting algorithm with the time complexity O( n log n).
       Quick sort it is a comparison sort, meaning that it can salt terms of any type for which less than relation is defined.


Program for Quick Sort:-


#include <stdio.h>
 
void quicksort (int [], int, int);
 
int main()
{
    int list[50];
    int size, i;
 
    printf("Enter the number of elements: ");
    scanf("%d", &size); 
    printf("Enter the elements to be sorted:\n");
    for (i = 0; i < size; i++)
    {
        scanf("%d", &list[i]);
    } 
    quicksort(list, 0, size - 1);
    printf("After applying quick sort\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);
    }
}




Post a Comment

0 Comments