SlideShare a Scribd company logo
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
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
}
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]
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)
 Parameters: left=3, mid=3, right=4, nL = mid - left + 1 = 1, nR = right - mid = 1.
 Subarrays: L = [14] (from arr[3]), R = [66] (from arr[4]).
 Initial Array State: [23, 55, 78, 14, 66, 31, 92, 45, 19, 87] (after previous merge).
Ste
p
Merge
Call
Loop
Executed
i j k n
L
n
R
Condition Action Array State
8 merge(3,
3, 4)
for (copy L) 0 - - 1 1 i < nL (0 < 1) Copy L[0] = arr[3] =
14
[23, 55, 78, 14, 66,
31, 92, 45, 19, 87]
9 merge(3,
3, 4)
for (copy
R)
- 0 - 1 1 j < nR (0 < 1) Copy R[0] = arr[4] =
66
[23, 55, 78, 14, 66,
31, 92, 45, 19, 87]
10 merge(3,
3, 4)
while (Loop
1)
0 0 3 1 1 i < nL && j <
nR (true)
L[0] <= R[0] (14 ≤
66), arr[3] = 14, i++
[23, 55, 78, 14, 66,
31, 92, 45, 19, 87]
11 merge(3,
3, 4)
while (Loop
1)
1 0 4 1 1 i < nL && j <
nR (false)
Loop 1 ends (i == nL) [23, 55, 78, 14, 66,
31, 92, 45, 19, 87]
12 merge(3,
3, 4)
while (Loop
2)
1 0 4 1 1 i < nL (1 < 1,
false)
Loop 2 does not
execute
[23, 55, 78, 14, 66,
31, 92, 45, 19, 87]
13 merge(3,
3, 4)
while (Loop
3)
1 0 4 1 1 j < nR (0 < 1) arr[4] = R[0] = 66, j+
+
[23, 55, 78, 14, 66,
31, 92, 45, 19, 87]
14 merge(3,
3, 4)
while (Loop
3)
1 1 5 1 1 j < nR (1 < 1,
false)
Loop 3 ends [23, 55, 78, 14, 66,
31, 92, 45, 19, 87]
Result of merge(3, 3, 4): arr[3..4] is now [14, 66].
Remaining Merge Iterations
Merge Call lef
t
mi
d
righ
t
Input Subarrays Sorted Result Array State After Merge
3 merge(0, 1,
2)
0 1 2 [23, 55], [78] [23, 55, 78] [23, 55, 78, 14, 66, 31,
92, 45, 19, 87]
4 merge(0, 2,
4)
0 2 4 [23, 55, 78], [14, 66] [14, 23, 55, 66, 78] [14, 23, 55, 66, 78, 31,
92, 45, 19, 87]
5 merge(5, 5,
6)
5 5 6 [31], [92] [31, 92] [14, 23, 55, 66, 78, 31,
92, 45, 19, 87]
6 merge(5, 6, 5 6 7 [31, 92], [45] [31, 45, 92] [14, 23, 55, 66, 78, 31,
7) 45, 92, 19, 87]
7 merge(8, 8,
9)
8 8 9 [19], [87] [19, 87] [14, 23, 55, 66, 78, 31,
45, 92, 19, 87]
8 merge(5, 7,
9)
5 7 9 [31, 45, 92], [19, 87] [19, 31, 45, 87, 92] [14, 23, 55, 66, 78, 19,
31, 45, 87, 92]
9 merge(0, 4,
9)
0 4 9 [14, 23, 55, 66, 78], [19, 31,
45, 87, 92]
[14, 19, 23, 31, 45, 55,
66, 78, 87, 92]
[14, 19, 23, 31, 45, 55,
66, 78, 87, 92]
Dry run illustrated in diagram:
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
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]
 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].
Table of All quickSortHelper Calls
This table summarizes each quickSortHelper call, similar to the Merge Sort summary format,
focusing on the subarray being sorted, the pivot, the array state after partitioning, and the
resulting recursive calls.
Final Sorted Array: [14, 19, 23, 31, 45, 55, 66, 78, 87, 92]
quickSortHelper(left,
right)
Subarray Pivot
(Value,
Index)
Array After
Partition
Next Calls Backtrack To
1 QS(0, 9) [55, 23, 78, 14,
66, 31, 92, 45,
19, 87]
55 (index
0)
[19, 23, 14, 31,
45, 55, 92, 66,
78, 87]
QS(0, 4),
QS(6, 9)
- (Root,
completes after
all calls)
2 QS(0, 4) [19, 23, 14, 31,
45]
19 (index
0)
[14, 19, 23, 31,
45, 55, 92, 66,
78, 87]
QS(0, 0),
QS(2, 4)
QS(0, 9) (after
both calls)
3 QS(0, 0) [14] - [14, 19, 23, 31,
45, 55, 92, 66,
78, 87]
- (Base
Case)
QS(0, 4) (to
process QS(2,
4))
4 QS(2, 4) [23, 31, 45] 23 (index
2)
[14, 19, 23, 31,
45, 55, 92, 66,
78, 87]
QS(2, 1),
QS(3, 4)
QS(0, 4) (after
both calls)
5 QS(2, 1) - - [14, 19, 23, 31,
45, 55, 92, 66,
78, 87]
- (Empty) QS(2, 4) (to
process QS(3,
4))
6 QS(3, 4) [31, 45] 31 (index
3)
[14, 19, 23, 31,
45, 55, 92, 66,
78, 87]
QS(3, 2),
QS(4, 4)
QS(2, 4) (after
both calls)
7 QS(3, 2) - - [14, 19, 23, 31,
45, 55, 92, 66,
78, 87]
- (Empty) QS(3, 4) (to
process QS(4,
4))
8 QS(4, 4) [45] - [14, 19, 23, 31,
45, 55, 92, 66,
78, 87]
- (Base
Case)
QS(3, 4) (then
up to QS(2, 4))
9 QS(6, 9) [92, 66, 78, 87] 92 (index
6)
[14, 19, 23, 31,
45, 55, 87, 66,
78, 92]
QS(6, 8),
QS(10, 9)
QS(0, 9) (after
both calls)
1
0
QS(6, 8) [87, 66, 78] 87 (index
6)
[14, 19, 23, 31,
45, 55, 78, 66,
87, 92]
QS(6, 7),
QS(9, 8)
QS(6, 9) (after
both calls)
1
1
QS(6, 7) [78, 66] 78 (index
6)
[14, 19, 23, 31,
45, 55, 66, 78,
87, 92]
QS(6, 6),
QS(8, 7)
QS(6, 8) (after
both calls)
1
2
QS(6, 6) [66] - [14, 19, 23, 31,
45, 55, 66, 78,
- (Base
Case)
QS(6, 7) (to
process QS(8,
87, 92] 7))
1
3
QS(8, 7) - - [14, 19, 23, 31,
45, 55, 66, 78,
87, 92]
- (Empty) QS(6, 7) (then
up to QS(6, 8))
1
4
QS(10, 9) - - [14, 19, 23, 31,
45, 55, 66, 78,
87, 92]
- (Empty) QS(6, 9) (then
up to QS(0, 9))
Dry run illustrated in diagram:

More Related Content

PPT
Data Structure Sorting
PPT
Sorting algos > Data Structures & Algorithums
PDF
Please I want a detailed complete answers for each part.Then make.pdf
PPTX
MERGE and Quick Sort algorithm explain ppt
PPTX
Sorting techniques
PPTX
Sortings .pptx
PDF
Sorting Algorithms and their implementations
PDF
Ds sorting
Data Structure Sorting
Sorting algos > Data Structures & Algorithums
Please I want a detailed complete answers for each part.Then make.pdf
MERGE and Quick Sort algorithm explain ppt
Sorting techniques
Sortings .pptx
Sorting Algorithms and their implementations
Ds sorting

Similar to Assignment 2 data structures National University Of Modern Languages (20)

PDF
Merge sort
PPT
quicksort (1).ppt
PPT
quick_sort.ppt
PPT
quick_sort_with_explanationandImplmentation.ppt
PPT
Quicksort and MergeSort Algorithm Analysis
PPT
quicksort.ppthhhhhhhhhhhhhhhhhhhhhhhhhhh
PPT
quicksort.ppt
PPT
quicksort (1).ppt
PPT
Quick & Merge Sort.ppt
PPT
quicksort.ppt
PPT
quicksort.ppt
PPTX
Sorting in data structures and algorithms , it has all the necessary points t...
PPTX
UNIT V Searching Sorting Hashing Techniques [Autosaved].pptx
PPTX
UNIT V Searching Sorting Hashing Techniques [Autosaved].pptx
PPT
3.8 quicksort 04
PPT
Unit 7 sorting
PPTX
09 QUICK SORT Design and Analysis of algorithms
PDF
Working of Merge Sort Code
PPTX
Weak 11-12 Sorting update.pptxbhjiiuuuuu
Merge sort
quicksort (1).ppt
quick_sort.ppt
quick_sort_with_explanationandImplmentation.ppt
Quicksort and MergeSort Algorithm Analysis
quicksort.ppthhhhhhhhhhhhhhhhhhhhhhhhhhh
quicksort.ppt
quicksort (1).ppt
Quick & Merge Sort.ppt
quicksort.ppt
quicksort.ppt
Sorting in data structures and algorithms , it has all the necessary points t...
UNIT V Searching Sorting Hashing Techniques [Autosaved].pptx
UNIT V Searching Sorting Hashing Techniques [Autosaved].pptx
3.8 quicksort 04
Unit 7 sorting
09 QUICK SORT Design and Analysis of algorithms
Working of Merge Sort Code
Weak 11-12 Sorting update.pptxbhjiiuuuuu
Ad

Recently uploaded (20)

PDF
Machine learning based COVID-19 study performance prediction
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPTX
Big Data Technologies - Introduction.pptx
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPT
Teaching material agriculture food technology
PDF
Empathic Computing: Creating Shared Understanding
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
Programs and apps: productivity, graphics, security and other tools
PPTX
Spectroscopy.pptx food analysis technology
Machine learning based COVID-19 study performance prediction
Per capita expenditure prediction using model stacking based on satellite ima...
Reach Out and Touch Someone: Haptics and Empathic Computing
Chapter 3 Spatial Domain Image Processing.pdf
Big Data Technologies - Introduction.pptx
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Teaching material agriculture food technology
Empathic Computing: Creating Shared Understanding
Agricultural_Statistics_at_a_Glance_2022_0.pdf
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Advanced methodologies resolving dimensionality complications for autism neur...
MYSQL Presentation for SQL database connectivity
Spectral efficient network and resource selection model in 5G networks
Review of recent advances in non-invasive hemoglobin estimation
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
“AI and Expert System Decision Support & Business Intelligence Systems”
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Programs and apps: productivity, graphics, security and other tools
Spectroscopy.pptx food analysis technology
Ad

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)
  • 5.  Parameters: left=3, mid=3, right=4, nL = mid - left + 1 = 1, nR = right - mid = 1.  Subarrays: L = [14] (from arr[3]), R = [66] (from arr[4]).  Initial Array State: [23, 55, 78, 14, 66, 31, 92, 45, 19, 87] (after previous merge). Ste p Merge Call Loop Executed i j k n L n R Condition Action Array State 8 merge(3, 3, 4) for (copy L) 0 - - 1 1 i < nL (0 < 1) Copy L[0] = arr[3] = 14 [23, 55, 78, 14, 66, 31, 92, 45, 19, 87] 9 merge(3, 3, 4) for (copy R) - 0 - 1 1 j < nR (0 < 1) Copy R[0] = arr[4] = 66 [23, 55, 78, 14, 66, 31, 92, 45, 19, 87] 10 merge(3, 3, 4) while (Loop 1) 0 0 3 1 1 i < nL && j < nR (true) L[0] <= R[0] (14 ≤ 66), arr[3] = 14, i++ [23, 55, 78, 14, 66, 31, 92, 45, 19, 87] 11 merge(3, 3, 4) while (Loop 1) 1 0 4 1 1 i < nL && j < nR (false) Loop 1 ends (i == nL) [23, 55, 78, 14, 66, 31, 92, 45, 19, 87] 12 merge(3, 3, 4) while (Loop 2) 1 0 4 1 1 i < nL (1 < 1, false) Loop 2 does not execute [23, 55, 78, 14, 66, 31, 92, 45, 19, 87] 13 merge(3, 3, 4) while (Loop 3) 1 0 4 1 1 j < nR (0 < 1) arr[4] = R[0] = 66, j+ + [23, 55, 78, 14, 66, 31, 92, 45, 19, 87] 14 merge(3, 3, 4) while (Loop 3) 1 1 5 1 1 j < nR (1 < 1, false) Loop 3 ends [23, 55, 78, 14, 66, 31, 92, 45, 19, 87] Result of merge(3, 3, 4): arr[3..4] is now [14, 66]. Remaining Merge Iterations Merge Call lef t mi d righ t Input Subarrays Sorted Result Array State After Merge 3 merge(0, 1, 2) 0 1 2 [23, 55], [78] [23, 55, 78] [23, 55, 78, 14, 66, 31, 92, 45, 19, 87] 4 merge(0, 2, 4) 0 2 4 [23, 55, 78], [14, 66] [14, 23, 55, 66, 78] [14, 23, 55, 66, 78, 31, 92, 45, 19, 87] 5 merge(5, 5, 6) 5 5 6 [31], [92] [31, 92] [14, 23, 55, 66, 78, 31, 92, 45, 19, 87] 6 merge(5, 6, 5 6 7 [31, 92], [45] [31, 45, 92] [14, 23, 55, 66, 78, 31,
  • 6. 7) 45, 92, 19, 87] 7 merge(8, 8, 9) 8 8 9 [19], [87] [19, 87] [14, 23, 55, 66, 78, 31, 45, 92, 19, 87] 8 merge(5, 7, 9) 5 7 9 [31, 45, 92], [19, 87] [19, 31, 45, 87, 92] [14, 23, 55, 66, 78, 19, 31, 45, 87, 92] 9 merge(0, 4, 9) 0 4 9 [14, 23, 55, 66, 78], [19, 31, 45, 87, 92] [14, 19, 23, 31, 45, 55, 66, 78, 87, 92] [14, 19, 23, 31, 45, 55, 66, 78, 87, 92] Dry run illustrated in diagram:
  • 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].
  • 10. Table of All quickSortHelper Calls This table summarizes each quickSortHelper call, similar to the Merge Sort summary format, focusing on the subarray being sorted, the pivot, the array state after partitioning, and the resulting recursive calls. Final Sorted Array: [14, 19, 23, 31, 45, 55, 66, 78, 87, 92] quickSortHelper(left, right) Subarray Pivot (Value, Index) Array After Partition Next Calls Backtrack To 1 QS(0, 9) [55, 23, 78, 14, 66, 31, 92, 45, 19, 87] 55 (index 0) [19, 23, 14, 31, 45, 55, 92, 66, 78, 87] QS(0, 4), QS(6, 9) - (Root, completes after all calls) 2 QS(0, 4) [19, 23, 14, 31, 45] 19 (index 0) [14, 19, 23, 31, 45, 55, 92, 66, 78, 87] QS(0, 0), QS(2, 4) QS(0, 9) (after both calls) 3 QS(0, 0) [14] - [14, 19, 23, 31, 45, 55, 92, 66, 78, 87] - (Base Case) QS(0, 4) (to process QS(2, 4)) 4 QS(2, 4) [23, 31, 45] 23 (index 2) [14, 19, 23, 31, 45, 55, 92, 66, 78, 87] QS(2, 1), QS(3, 4) QS(0, 4) (after both calls) 5 QS(2, 1) - - [14, 19, 23, 31, 45, 55, 92, 66, 78, 87] - (Empty) QS(2, 4) (to process QS(3, 4)) 6 QS(3, 4) [31, 45] 31 (index 3) [14, 19, 23, 31, 45, 55, 92, 66, 78, 87] QS(3, 2), QS(4, 4) QS(2, 4) (after both calls) 7 QS(3, 2) - - [14, 19, 23, 31, 45, 55, 92, 66, 78, 87] - (Empty) QS(3, 4) (to process QS(4, 4)) 8 QS(4, 4) [45] - [14, 19, 23, 31, 45, 55, 92, 66, 78, 87] - (Base Case) QS(3, 4) (then up to QS(2, 4)) 9 QS(6, 9) [92, 66, 78, 87] 92 (index 6) [14, 19, 23, 31, 45, 55, 87, 66, 78, 92] QS(6, 8), QS(10, 9) QS(0, 9) (after both calls) 1 0 QS(6, 8) [87, 66, 78] 87 (index 6) [14, 19, 23, 31, 45, 55, 78, 66, 87, 92] QS(6, 7), QS(9, 8) QS(6, 9) (after both calls) 1 1 QS(6, 7) [78, 66] 78 (index 6) [14, 19, 23, 31, 45, 55, 66, 78, 87, 92] QS(6, 6), QS(8, 7) QS(6, 8) (after both calls) 1 2 QS(6, 6) [66] - [14, 19, 23, 31, 45, 55, 66, 78, - (Base Case) QS(6, 7) (to process QS(8,
  • 11. 87, 92] 7)) 1 3 QS(8, 7) - - [14, 19, 23, 31, 45, 55, 66, 78, 87, 92] - (Empty) QS(6, 7) (then up to QS(6, 8)) 1 4 QS(10, 9) - - [14, 19, 23, 31, 45, 55, 66, 78, 87, 92] - (Empty) QS(6, 9) (then up to QS(0, 9)) Dry run illustrated in diagram: