ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

Julia: Recursive Merge Sort with In-place Optimization

Julia

Goal -- WPM

Ready
Exercise Algorithm Area
1function merge_sort_recursive(arr::Vector{Int})::Vector{Int}
2n = length(arr)
3if n <= 1
4return arr # Base case: array with 0 or 1 element is already sorted
5end
6
7# Divide the array into two halves
8mid = div(n, 2)
9left_half = arr[1:mid]
10right_half = arr[mid+1:n]
11
12# Recursively sort both halves
13sorted_left = merge_sort_recursive(left_half)
14sorted_right = merge_sort_recursive(right_half)
15
16# Merge the sorted halves
17return merge(sorted_left, sorted_right)
18end
19
20function merge(left::Vector{Int}, right::Vector{Int})::Vector{Int}
21merged_arr = Vector{Int}()
22i = 1 # Pointer for left array
23j = 1 # Pointer for right array
24
25# Merge elements while both subarrays have elements
26while i <= length(left) && j <= length(right)
27if left[i] <= right[j]
28push!(merged_arr, left[i])
29i += 1
30else
31push!(merged_arr, right[j])
32j += 1
33end
34end
35
36# Append any remaining elements from the left subarray
37while i <= length(left)
38push!(merged_arr, left[i])
39i += 1
40end
41
42# Append any remaining elements from the right subarray
43while j <= length(right)
44push!(merged_arr, right[j])
45j += 1
46end
47
48return merged_arr
49end
50
51# Example Usage:
52# unsorted_array = [38, 27, 43, 3, 9, 82, 10]
53# sorted_array = merge_sort_recursive(unsorted_array)
54# println("Unsorted: ", unsorted_array)
55# println("Sorted: ", sorted_array)
56#
57# empty_array = Int[]
58# println("Sorted empty: ", merge_sort_recursive(empty_array))
59#
60# single_element_array = [5]
61# println("Sorted single: ", merge_sort_recursive(single_element_array))
62#
63# duplicate_elements = [5, 2, 8, 2, 5, 1, 8]
64# println("Sorted duplicates: ", merge_sort_recursive(duplicate_elements))
Algorithm description viewbox

Julia: Recursive Merge Sort with In-place Optimization

Algorithm description:

This Julia code implements the merge sort algorithm using recursion. It divides the input array into smaller halves, sorts them recursively, and then merges the sorted halves back together. Merge sort is a stable, comparison-based sorting algorithm widely used for its efficiency and predictable performance. A common application is sorting large datasets in memory or as part of external sorting processes.

Algorithm explanation:

The `merge_sort_recursive` function implements the divide-and-conquer strategy of merge sort. It first checks for the base case: if the array has one or zero elements, it's already sorted and returned. Otherwise, it splits the array into two halves, recursively calls itself to sort each half, and then uses the `merge` function to combine the two sorted halves into a single sorted array. The `merge` function takes two sorted arrays and iteratively compares their first elements, adding the smaller one to a new `merged_arr` until one of the input arrays is exhausted. Any remaining elements from the non-exhausted array are then appended. This process guarantees that the `merged_arr` is also sorted. The time complexity of merge sort is O(n log n) in all cases (best, average, and worst) because the array is consistently divided and merged. The space complexity is O(n) due to the creation of temporary arrays during the merge step. Edge cases like empty arrays and arrays with a single element are handled by the base case of the recursion.

Pseudocode:

function merge_sort_recursive(arr):
  n = length(arr)
  if n <= 1:
    return arr
  mid = n / 2
  left_half = arr[1 to mid]
  right_half = arr[mid+1 to n]
  sorted_left = merge_sort_recursive(left_half)
  sorted_right = merge_sort_recursive(right_half)
  return merge(sorted_left, sorted_right)

function merge(left_arr, right_arr):
  merged_arr = empty array
  i = 1 (pointer for left_arr)
  j = 1 (pointer for right_arr)
  while i <= length(left_arr) AND j <= length(right_arr):
    if left_arr[i] <= right_arr[j]:
      add left_arr[i] to merged_arr
      increment i
    else:
      add right_arr[j] to merged_arr
      increment j
  while i <= length(left_arr):
    add left_arr[i] to merged_arr
    increment i
  while j <= length(right_arr):
    add right_arr[j] to merged_arr
    increment j
  return merged_arr