Title: Heap Sort in Python - Runnable Code Snippet
Open Graph Title: Heap Sort in Python - Runnable Code Snippet
X Title: Heap Sort in Python - Runnable Code Snippet
Description: Sort an array using the heap sort algorithm with O(n log n) time complexity. Runnable Python snippet with live execution.
Open Graph Description: Sort an array using the heap sort algorithm with O(n log n) time complexity. Runnable Python snippet with live execution.
X Description: Sort an array using the heap sort algorithm with O(n log n) time complexity. Runnable Python snippet with live execution.
Keywords:
Opengraph URL: https://tempercode.dev/snippets/heap-sort-python
Domain: tempercode.dev
{"@context":"https://schema.org","@type":"SoftwareSourceCode","name":"Heap Sort","description":"Sort an array using the heap sort algorithm with O(n log n) time complexity.","programmingLanguage":"Python","text":"def heapify(a, n, i):\n largest, l, r = i, 2 * i + 1, 2 * i + 2\n if l < n and a[l] > a[largest]: largest = l\n if r < n and a[r] > a[largest]: largest = r\n if largest != i:\n a[i], a[largest] = a[largest], a[i]\n heapify(a, n, largest)\n\na = arr.copy()\nn = len(a)\nfor i in range(n // 2 - 1, -1, -1): heapify(a, n, i)\nfor i in range(n - 1, 0, -1):\n a[0], a[i] = a[i], a[0]\n heapify(a, i, 0)\nreturn a"}
| theme-color | #0c0d12 |
| color-scheme | dark |
| author | Temper |
| creator | Temper |
| publisher | Temper |
| og:image | https://tempercode.dev/og-image.png |
| og:type | article |
| twitter:card | summary_large_image |
| twitter:image | https://tempercode.dev/og-image.png |
Links:
Viewport: width=device-width, initial-scale=1