Quick Sort - Implementation in C

Quick Sort - Implementation in C



Like Merge Sort, Quick Sort is also based on divide and conquer algorithm. It piccks an element as pivot and partitions the array around the pivot element.


Algorithm For Quick Sort

Basically two functions are required to perform quick sort
a. Quicksort(arr, low, high)
b. Partition(arr, low, high)



Quicksort(arr, low, high) 

Here arr is an array of size n, low is lowerbound of array i..e. 0 and high is n-1.

  1. The condition is checked for low < high.
  2. If the condition is true, then in a variable named test, the returning value of function partition is assigned. 
  3. Also a recursive function Quicksort is called with parameters - base address, lower bound and test - 1.
  4. Also a recursive function Quicksort with parameters - base address, test +1, and upperbound is called.

Partition(arr[], low, high)

Partition is a function that is called to divide the array into wo parts around the pivot point after iteration.

  1. The last element of array is assumed to be the pivot element..
  2. (low-1) i.e. 0 is assigned to a variable 'i'.
  3. A loop is run from j=low to j<=high-1.
  4. Inside the loop, if the jth element of array is smaller than the pivot element, the value of 'i' is incremented by 1 and the values of ith element of array is swapped with jth element.
  5. After the first iteration, the values of pivot element and the (i+1)th element are swapped.
  6. The value of (i+1) is returned by the function.



PROGRAM

SHARE If you find this useful, please share with your friends and Community.
CODE TOGETHER..GROW TOGETHER.
Newer Posts Newer Posts Older Posts Older Posts

More posts

Comments

Post a Comment

Sponsored Content