📖 Scenario: You have a list of numbers that you want to sort in ascending order. Sorting helps you organize data so you can find things faster, like sorting books by title on a shelf.
🎯 Goal: Build a program that uses the merge sort algorithm to sort a list of integers step-by-step. You will first create the list, then set up helper variables, write the merge function, and finally print the sorted list.
📋 What You'll Learn
Create an integer array with exact values
Create helper variables for array size
Write a merge function to combine two sorted halves
Write the mergeSort function to recursively sort the array
Print the sorted array after sorting
💡 Why This Matters
🌍 Real World
Sorting is used everywhere, like organizing contacts, searching data quickly, or preparing data for reports.
💼 Career
Understanding merge sort helps in software development roles where efficient data processing and algorithm knowledge are important.
Progress0 / 4 steps
1
Create the array to sort
Create an integer array called arr with these exact values: 38, 27, 43, 3, 9, 82, 10.
DSA C++
Hint
Use C++ array syntax with curly braces and commas.
2
Set up the array size variable
Create an integer variable called n and set it to the size of arr using sizeof(arr) / sizeof(arr[0]).
DSA C++
Hint
Use sizeof operator to find number of elements in the array.
3
Write the merge function
Write a function called merge that takes arr, left, mid, and right as parameters. This function should merge two sorted halves of the array from left to mid and from mid+1 to right into a sorted segment.
DSA C++
Hint
Use temporary arrays to hold left and right halves, then merge them back into arr.
4
Write mergeSort and print the sorted array
Write a recursive function called mergeSort that takes arr, left, and right as parameters. It should split the array and call merge to sort. Then call mergeSort(arr, 0, n - 1) and print the sorted array elements separated by spaces.
DSA C++
Hint
Use recursion to split the array and merge sorted halves. Then print all elements separated by spaces.
Practice
(1/5)
1. What is the main idea behind the Merge Sort Algorithm?
easy
A. Select the smallest element and place it at the beginning
B. Divide the array into halves, sort each half, then merge them
C. Sort the array by repeatedly swapping adjacent elements
D. Build a heap and extract elements in sorted order
Solution
Step 1: Understand the divide and conquer approach
Merge sort splits the array into two halves recursively until single elements remain.
Step 2: Merge sorted halves
It then merges these sorted halves to produce a fully sorted array.
Final Answer:
Divide the array into halves, sort each half, then merge them -> Option B
Quick Check:
Merge Sort = Divide, Sort, Merge [OK]
Hint: Remember: split, sort, then merge [OK]
Common Mistakes:
Confusing merge sort with bubble sort
Thinking merge sort swaps adjacent elements
Mixing merge sort with selection sort
2. Which of the following is the correct way to declare the merge function header in C++ for merge sort?
easy
A. int mergeSort(int arr[], int left, int right);
B. int merge(int arr[], int left, int mid, int right);
C. void mergeSort(int arr[], int left, int right);
D. void merge(int arr[], int left, int mid, int right);
Solution
Step 1: Identify the merge function role
The merge function combines two sorted parts; it does not return a value, so it should be void.
Step 2: Check parameter list
It takes the array and three indices: left, mid, and right to merge subarrays.
Final Answer:
void merge(int arr[], int left, int mid, int right); -> Option D
Quick Check:
Merge function = void with arr and indices [OK]
Hint: Merge combines parts, returns nothing [OK]
Common Mistakes:
Using int return type for merge
Confusing merge with mergeSort function
Wrong parameter order or missing parameters
3. What will be the output of the following code snippet after applying merge sort on the array?
int arr[] = {5, 2, 9, 1};
mergeSort(arr, 0, 3);
for(int i = 0; i < 4; i++) std::cout << arr[i] << " ";
medium
A. 5 2 9 1
B. 9 5 2 1
C. 1 2 5 9
D. 2 1 5 9
Solution
Step 1: Apply merge sort on the array
The array {5, 2, 9, 1} is split and sorted recursively to {1, 2, 5, 9}.
Step 2: Print the sorted array
The for loop prints elements in sorted order: 1 2 5 9.