DSA - Lab4 Sorting Techniques2
DSA - Lab4 Sorting Techniques2
Lab Manual 04
CS201: Data Structure and Algorithms
Class: BSCS-2k21
Lab 4: Sorting techniques-II
Instructors:
Mr. Awais Mehmood
awais.mehmood@uettaxila.edu.pk
&
Mr. M. Faheem Saleem
muhammad.faheem@uettaxila.edu.pk
Lab Manual 4
Introduction
This lab is in continuation to the previous lab which is about sorting Operations on Arrays in
C++.
Objectives
The objectives of this session is to understand the various sorting operations on arrays in C++.
Tools/Software Requirement
Dev C++
Goals for today’s lab:
Sorting Arrays:-
The process of arranging data in a specified order is called sorting. Numeric type data may be
arranged either in ascending or in descending order. Similarly character type data may be
arranged in alphabetical order.
There are different methods to sort data into a list. The most commonly used methods are:
1. Merge Sort
2. Quick Sort
1) Merge Sort
Think of it as a recursive algorithm that continuously splits the array in half until
it cannot be further divided. This means that if the array becomes empty or has
only one element left, the dividing will stop, i.e., it is the base case to stop the
recursion. If the array has multiple elements, split the array into halves and
recursively invoke the merge sort on each of the halves. Finally, when both
halves are sorted, the merge operation is applied. Merge operation is the
process of taking two smaller sorted arrays and combining them to eventually
make a larger one.
Illustration:
To know the functioning of merge sort, let’s 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 are 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.
Algo:
Algo:
step 1: start
step 2: declare array and left, right, mid variable
step 3: perform merge function.
if left > right
return
mid= (left+right)/2
mergesort(array, left, mid)
mergesort(array, mid+1, right)
merge(array, left, mid, right)
step 4: Stop
Follow the steps below the solve the problem:
MergeSort(arr[], l, r)
If r > l
• Find the middle point to divide the array into two halves:
• middle m = l + (r – l)/2
• Call mergeSort for first half:
• Call mergeSort(arr, l, m)
• Call mergeSort for second half:
• Call mergeSort(arr, m + 1, r)
• Merge the two halves sorted in steps 2 and 3:
• Call merge(arr, l, m, r)
Class task:
Implement merge sort
Home task:
Write algo of quick sort and implement it