0% found this document useful (0 votes)
14 views13 pages

Introduction of Sorting Techniques

Uploaded by

Apoorv Mishra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views13 pages

Introduction of Sorting Techniques

Uploaded by

Apoorv Mishra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

There are mainly three steps in the algorithm.

1. Choose a pivot

2. Partition the array around pivot. After partition, it is ensured that all
elements are smaller than all right and we get index of the end point of
smaller elements. The left and right may not be sorted individually.

3. Recursively call for the two partitioned left and right subarrays.

4. We stop recursion when there is only one element is left.


Choice of Pivot:
There are many different choices for picking pivots.

*Always pick the first (or last) element as a pivot.


The below implementation is picks the last element as pivot. The problem
with this approach is it ends up in the worst case when array is already sorted.

*Pick a random element as a pivot.


This is a preferred approach because it does not have a pattern for which the
worst case happens.

*Pick the median element is pivot.


This is an ideal approach in terms of time complexity as we can find median
in linear time and the partition function will always divide the input array into
two halves. But it is low on average as median finding has high constants.
4. Merge Sort
The Merge Sort algorithm is a sorting algorithm that is based on
the Divide and Conquers paradigm. In this algorithm, the array is initially
divided into two equal halves and then they are combined in a sorted
manner.
Let’s see how Merge Sort uses Divide and Conquer:
The merge sort algorithm is an implementation of the divide and conquers
technique. Thus, it gets completed in three steps:
1.Divide: In this step, the array/list divides itself recursively into sub-
arrays until the base case is reached.
2.Conquer: Here, the sub-arrays are sorted using recursion.
3.Combine: This step makes use of the merge( ) function to combine the
sub-arrays into the final sorted array.
Let’s look at the working of above example:
Divide:
[38, 27, 43, 10] is divided into [38, 27 ] and [43, 10] .
[38, 27] is divided into [38] and [27] .
[43, 10] is divided into [43] and [10] .

Conquer:
[38] is already sorted.
[27] is already sorted.
[43] is already sorted.
[10] is already sorted.

Merge:
Merge [38] and [27] to get [27, 38] .
Merge [43] and [10] to get [10,43] .
Merge [27, 38] and [10,43] to get the final sorted list [10, 27, 38, 43]
Therefore, the sorted list is [10, 27, 38, 43] .
Working of Merge Sort algorithm:
To know the functioning of merge sort, lets consider an array arr[] = {38, 27, 43, 3, 9, 82, 10}

At first, check if the left index of array is less than the right index, if yes then calculate its mid point
Now, as we already know that merge sort first divides the whole array iteratively into equal halves,
unless the atomic values are achieved. Here, we see that an array of 7 items is divided into two arrays
of size 4 and 3 respectively.
Now, again find that is left index is less than the right index for both arrays, if found yes, then again
calculate mid points for both the arrays.
Now, further divide these two arrays into further halves, until the atomic units of the array is
reached and further division is not possible.
After dividing the array into smallest units, start merging the elements again based on comparison
of size of elements
Firstly, compare the element for each list and then combine them into another list in a sorted
manner.
After the final merging, the list looks like this:
L[i] = arr[l + i];
for (j = 0; j < n2; j++)
R[j] = arr[m + 1 + j];

// Merge the temp arrays back into arr[l..r


i = 0;
j = 0;
k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
}
else {
arr[k] = R[j];
j++;
}
k++;
}

// Copy the remaining elements of L[],


// if there are any
while (i < n1) {
arr[k] = L[i];
i++;
k++;
}

// Copy the remaining elements of R[],


// if there are any
while (j < n2) {
arr[k] = R[j];

You might also like