0% found this document useful (0 votes)
7 views7 pages

Quicksort & Mergesort

The document explains the Quicksort and Mergesort algorithms, detailing their divide-and-conquer processes for sorting arrays. Quicksort involves partitioning an array around a pivot, with a worst-case performance of O(n²) and best/average case of O(n log n), while Mergesort consistently performs at O(n log n) for all cases. It includes C implementations for both algorithms, highlighting key functions such as partitioning and merging.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views7 pages

Quicksort & Mergesort

The document explains the Quicksort and Mergesort algorithms, detailing their divide-and-conquer processes for sorting arrays. Quicksort involves partitioning an array around a pivot, with a worst-case performance of O(n²) and best/average case of O(n log n), while Mergesort consistently performs at O(n log n) for all cases. It includes C implementations for both algorithms, highlighting key functions such as partitioning and merging.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

QUICKSORT

Quicksort applies the divide-and-conquer paradigm. Here is the three-step divide-


and-conquer process for sorting a typical subarray A[p…r] :

Divide: Partition (rearrange) the array A[p…r] into two sub-arrays A[p…q-1] and
A[q+1…r] such that each element of A[p…q-1] is less than or equal to
A[q], which is, in turn, less than or equal to each element of A[q+1…r].
Compute the index q as part of this partitioning procedure.

Conquer: Sort the two sub-arrays A[p…q-1] and A[q+1…r] by recursive calls to
quicksort.

Combine: Because the subarrays are already sorted, no work is needed to combine
them: the entire array A[p…r] is now sorted.

The following procedure implements quicksort:

QUICKSORT(A, p, r)
1 if p < r
2 q = PARTITION(A, p, r)
3 QUICKSORT(A, p, q – 1)
4 QUICKSORT(A, q + 1, r)

To sort an entire array A, the initial call is QUICKSORT(A, 1, A.length).

Partitioning the Array

The key to the algorithm is the PARTITION procedure, which rearranges the
subarray A[p…r] in-place.

PARTITION (A, p, r)
1. While data[to_big_index] <= data[pivot]
to_big_index = to_big_index + 1
2. While data[to_small_index] > data[pivot]
to_small_index = to_small_index - 1
3. If to_big_index < to_small_index
swap data[to_big_index] and data[to_small_index]
4. While to_small_index > to_big_index, go to 1.
5. Swap data[to_small_index] and data[pivot_index]

Performance

Worst case performance O(n2)


Best case performance O(nlogn)
Average case performance O(nlogn)

1
How it works:
1. Choose a value in the array to be the pivot element.
2. Order the rest of the array so that lower values than the pivot element are on the
left, and higher values are on the right.
3. Swap the pivot element with the first element of the higher values so that the pivot
element lands in between the lower and higher values.
4. Do the same operations (recursively) for the sub-arrays on the left and right side
of the pivot element.

How to choose the pivot element:


Approach 1: You may pick the first element as the pivot.
Approach 2: You may pick the last element as the pivot.
Approach 3: You may pick the middle element as the pivot.

Quicksort implementation in C

#include <stdio.h>

//Prototype decleration
void swap(int*, int*);
void printArray(int[], int);
int partition(int[], int, int);
void quickSort(int[], int, int);

// Driver Code
int main()
{
int arr[] = { 70, 6, 10, 50, 9, 2, 1, 15, 70 };
int n = sizeof(arr) / sizeof(arr[0]);
quickSort(arr, 0, n - 1);
printf("Sorted array: \n");
printArray(arr, n);
return 0;
}

/* This function takes first element as pivot, the function


places the pivot element (first element) on its sorted
position and all the element lesser than or equal to the pivot
are placed left to it, and all the elements greater than the
pivot are placed right to it.*/

int partition(int arr[], int low, int high)


{
// First element as pivot
int pivot = arr[low];
int st = low; // st points to the start of sub-array
int end = high; // end points to the end of the sub-array
int k = high;
for (int i = high; i > low; i--)
{
if (arr[i] > pivot)

2
{
swap(&arr[i], &arr[k]);
k--;
}
}
swap(&arr[low], &arr[k]);

// now pivot element is at its sorted position


// return pivot element index (end)

return k;
}

/* The main function that implements QuickSort


arr[] --> Array to be sorted,
low --> Starting index,
high --> Last index */

void quickSort(int arr[], int low, int high)


{
// If low is lesser than high
if (low < high)
{
// idx is index of pivot element which is at its
// sorted position
int idx = partition(arr, low, high);

// Separately sort elements before


// partition and after partition

quickSort(arr, low, idx - 1);


quickSort(arr, idx + 1, high);
}
}

/* Function to print array */


void printArray(int arr[], int size)
{

/*Function to swap two int variables*/


void swap(int *a, int *b)
{

Time complexity
The time complexity of the Quick Sort algorithm is best and average case O(n*log n),
while the worst case is O(n2), where n represents the number of items being sorted.

3
MERGESORT

MERGESORT (A, p, r)
1 if p < r
2 q = (p + r)/2
3 MERGESORT (A, p, q)
4 MERGESORT (A, q+1, r)
5 MERGE (A, p, q, r)

4
Mergesort implementation in C

// Merge two subarrays L and M into arr


void merge(int arr[], int p, int q, int r)
{
// Create L ← A[p..q] and M ← A[q+1..r]
int n1 = q - p + 1;
int n2 = r - q;
int L[n1], M[n2];

for (int i = 0; i < n1; i++)


L[i] = arr[p + i];
for (int j = 0; j < n2; j++)
M[j] = arr[q + 1 + j];

// Maintain current index of sub-arrays and main array


int i, j, k;
i = 0;

5
j = 0;
k = p;

// Until we reach either end of either L or M, pick larger among


// elements of L and M and place them in the correct position at A[p..r]
while (i < n1 && j < n2)
{
if (L[i] <= M[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = M[j];
j++;
}
k++;
}
// When we run out of elements in either L or M,
// pick up the remaining elements and put in A[p..r]
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}
while (j < n2)
{
arr[k] = M[j];
j++;
k++;
}
}

// Divide the array into two subarrays, sort them and merge them
void mergeSort(int arr[], int l, int r)
{
if (l < r)
{
// m is the point where the array is divided into two subarrays
int m = l + (r - l) / 2;

mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);

// Merge the sorted subarrays


merge(arr, l, m, r);
}
}

6
Time complexity

The Time Complexity of merge sort for Best case, average case and worst case
is O(n*log n), where n represents the number of items being sorted.

You might also like