Merge and Quick
Merge and Quick
AND
QUICK SORT
MERGE SORT
Merge sort is a sorting
algorithm that follows the
divide-and-conquer
approach. It works by
recursively dividing the input
array into smaller subarrays
and sorting those subarrays
then merging them back
together to obtain the sorted
array.
In simple terms, we can say
that the process of merge
sort is to divide the array
into two halves, sort each
half, and then merge the
sorted halves back together.
This process is repeated until
#include <stdio.h>
#include <stdlib.h>
ALGORITHM
// Copy the remaining
// Merges two subarrays of
elements of L[],
arr[].
// if there are any
// First subarray is arr[l..m]
while (i < n1) {
// Second subarray is
arr[k] = L[i];
arr[m+1..r]
i++;
void merge(int arr[], int l, int
k++;
m, int r)
}
{
int i, j, k;
// Copy the remaining
int n1 = m - l + 1;
elements of R[],
int n2 = r - m;
// if there are any
while (j < n2) {
// Create temp arrays
arr[k] = R[j];
int L[n1], R[n2];
j++;
k++;
// Copy data to temp arrays
}
L[] and R[]
}
for (i = 0; i < n1; i++)
L[i] = arr[l + i];
// l is for left index and r is
for (j = 0; j < n2; j++)
right index of the
R[j] = arr[m + 1 + j];
// sub-array of arr to be sorted
// Merge the temp arrays
void mergeSort(int arr[], int l,
back into arr[l..r
int r)
i = 0;
{
j = 0;
if (l < r) {
k = l;
int m = l + (r - l) / 2;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
// Sort first and second
arr[k] = L[i];
halves
i++;
mergeSort(arr, l, m);
}
mergeSort(arr, m + 1, r);
else {
arr[k] = R[j];
Recurrence Relation of Merge Sort:
The recurrence relation of merge sort is:
T(n)=Θ(1), if n=1
T(n)=2T(n/2)+Θ(n), if n>1
•T(n) Represents the total time taken by the algorithm to sort an array of
size n.
•O(n) represents the time taken to merge the two sorted halves
• Best Case: O(n log n), When the array is already sorted or
nearly sorted.
• Average Case: O(n log n), When the array is randomly ordered.
• Worst Case: O(n log n), When the array is sorted in reverse
order.
•Space Complexity: O(n), Additional space is required for the temporary
array used during merging.
Advantages of Merge Sort:
•Stability : Merge sort is a stable sorting algorithm, which means it
maintains the relative order of equal elements in the input array.
•Inversion counting
•The merge function of merge sort to efficiently solve the problems like
union and intersection of two sorted arrays.
QUICK SORT
QuickSort is a sorting
algorithm based on the
Divide and Conquer that
picks an element as a pivot
and partitions the given array
around the picked pivot by
placing the pivot in its correct
position in the sorted array.
ALGORITHM
#include <stdio.h> // The QuickSort function
implementation
void swap(int* a, int* b); void quickSort(int arr[], int
low, int high) {
// Partition function if (low < high) {
int partition(int arr[], int low,
int high) { // pi is the partition
return index of pivot
// Choose the pivot int pi = partition(arr, low,
int pivot = arr[high]; high);
•It has a low overhead, as it only requires a small amount of memory to function.
•It is Cache Friendly as we work on the same array to sort and do not copy data
to any auxiliary array.
•Fastest general purpose algorithm for large data when stability is not required.
•It is tail recursive and hence all the tail call optimization can be done.
•It is not a stable sort, meaning that if two elements have the same key, their
relative order will not be preserved in the sorted output in case of quick sort,
because here we are swapping elements according to the pivot’s position
(without considering their original positions).
Applications of Quick Sort
•Efficient for sorting large datasets with O(n log n) average-case time
complexity.
Presente
d by-
SUDIP PATRA| NIRJAN MONDAL