func mergeSort(nums []int) []int {
}func mergeSort(nums []int) []int {
// Base case: a slice of length 0 or 1 is already sorted — nothing to do.
}func mergeSort(nums []int) []int {
// Base case: a slice of length 0 or 1 is already sorted — nothing to do.
if len(nums) <= 1 {
return nums
}
}func mergeSort(nums []int) []int {
// Base case: a slice of length 0 or 1 is already sorted — nothing to do.
if len(nums) <= 1 {
return nums
}
// Divide: split down the middle and sort each half recursively.
}func mergeSort(nums []int) []int {
// Base case: a slice of length 0 or 1 is already sorted — nothing to do.
if len(nums) <= 1 {
return nums
}
// Divide: split down the middle and sort each half recursively.
mid := len(nums) / 2
left := mergeSort(nums[:mid])
right := mergeSort(nums[mid:])
}func mergeSort(nums []int) []int {
// Base case: a slice of length 0 or 1 is already sorted — nothing to do.
if len(nums) <= 1 {
return nums
}
// Divide: split down the middle and sort each half recursively.
mid := len(nums) / 2
left := mergeSort(nums[:mid])
right := mergeSort(nums[mid:])
// Conquer: both halves are sorted, so merge them into one sorted slice.
}func mergeSort(nums []int) []int {
// Base case: a slice of length 0 or 1 is already sorted — nothing to do.
if len(nums) <= 1 {
return nums
}
// Divide: split down the middle and sort each half recursively.
mid := len(nums) / 2
left := mergeSort(nums[:mid])
right := mergeSort(nums[mid:])
// Conquer: both halves are sorted, so merge them into one sorted slice.
return merge(left, right)
}func mergeSort(nums []int) []int {
// Base case: a slice of length 0 or 1 is already sorted — nothing to do.
if len(nums) <= 1 {
return nums
}
// Divide: split down the middle and sort each half recursively.
mid := len(nums) / 2
left := mergeSort(nums[:mid])
right := mergeSort(nums[mid:])
// Conquer: both halves are sorted, so merge them into one sorted slice.
return merge(left, right)
}
// merge combines two already-sorted slices into one sorted slice.
// Preallocate the exact capacity, and give each input its own read index.func mergeSort(nums []int) []int {
// Base case: a slice of length 0 or 1 is already sorted — nothing to do.
if len(nums) <= 1 {
return nums
}
// Divide: split down the middle and sort each half recursively.
mid := len(nums) / 2
left := mergeSort(nums[:mid])
right := mergeSort(nums[mid:])
// Conquer: both halves are sorted, so merge them into one sorted slice.
return merge(left, right)
}
// merge combines two already-sorted slices into one sorted slice.
func merge(a, b []int) []int {
// Preallocate the exact capacity, and give each input its own read index.
res := make([]int, 0, len(a)+len(b))
i, j := 0, 0
}func mergeSort(nums []int) []int {
// Base case: a slice of length 0 or 1 is already sorted — nothing to do.
if len(nums) <= 1 {
return nums
}
// Divide: split down the middle and sort each half recursively.
mid := len(nums) / 2
left := mergeSort(nums[:mid])
right := mergeSort(nums[mid:])
// Conquer: both halves are sorted, so merge them into one sorted slice.
return merge(left, right)
}
// merge combines two already-sorted slices into one sorted slice.
func merge(a, b []int) []int {
// Preallocate the exact capacity, and give each input its own read index.
res := make([]int, 0, len(a)+len(b))
i, j := 0, 0
// While both still have elements, compare their fronts and take the smaller.
}func mergeSort(nums []int) []int {
// Base case: a slice of length 0 or 1 is already sorted — nothing to do.
if len(nums) <= 1 {
return nums
}
// Divide: split down the middle and sort each half recursively.
mid := len(nums) / 2
left := mergeSort(nums[:mid])
right := mergeSort(nums[mid:])
// Conquer: both halves are sorted, so merge them into one sorted slice.
return merge(left, right)
}
// merge combines two already-sorted slices into one sorted slice.
func merge(a, b []int) []int {
// Preallocate the exact capacity, and give each input its own read index.
res := make([]int, 0, len(a)+len(b))
i, j := 0, 0
// While both still have elements, compare their fronts and take the smaller.
for i < len(a) && j < len(b) {
}
}func mergeSort(nums []int) []int {
// Base case: a slice of length 0 or 1 is already sorted — nothing to do.
if len(nums) <= 1 {
return nums
}
// Divide: split down the middle and sort each half recursively.
mid := len(nums) / 2
left := mergeSort(nums[:mid])
right := mergeSort(nums[mid:])
// Conquer: both halves are sorted, so merge them into one sorted slice.
return merge(left, right)
}
// merge combines two already-sorted slices into one sorted slice.
func merge(a, b []int) []int {
// Preallocate the exact capacity, and give each input its own read index.
res := make([]int, 0, len(a)+len(b))
i, j := 0, 0
// While both still have elements, compare their fronts and take the smaller.
for i < len(a) && j < len(b) {
// Using <= (not <) keeps equal elements in their original order — stable.
}
}func mergeSort(nums []int) []int {
// Base case: a slice of length 0 or 1 is already sorted — nothing to do.
if len(nums) <= 1 {
return nums
}
// Divide: split down the middle and sort each half recursively.
mid := len(nums) / 2
left := mergeSort(nums[:mid])
right := mergeSort(nums[mid:])
// Conquer: both halves are sorted, so merge them into one sorted slice.
return merge(left, right)
}
// merge combines two already-sorted slices into one sorted slice.
func merge(a, b []int) []int {
// Preallocate the exact capacity, and give each input its own read index.
res := make([]int, 0, len(a)+len(b))
i, j := 0, 0
// While both still have elements, compare their fronts and take the smaller.
for i < len(a) && j < len(b) {
// Using <= (not <) keeps equal elements in their original order — stable.
if a[i] <= b[j] {
res = append(res, a[i])
i++
} else {
res = append(res, b[j])
j++
}
}
}func mergeSort(nums []int) []int {
// Base case: a slice of length 0 or 1 is already sorted — nothing to do.
if len(nums) <= 1 {
return nums
}
// Divide: split down the middle and sort each half recursively.
mid := len(nums) / 2
left := mergeSort(nums[:mid])
right := mergeSort(nums[mid:])
// Conquer: both halves are sorted, so merge them into one sorted slice.
return merge(left, right)
}
// merge combines two already-sorted slices into one sorted slice.
func merge(a, b []int) []int {
// Preallocate the exact capacity, and give each input its own read index.
res := make([]int, 0, len(a)+len(b))
i, j := 0, 0
// While both still have elements, compare their fronts and take the smaller.
for i < len(a) && j < len(b) {
// Using <= (not <) keeps equal elements in their original order — stable.
if a[i] <= b[j] {
res = append(res, a[i])
i++
} else {
res = append(res, b[j])
j++
}
}
// Exactly one side has leftovers (already sorted); append them as-is.
}func mergeSort(nums []int) []int {
// Base case: a slice of length 0 or 1 is already sorted — nothing to do.
if len(nums) <= 1 {
return nums
}
// Divide: split down the middle and sort each half recursively.
mid := len(nums) / 2
left := mergeSort(nums[:mid])
right := mergeSort(nums[mid:])
// Conquer: both halves are sorted, so merge them into one sorted slice.
return merge(left, right)
}
// merge combines two already-sorted slices into one sorted slice.
func merge(a, b []int) []int {
// Preallocate the exact capacity, and give each input its own read index.
res := make([]int, 0, len(a)+len(b))
i, j := 0, 0
// While both still have elements, compare their fronts and take the smaller.
for i < len(a) && j < len(b) {
// Using <= (not <) keeps equal elements in their original order — stable.
if a[i] <= b[j] {
res = append(res, a[i])
i++
} else {
res = append(res, b[j])
j++
}
}
// Exactly one side has leftovers (already sorted); append them as-is.
res = append(res, a[i:]...)
res = append(res, b[j:]...)
return res
}Tap the left half to go back, the right half to go forward.
Arrow keys or space to navigate · Esc to exit.