Assignment 2 data structures National University Of Modern Languages
1. National University of Modern
Languages
Department of Computer Sciences
Merge Sort
void ArrayList::mergeSort()
{
if (currentIndex > 1)
{
mergeSortHelper(0, currentIndex - 1);
}
}
void ArrayList::mergeSortHelper(int left, int right)
{
if (left < right)
{
int mid = left + (right - left) / 2;
mergeSortHelper(left, mid); // Sort left half
Instructor: Zainab Malik
Due Date : 10/05/2025
Student Roll Name: Iffat
Obtained Marks:
Subject: Data Structures
Assignment No: 2
Class: BSCS 3A
Student Roll No: 3357
Total Marks: 25
2. mergeSortHelper(mid + 1, right); // Sort right half
merge(left, mid, right); // Merge sorted halves
}
}
void ArrayList::merge(int left, int mid, int right)
{
int nL = mid - left + 1; // Size of left subarray
int nR = right - mid; // Size of right subarray
int *L = new int[nL]; // Dynamically allocate L
int *R = new int[nR]; // Dynamically allocate R
for (int i = 0; i < nL; i++)
{
L[i] = arr[left + i]; // Copy left subarray
}
for (int j = 0; j < nR; j++)
{
R[j] = arr[mid + 1 + j]; // Copy right subarray
}
int i = 0, j = 0, k = left;
while (i < nL && j < nR)
{ // Merge by comparing
if (L[i] <= R[j]) arr[k] = L[i], i++;
else arr[k] = R[j], j++;
k++;
}
while (i < nL)
{
arr[k] = L[i];
i++;
k++; // Copy remaining L
}
while (j < nR)
{
arr[k] = R[j];
j++;
k++; // Copy remaining R
}
delete[] L; // Free memory
delete[] R; // Free memory
}
3. Dry Run
Let’s say we have an array of 10 elements:
currentIndex=10
arr= [55, 23, 78, 14, 66, 31, 92, 45, 19, 87]
The mergeSort function will sort arr[0] to arr[currentIndex-1] (indices 0 to 9).
First it will call mergeSortHelper(0,9)
First Iteration of mergeSortHelper( ):
left=0;
right=9;
as 0<9
mid=0+(9-0)/2 =4 (as it is an int)
it keeps dividing until array is divided into sub arrays of single element
Table: Merge Sort Process :
This table tracks what happens when we hit the base case and start merging. It’s just for the first
part (indices 0 to 2) to keep it simple.
Function Call left right mid Action Subarrays Result Array State
1 MS(0, 2) 0 2 1 Calculate mid, call
MS(0, 1), call MS(2,
2)
- - [55, 23, 78, 14,
66, 31, 92, 45, 19,
87]
2 MS(0, 1) 0 1 0 Calculate mid, call
MS(0, 0), call MS(1,
1)
- - [55, 23, 78, 14,
66, 31, 92, 45, 19,
87]
3 MS(0, 0) 0 0 - left == right, return [55] Base
Case
[55, 23, 78, 14,
66, 31, 92, 45, 19,
87]
4 MS(1, 1) 1 1 - left == right, return [23] Base
Case
[55, 23, 78, 14,
66, 31, 92, 45, 19,
87]
4. 5 MS(0, 1) 0 1 0 Both done, call
merge(0, 0, 1)
[55], [23] [23, 55] [23, 55, 78, 14,
66, 31, 92, 45, 19,
87]
6 MS(2, 2) 2 2 - left == right, return [78] Base
Case
[23, 55, 78, 14,
66, 31, 92, 45, 19,
87]
7 MS(0, 2) 0 2 1 Both done, call
merge(0, 1, 2)
[23, 55], [78] [23, 55,
78]
[23, 55, 78, 14,
66, 31, 92, 45, 19,
87]
First Two Merges
Merge Call 1: merge(0, 0, 1)
Parameters: left=0, mid=0, right=1, nL = mid - left + 1 = 1, nR = right - mid = 1.
Subarrays: L = [55] (from arr[0]), R = [23] (from arr[1]).
Initial Array State: [55, 23, 78, 14, 66, 31, 92, 45, 19, 87].
Merge
Call
Loop
Executed
i j k n
L
n
R
Condition Action Array State
1 merge(0, 0,
1)
for (copy L) 0 - - 1 1 i < nL (0 < 1) Copy L[0] = arr[0] =
55
[55, 23, 78, 14, 66,
31, 92, 45, 19, 87]
2 merge(0, 0,
1)
for (copy R) - 0 - 1 1 j < nR (0 < 1) Copy R[0] = arr[1] =
23
[55, 23, 78, 14, 66,
31, 92, 45, 19, 87]
3 merge(0, 0,
1)
while (Loop
1)
0 0 0 1 1 i < nL && j <
nR (true)
L[0] > R[0] (55 > 23),
arr[0] = 23, j++
[23, 23, 78, 14, 66,
31, 92, 45, 19, 87]
4 merge(0, 0,
1)
while (Loop
1)
0 1 1 1 1 i < nL && j <
nR (false)
Loop 1 ends (j == nR) [23, 23, 78, 14, 66,
31, 92, 45, 19, 87]
5 merge(0, 0,
1)
while (Loop
2)
0 1 1 1 1 i < nL (0 < 1) arr[1] = L[0] = 55, i++ [23, 55, 78, 14, 66,
31, 92, 45, 19, 87]
6 merge(0, 0,
1)
while (Loop
2)
1 1 2 1 1 i < nL (1 < 1,
false)
Loop 2 ends [23, 55, 78, 14, 66,
31, 92, 45, 19, 87]
7 merge(0, 0,
1)
while (Loop
3)
1 1 2 1 1 j < nR (1 < 1,
false)
Loop 3 does not
execute
[23, 55, 78, 14, 66,
31, 92, 45, 19, 87]
Result of merge(0, 0, 1): arr[0..1] is now [23, 55].
Merge Call 2: merge(3, 3, 4)
7. Quick Sort
void ArrayList::quickSort()
{
if (currentIndex > 1)
{
quickSortHelper(0, currentIndex - 1);
}
}
void ArrayList::quickSortHelper(int left, int right)
{
if (left < right)
{
int pivotIndex = partition(left, right);
quickSortHelper(left, pivotIndex - 1); // Sort left partition
quickSortHelper(pivotIndex + 1, right); // Sort right partition
}
}
int ArrayList::partition(int left, int right)
{
int pivot = arr[left]; // Use first element as pivot
int i = left + 1; // Start i after the pivot
for (int j = left + 1; j <= right; j++)
{
if (arr[j] < pivot) {
swap(i, j);
i++;
}
}
swap(left, i - 1); // Place pivot in its final position
return i - 1; // Return the pivot's final index
}
Let’s say we have an array arr of 10 elements:
[55, 23, 78, 14, 66, 31, 92, 45, 19, 87]
currentIndex=10
Quick sort function will sort the array
8. Quicksort function calls quicksort helper function with
left=0
right=9
it will call quickSortHelper(0,9)
First Iteration of quickSortHelper( ):
we have left=0
right=9
as (0<9)
this will call partition function
pivotIndex=partition(0,9)
Initialize:
o pivot = arr[0] = 55.
o i = left + 1 = 1.
o j loops from 1 to 9.
Dry run of partition loop:
Ste
p
j arr[j
]
arr[j] <
pivot (55)
Action i Array State After
Step
1 1 23 True Swap arr[1] and arr[1]: No change 2 [55, 23, 78, 14, 66,
31, 92, 45, 19, 87]
2 2 78 False No swap 2 [55, 23, 78, 14, 66,
31, 92, 45, 19, 87]
3 3 14 True Swap arr[2] and arr[3]: [55, 23,
14, 78, ...]
3 [55, 23, 14, 78, 66,
31, 92, 45, 19, 87]
4 4 66 False No swap 3 [55, 23, 14, 78, 66,
31, 92, 45, 19, 87]
5 5 31 True Swap arr[3] and arr[5]: [55, 23,
14, 31, 66, 78, ...]
4 [55, 23, 14, 31, 66,
78, 92, 45, 19, 87]
6 6 92 False No swap 4 [55, 23, 14, 31, 66,
78, 92, 45, 19, 87]
7 7 45 True Swap arr[4] and arr[7]: [55, 23,
14, 31, 45, 78, 92, 66, ...]
5 [55, 23, 14, 31, 45,
78, 92, 66, 19, 87]
8 8 19 True Swap arr[5] and arr[8]: [55, 23,
14, 31, 45, 19, 92, 66, 78, ...]
6 [55, 23, 14, 31, 45,
19, 92, 66, 78, 87]
9 9 87 False No swap 6 [55, 23, 14, 31, 45,
19, 92, 66, 78, 87]
9. Place Pivot:
Swap arr[left=0] and arr[i-1=5]: [55, 23, 14, 31, 45, 19, 92, 66, 78, 87] → [19, 23, 14, 31,
45, 55, 92, 66, 78, 87].
pivotIndex = i - 1 = 5.
Array After Partition: [19, 23, 14, 31, 45, 55, 92, 66, 78, 87].
quickSortHelper(0, 4)
Current Array: [19, 23, 14, 31, 45, 55, 92, 66, 78, 87]
Call: quickSortHelper(0, 4) (from the first iteration’s left partition)
Step 1: Check Condition
o left = 0, right = 4, 0 < 4 is true, proceed to partition.
o Subarray to sort: [19, 23, 14, 31, 45] (indices 0 to 4).
Step 2: Call partition(0, 4)
o Initialize:
pivot = arr[0] = 19.
i = left + 1 = 1.
j loops from 1 to 4.
Ste
p
j arr[j
]
arr[j] < pivot
(19)
Action i Array State After
Step
1 1 23 False No swap 1 [19, 23, 14, 31, 45, 55, 92,
66, 78, 87]
2 2 14 True Swap arr[1] and arr[2]: [19, 14,
23, 31, 45, ...]
2 [19, 14, 23, 31, 45, 55, 92,
66, 78, 87]
3 3 31 False No swap 2 [19, 14, 23, 31, 45, 55, 92,
66, 78, 87]
4 4 45 False No swap 2 [19, 14, 23, 31, 45, 55, 92,
66, 78, 87]
o Place Pivot:
Swap arr[left=0] and arr[i-1=1]: [19, 14, 23, 31, 45, 55, 92, 66, 78, 87] → [14,
19, 23, 31, 45, 55, 92, 66, 78, 87].
pivotIndex = i - 1 = 1.
o Array After Partition: [14, 19, 23, 31, 45, 55, 92, 66, 78, 87].