C language sort
C language sort
Quick Sort:
Like Merge Sort, Quick Sort is a Divide and Conquer algorithm. It picks an element as a
pivot and partitions the given array around the picked pivot.
There are mainly three steps in the algorithm:
1. Choose a Pivot: Select an element from the array as the pivot. The choice of pivot
can vary (e.g., first element, last element, random element, or median).
2. Partition the Array: Rearrange the array around the pivot. After partitioning, all
elements smaller than the pivot will be on its left, and all elements greater than
the pivot will be on its right. The pivot is then in its correct position, and we obtain
the index of the pivot.
3. Recursively Call: Recursively apply the same process to the two partitioned sub-
arrays (left and right of the pivot).
4. Base Case: The recursion stops when there is only one element left in the sub-
array, as a single element is already sorted.
Algorithm
/* low –> Starting index, high –> Ending index */
quickSort(arr[], low, high) {
if (low < high) {
/* pi is partitioning index, arr[pi] is now at right place */
pi = partition(arr, low, high);
quickSort(arr, low, pi – 1); // Before pi
quickSort(arr, pi + 1, high); // After pi
}
}
C Program
#include <stdio.h>
void swap(int *a, int *b) {
int t = *a;
*a = *b;
*b = t;
}
int partition(int array[], int low, int high) {
int pivot = array[high];
int i = (low - 1);
for (int j = low; j < high; j++) {
if (array[j] <= pivot) {
i++;
swap(&array[i], &array[j]);
}
}
swap(&array[i + 1], &array[high]);
return (i + 1);
}
// main function
int main() {
int data[] = {8, 7, 2, 1, 0, 9, 6};
int n = sizeof(data) / sizeof(data[0]);
printf("Unsorted Array\n");
printArray(data, n);
quickSort(data, 0, n - 1);
printf("Sorted array in ascending order: \n");
printArray(data, n);
}
Time Complexity
Best Case Complexity - In Quick sort, the best-case occurs when the pivot element is the
middle element or near to the middle element. The best-case time complexity of quick
sort is O(n*logn).
Average Case Complexity - It occurs when the array elements are in jumbled order that
is not properly ascending and not properly descending. The average case time
complexity of quick sort is O(n*logn).
Worst Case Complexity - In quick sort, worst case occurs when the pivot element is
either greatest or smallest element. Suppose, if the pivot element is always the last
element of the array, the worst case would occur when the given array is sorted already
in ascending or descending order. The worst-case time complexity of quick sort is O(n2).
Step-1:
Step-2:
Step-3:
Algorithm
Start from the leftmost element of arr[] and compare Key with each element of arr[]
If Key matches with an element, return the index.
If Key doesn’t match with any of the elements, return -1.
3. Binary Search:
Binary Search is a searching algorithm used in a sorted array by repeatedly dividing the
search interval in half of the array.
The idea of binary search is to use the information that the array is sorted and reduce
the time complexity to O(Log n).