DUET PG Score Card 2021
NHAI Recruitment 2021
Bihar Board 12th Dummy Admit Card 2022
RBI Assistant Notification 2021
Jee Advanced 2021 Result
NIACL Assistant Recruitment 2021
SBI PO Online Form 2021

Quick Sort In C Language Quick Sort Algorithm

Quick Sort In C: Quick Sort is one of the fastest internal sorting algorithms.

It is based on the following three main strategies:

  1. Split or Partition: Select a random element called pivot from the sequence of elements to be sorted. Suppose the selected element is X, where X is any number. Now split (divide) the list into the two small lists, viz. Y and Z such that:
    • All the elements of the first part Y are less then the selected element pivot.
    • All the elements of the second part Z are greater then the selected element pivot.
  2. Sort the sub-arrays.
  3. Merge (join/concatenate) the sorted sub-array.

Quick Sort In C

if p < r
then: q = PARTITION ( A, p, r)
          QUICK_SORT (A, p, q-1)  
          QUICK_SORT (A, q+1, r)
Set x = A[r].
Set i = p-1.
Repeat Step 4 to 6 for j = p to r-1
      do if A[j] <= x.
           then Set i = i+1.
                   Exchange A[i] <=>A[j]
[End of Loop]
Exchange A[i+1] <=> A[r]     
return i+1
#include<stdio.h>
#include<stdlib.h>

void quicksort (int a[], int start, int end)
{
  if (start < end)
    {
      int q = partition (a, start, end);
      quicksort (a, start, q - 1);
      quicksort (a, q + 1, end);
    }
}

int partition (int a[], int start, int end)
{
  int pivot = a[end];
  int pindex = start;
  int i;
  for (i = start; i < end; i++)
    {
      if (a[i] <= pivot)
	{
	  swap (&a[i], &a[pindex]);
	  pindex++;
	}

    }
  swap (&a[pindex], &a[end]);
  return pindex;

}

void swap (int *a, int *b)
{
  int temp = *a;
  *a = *b;
  *b = temp;
}

main ()
{
  int arr[10];
  int i;
  printf ("Enter 10 elements ");
  for (i = 0; i < 10; i++)
    {
      scanf ("%d", &arr[i]);
    }
  int n = 10;
  quicksort (arr, 0, n - 1);
  printf ("Sorted 10 elements ");
  for (i = 0; i < 10; i++)
    {
      printf ("%d ", arr[i]);
    }
}

Candidates having any kind of query so he/she can ask in our comment section. Our Panel will b reverting back as soon as possible. Thank You. For more details visit our website (https://speedjobalert.com/)

Leave a Comment