Sekcja DSAŁatwe

Sortowanie sterty

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

Oświadczenie o problemie

Łatwe

Napisz funkcję heap_sort(arr), która sortuje listę liczb całkowitych arr w kolejności rosnącej przy użyciu algorytmu sortowania sterty (najpierw budując maksymalną stertę) i zwraca posortowaną listę.

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

Przykłady

Example 1
Input
arr = [12, 11, 13, 5, 6, 7]
Output
[5, 6, 7, 11, 12, 13]
Explanation

A max heap is created, the maximum element is repeatedly swapped with the last element of the heap, and heapify is performed.

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.