Merge sort with a custom comparator

Rust Medium functions

Task «Merge sort with a custom comparator»

Write a function `merge_sort` that takes an array of integers and a comparator closure that determines the sort order. The function should sort the array in ascending or descending order depending on the comparator and return a new sorted array. The task is to implement the merge sort algorithm with the ability to pass a custom comparison rule. Input data: first an integer n (1 ≤ n ≤ 1000) — the number of elements, then n integers, then the string 'asc' for ascending sort or 'desc' for descending sort. Output data: the sorted array separated by spaces.

Input Format

First line: an integer n (1 ≤ n ≤ 1000). Second line: n integers separated by spaces. Third line: 'asc' or 'desc'.

Output Format

A string of sorted numbers separated by spaces.

main.rs