Sekcja DSAŁatwe

Sortowanie przez scalanie

Szczegółowy przewodnik i implementacja Python dla problemu „Sortowanie przez scalanie”.

Oświadczenie o problemie

Łatwe

Napisz funkcję merge_sort(arr), która sortuje listę liczb całkowitych arr w kolejności rosnącej przy użyciu algorytmu sortowania przez scalanie i zwraca posortowaną listę.

Ograniczenia
  • 0 <= len(arr) <= 10^4
  • -10^5 <= arr[i] <= 10^5

Przykłady

Example 1
Input
arr = [38, 27, 43, 3, 9, 82, 10]
Output
[3, 9, 10, 27, 38, 43, 82]
Explanation

The list is divided in half recursively, sorted, and then merged back together.

Need a Hint?
Rozważ użycie struktur danych specyficznych dla sortowania, takich jak zestawy lub sterty.
Edge Cases to Watch
  • Puste struktury wejściowe
  • Wejścia jednoelementowe
  • Duże granice liczbowe

Gotowy do rozwiązania?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Otwórz w Edytorze
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

Polecane zasoby Pythona

Poszerzaj swoją wiedzę dzięki powiązanym interaktywnym samouczkom, ściągawkom i porównaniom kodów.