# C Program for Quick Sort

**Problem:**Write a C program for quick sort. C program to accept and sort an array in ascending as well as descending order using quick sort algorithm.

**Procedure:**

We consider one element at a time and place it in its correct position. This element is called as pivot.

This pivot is placed such that all elements to the left of the pivot are less than the pivot and all elements to the right are greater. This partitions the array into two parts -left partition and right partition. The same method is applied for each of the partition. This process continues till no more partitions can be made

**Note: Any element can be chosen as pivot.**

We are considering the first element as pivot element. The recursive function is similar to merge sort. However, in quick sort, we do not divide into two equal parts but partition on the basis of the pivot element. The function sorts the elements a[lb] to a[ub] where lb=lower-bound and ub=upper-bound .

void QuickSort(int a[],int lb,int ub) { int j; if(lb<ub) { j=partition(a,lb,ub); QuickSort(a,lb,j-1); QuickSort(a,j+1,ub); } }Now we use two variables up and down for moving down and up the array in partition function.

### Algorithm For Partitioning:

- Start
- down=lb
- up=ub
- pivot=A[lb]
- while(A[down]<=pivot && down<ub)

down++ - while(A[up]>pivot && up>lb)

up-- - if(down<up)

interchange A[down] and A[up]

goto step 5 - Interchange A[up] and A[lb]
- return up
- stop.

## C Program for quick Sort:

**Get This Program:**