C Menu9>
- Basics of C
- Basics of C Programming
- Features of C Programming
- Preprocessors in C Programming
- History of C Programming
- Low Level Programming
- Why C is Middle Level Language
- Introduction to C programming
- Structure of a C program
- Input-Output Functions
- Difference between Compiler and Interpreter
- Assembler v/s Compiler v/s Interpreter v/s Linker v/s Loader
- Basics to Code
- Operators in C
- Operators in C
- Precedence Associativity Operators
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Bitwise Operators
- Assignment Operators
- Ternary Operators in C
- Size of Operators
- Conditional Operators
- Comma Operators
- Format Specifiers in C
- Difference between %d and %i
- Difference between %f, %e, %E and %g
- How to print% using printf
- How to print \ using printf
- How to print “” using printf
- What is the use of %p in C
- Library Functions in C
- pow() in C
- sqrt() in C
- Storage Classes in C
- Conditional statements and Decision Making in C
- DataType & Functions in C
- Arrays, String, Structure, Union and Enum
- Pointer in C
- Library Function in C
- Library function in C
- math.h in c
- Library function math.h acos
- Library function math.h acosh
- Library function math.h asin
- Library function math.h asinh
- Library function math.h atan
- Library function math.h atan2
- Library function math.h atanh
- Library function math.h cbrt
- Library function math.h cell
- Library function math.h cos
- Library function math.h cosh
- Library function math.h exp
- Library function math.h fabs
- Library function math.h floor
- Library function math.h hypot
- Library function math.h log
- Library function math.h log10
- Library function math.h pow
- Library function math.h sin
- Library function math.h sinh
- Library function math.h sqrt
- Library function math.h tan
- Library function math.h tanh
- Library function studio.h clearerr
- Library function string.h strcat
- Library function string.h strcmp
- Library function string.h strcpy
- Library function string.h strlen
- File Handling
- Dynamic Memory Allocation
- Miscellaneous Topics in C
- Miscellaneous Coding Questions
PREPINSTA PRIME
Quick Sort in C
How Quick Sort works in C
Quick sort is an algorithm of the divide and conquer type. That is,the problem of sorting a set is reduced of the problem of sorting two smaller sets.
Space Complexity is O(1) since we didn't need any extra array, since its in-place algorithm that is are all sorted within the original array itself.
The Auxiliary space complexity is O(log n) due to function call stack.
| Time Complexity | O(n log n) |
| Best Case | O(n log n) |
| Worst Case | O(n2) |
| Space Complexity | O(1) |
| Auxiliary Space Complexity | O(log n) |
| In Place | Yes |
| Stable | No |
How it works?
Quick sort works in the following way –
- Choose an item from array called as pivot
- Move all the elements smaller than pivot to left partition
- Move all the elements greater than pivot to right partition.
Choose new pivot item in each partition and keep doing the same process again until partition of one element each aren’t formed.
How to choose Pivot?
These are the following ways you can choose pivot –
- First element as pivot.
- Last element as pivot (We use this in our examples & Code)
- Random element as pivot.
- Median element as pivot.
Execution of quick sort
Quicksort works recursively, Partitioning moves all smaller elements to the left of the pivot and greater to the right side of the pivot, thus each time two sub-array partitions are formed. Thus again, we do partitioning in each of these sub-arrays individually.
Following code below gives a perfect example for this –
QuickSort Algorithm
quickSort(arr[], low, high)
{
if (low < high)
{
/* indexPI is partitioning index, partition() function will
return index of partition */
indexPI = partition(arr, low, high);
quickSort(arr, low, indexPI - 1); // left partition
quickSort(arr, indexPI + 1, high); // right partition
}
}Partition Algorithm
partition (arr[], low, high)
{
// pivot element selected as right most element in array each time
pivot = arr[high];
swapIndex = (low - 1) //swapping index
for (j = low; j <= high- 1; j++)
{
// Check if current element is smaller than pivot element
if (arr[j] < pivot)
{
i++; // increment swapping index
swap arr[j] and arr[swapIndex]
}
}
swap arr[swapIndex + 1] and arr[high])
return (swapIndex + 1)
}
Code of Quick Sort in C
#include<stdio.h>
// A utility function to swap two elements
void swap(int* x, int* y)
{
int temp = *x;
*x = *y;
*y = temp;
}
/* Partition function to do Partition
elements on the left side of pivot elements would be smaller than pivot
elements on the right side of pivot would be greater than the pivot
*/
int partition (int array[], int low, int high)
{
// pivot element selected as right most element in array each time
int pivot = array[high];
int swapIndex = (low - 1); //swapping index
for (int j = low; j <= high- 1; j++)
{
// Check if current element is smaller than pivot element
if (array[j] < pivot)
{
swapIndex ++; // increment swapping index
swap(&array[swapIndex], &array[j]);
}
}
swap(&array[swapIndex + 1], &array[high]);
return (swapIndex + 1);
}
//Recursive function to apply quickSort
void quickSort(int array[], int low, int high)
{
if (low < high)
{
/* indexPI is partitioning index, partition() function will
return index of partition */
int indexPI = partition(array, low, high);
quickSort(array, low, indexPI - 1); // left partition
quickSort(array, indexPI + 1, high); // right partition
}
}
/* Function to display the array */
void display(int array[], int size)
{
int i;
for (i=0; i < size; i++)
printf("%d ", array[i]);
}
//Main function to run the program
int main()
{
int array[] = {70, 90, 10, 30, 50, 20, 60};
int size = sizeof(array)/sizeof(array[0]);
quickSort(array, 0, size-1);
printf("Array after Quick Sorting: ");
display(array, size);
return 0;
}Output
Array after Quick Sorting: 10 20 30 50 60 70 90
Facts about Quick Sort
- Quicksort is an in-place algorithm. In-place sorting means, it does not use additional storage space to perform sorting.
- The algorithm is efficient for large-sized data sets.
- The average or best-case complexity of quicksort is O(n log n).
- Worst-case time complexity is O(n^2)
- Cache Friendly: Quick Sort is also a cache-friendly sorting algorithm as it has a good locality of reference when used for arrays
Sorting
Sorting algorithms are easy to learn but are really important for college semester exams and companies offering package between 3 – 6 LPA would ask direct searching questions in online test/ interviews.

Login/Signup to comment