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.
- The condition is checked for low < high.
- If the condition is true, then in a variable named test, the returning value of function partition is assigned.
- Also a recursive function Quicksort is called with parameters - base address, lower bound and test - 1.
- 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.
- The last element of array is assumed to be the pivot element..
- (low-1) i.e. 0 is assigned to a variable 'i'.
- A loop is run from j=low to j<=high-1.
- 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.
- After the first iteration, the values of pivot element and the (i+1)th element are swapped.
- 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.
CODE TOGETHER..GROW TOGETHER.
Newer Posts
Newer Posts
Older Posts
Older Posts
Comments