📖 Scenario: You are working on a program that needs to sort a list of numbers efficiently. Merge Sort is a popular sorting method that splits the list into smaller parts, sorts them, and then merges them back together in order.Imagine you have a messy pile of numbered cards, and you want to arrange them from smallest to largest quickly and neatly.
🎯 Goal: Build a Go program that implements the Merge Sort algorithm to sort a list of integers.You will create the initial list, write the merge function, write the merge sort function, and finally print the sorted list.
📋 What You'll Learn
Create a slice of integers called numbers with the exact values: 38, 27, 43, 3, 9, 82, 10
Create a helper function called merge that merges two sorted slices into one sorted slice
Create a recursive function called mergeSort that splits the slice and uses merge to sort
Print the sorted slice after applying mergeSort
💡 Why This Matters
🌍 Real World
Merge Sort is used in many software systems where efficient and stable sorting is needed, such as databases, search engines, and large data processing.
💼 Career
Understanding merge sort helps in technical interviews and improves problem-solving skills for software development roles.
Progress0 / 4 steps
1
Create the initial slice of numbers
Create a slice of integers called numbers with these exact values: 38, 27, 43, 3, 9, 82, 10
DSA Go
Hint
Use numbers := []int{38, 27, 43, 3, 9, 82, 10} to create the slice.
2
Create the merge function
Create a function called merge that takes two sorted slices of integers named left and right and returns a single sorted slice of integers
DSA Go
Hint
Use two indexes to compare elements from left and right, append the smaller to result, then append remaining elements.
3
Create the mergeSort function
Create a recursive function called mergeSort that takes a slice of integers named arr and returns a sorted slice of integers. It should split arr into halves, recursively sort each half, and merge them using the merge function
DSA Go
Hint
Split the slice into two halves, recursively call mergeSort on each half, then merge the results.
4
Print the sorted slice
In the main function, call mergeSort with the numbers slice and print the returned sorted slice using fmt.Println
DSA Go
Hint
Call mergeSort(numbers) and print the result with fmt.Println.
Practice
(1/5)
1. What is the main idea behind the Merge Sort algorithm?
easy
A. Compare each element with every other element and swap if needed
B. Pick the smallest element and place it at the beginning repeatedly
C. Divide the list into halves, sort each half, then merge them
D. Use a pivot to partition the list and sort partitions recursively
Solution
Step 1: Understand the divide and conquer approach
Merge Sort splits the list into two halves recursively until single elements remain.
Step 2: Merge sorted halves
It then merges these halves back together in sorted order.
Final Answer:
Divide the list into halves, sort each half, then merge them -> Option C
Quick Check:
Merge Sort = Divide and Conquer [OK]
Hint: Remember: split, sort halves, then merge [OK]
Common Mistakes:
Confusing Merge Sort with Bubble Sort
Thinking Merge Sort uses a pivot like Quick Sort
Believing Merge Sort sorts in place without extra space
2. Which of the following is the correct way to declare a function for merge sort in Go?
easy
A. func mergeSort(arr []int) []int {}
B. func mergeSort(arr int[]) int[] {}
C. func mergeSort(arr array of int) array of int {}
D. func mergeSort(arr []int) int {}
Solution
Step 1: Check Go function syntax for slices
In Go, slices are declared as []int for integer slices.
Step 2: Verify return type matches input type
Merge sort returns a sorted slice, so return type is also []int.
Final Answer:
func mergeSort(arr []int) []int {} -> Option A
Quick Check:
Go slice syntax = []int [OK]
Hint: Go slices use []type syntax, not array of type [OK]
Common Mistakes:
Using array syntax like int[] which is invalid in Go
Returning int instead of []int for sorted slice
Using incorrect parameter types like 'array of int'
3. What will be the output of the following Go code snippet using merge sort?
package main
import "fmt"
func mergeSort(arr []int) []int {
if len(arr) <= 1 {
return arr
}
mid := len(arr) / 2
left := mergeSort(arr[:mid])
right := mergeSort(arr[mid:])
return merge(left, right)
}
func merge(left, right []int) []int {
result := []int{}
i, j := 0, 0
for i < len(left) && j < len(right) {
if left[i] < right[j] {
result = append(result, left[i])
i++
} else {
result = append(result, right[j])
j++
}
}
result = append(result, left[i:]...)
result = append(result, right[j:]...)
return result
}
func main() {
arr := []int{5, 3, 8, 4, 2}
sorted := mergeSort(arr)
fmt.Println(sorted)
}
medium
A. [5 3 8 4 2]
B. [2 3 4 5 8]
C. [8 5 4 3 2]
D. [2 3 5 4 8]
Solution
Step 1: Trace the recursive splitting
The array splits into halves until single elements: [5,3,8,4,2] -> [5,3] and [8,4,2], then further splits.
Step 2: Merge sorted halves step-by-step
Merge [5] and [3] -> [3,5]; merge [8] and [4,2] -> [2,4,8]; finally merge [3,5] and [2,4,8] -> [2,3,4,5,8].
Final Answer:
[2 3 4 5 8] -> Option B
Quick Check:
Sorted array = [2 3 4 5 8] [OK]
Hint: Merge halves sorted individually to get full sorted array [OK]
Common Mistakes:
Assuming original array prints without sorting
Mixing up merge order causing wrong output
Forgetting to append remaining elements after loop
4. Identify the bug in this merge function snippet and select the fix:
func merge(left, right []int) []int {
result := []int{}
i, j := 0, 0
for i < len(left) && j < len(right) {
if left[i] <= right[j] {
result = append(result, left[i])
i++
} else {
result = append(result, right[j])
j++
}
}
result = append(result, left[i:]...)
result = append(result, right[i:]...)
return result
}
medium
A. Remove the last two append statements
B. Change comparison from <= to <
C. Initialize result with length of left + right
D. Change last append from right[i:] to right[j:]
Solution
Step 1: Analyze the append of remaining elements
After merging common parts, remaining elements from left[i:] and right[j:] must be appended.
Step 2: Spot the incorrect slice used
The code incorrectly appends right[i:] instead of right[j:], which can cause wrong elements or panic.
Final Answer:
Change last append from right[i:] to right[j:] -> Option D
Quick Check:
Append remaining right elements using j index [OK]
Hint: Use correct index variable when appending leftover elements [OK]
Common Mistakes:
Using wrong index variable for right slice leftover