堆排序,就是利用大顶堆或者小顶堆来设计的排序算法,是一种选择排序。堆是一种完全二叉树: - 大顶堆:每个节点的数值都大于或者等于其左右孩子节点的数值。
- 小顶堆:每个节点的数值都小于或者等于其左右孩子节点的数值。
堆排序,分别用汇编语言,C语言,C++,java,python编写出来
以下是使用汇编语言、C语言、C++、Java和Python编写的堆排序算法示例代码:
1.汇编语言(x86)代码示例:
- assembly
- section .data
- arr db 5, 3, 1, 2, 4
- n equ $-arr
- section .text
- global _start
- _start:
- mov esi, n / 2 - 1 ; 构建初始堆
- jmp heapify_loop
- heapify_loop:
- call heapify
- dec esi
- cmp esi, 0
- jge heapify_loop
- mov esi, n - 1 ; 执行堆排序
- jmp sort_loop
- sort_loop:
- mov al, [arr]
- xchg al, [arr + esi]
- xchg al, [arr]
- dec esi
- cmp esi, 0
- jge sort_loop
- jmp exit
- heapify:
- push esi
- push ecx
- push eax
- mov ecx, esi
- shl ecx, 1
- inc ecx
- cmp ecx, n
- jge skip_left_child
- mov al, [arr + ecx]
- cmp al, [arr + esi]
- jle skip_left_child
- mov esi, ecx
- skip_left_child:
- mov ecx, esi
- inc ecx
- cmp ecx, n
- jge skip_right_child
- mov al, [arr + ecx]
- cmp al, [arr + esi]
- jle skip_right_child
- mov esi, ecx
- skip_right_child:
- cmp esi, ecx
- je skip_swap
- mov al, [arr + esi]
- xchg al, [arr + ecx]
- xchg al, [arr + esi]
- call heapify
- skip_swap:
-
复制代码游客,本帖隐藏的内容需要积分高于 9 才可浏览,您当前积分为 0
2.C语言代码示例:
- #include <stdio.h>
- void heapify(int arr[], int n, int i) {
- int largest = i;
- int left = 2 * i + 1;
- int right = 2 * i + 2;
- if (left < n && arr[left] > arr[largest]) {
- largest = left;
- }
- if (right < n && arr[right] > arr[largest]) {
- largest = right;
- }
- if (largest != i) {
- int temp = arr[i];
- arr[i] = arr[largest];
- arr[largest] = temp;
- heapify(arr, n, largest);
- }
- }
- void heapSort(int arr[], int n) {
- for (int i = n / 2 - 1; i >= 0; i--) {
- heapify(arr, n, i);
- }
- for (int i = n - 1; i >= 0; i--) {
- int temp = arr[0];
- arr[0] = arr[i];
- arr[i] = temp;
- heapify(arr, i, 0);
- }
- }
复制代码游客,本帖隐藏的内容需要积分高于 9 才可浏览,您当前积分为 0
3.C++代码示例:
- #include <iostream>
- using namespace std;
- void heapify(int arr[], int n, int i) {
- int largest = i;
- int left = 2 * i + 1;
- int right = 2 * i + 2;
- if (left < n && arr[left] > arr[largest]) {
- largest = left;
- }
- if (right < n && arr[right] > arr[largest]) {
- largest = right;
- }
- if (largest != i) {
- swap(arr[i], arr[largest]);
- heapify(arr, n, largest);
- }
- }
- void heapSort(int arr[], int n) {
- for (int i = n / 2 - 1; i >= 0; i--) {
- heapify(arr, n, i);
- }
- for (int i = n - 1; i >= 0; i--) {
- swap(arr[0], arr[i]);
- heapify(arr, i, 0);
- }
- }
复制代码游客,本帖隐藏的内容需要积分高于 9 才可浏览,您当前积分为 0
4.Java代码示例:
- public class HeapSort {
- public static void heapify(int arr[], int n, int i) {
- int largest = i;
- int left = 2 * i + 1;
- int right = 2 * i + 2;
- if (left < n && arr[left] > arr[largest]) {
- largest = left;
- }
- if (right < n && arr[right] > arr[largest]) {
- largest = right;
- }
- if (largest != i) {
- int temp = arr[i];
- arr[i] = arr[largest];
- arr[largest] = temp;
- heapify(arr, n, largest);
- }
- }
- public static void heapSort(int arr[]) {
- int n = arr.length;
- for (int i = n / 2 - 1; i >= 0; i--) {
- heapify(arr, n, i);
- }
- for (int i = n - 1; i >= 0; i--) {
- int temp = arr[0];
- arr[0] = arr[i];
- arr[i] = temp;
- heapify(arr, i, 0);
- }
- }
-
- }
复制代码游客,本帖隐藏的内容需要积分高于 9 才可浏览,您当前积分为 0
5.Python代码示例:
- def heapify(arr, n, i):
- largest = i
- left = 2 * i + 1
- right = 2 * i + 2
- if left < n and arr[left] > arr[largest]:
- largest = left
- if right < n and arr[right] > arr[largest]:
- largest = right
- if largest != i:
- arr[i], arr[largest] = arr[largest], arr[i]
- heapify(arr, n, largest)
- def heapSort(arr):
- n = len(arr)
- for i in range(n // 2 - 1, -1, -1):
- heapify(arr, n, i)
- for i in range(n - 1, 0, -1):
- arr[0], arr[i] = arr[i], arr[0]
- heapify(arr, i, 0)
复制代码游客,本帖隐藏的内容需要积分高于 9 才可浏览,您当前积分为 0
这些示例代码演示了如何使用汇编语言、C语言、C++、Java和Python编写堆排序算法。请根据您的需求和偏好选择适合您的语言。
扫码关注微信公众号,免费查看完整算法内容。
|