计数排序,分别用汇编语言,C语言,C++,java,python编写出来
计数排序,不是基于比较,而是基于计数。计数排序步骤如下:[*]遍历数组,找出最大值和最小值。
[*]根据最大值和最小值,初始化对应的统计元素数量的数组。
[*]遍历元素,统计元素个数到新的数组。
[*]遍历统计的数组,按照顺序输出排序的数组元素。
计数排序,分别用汇编语言,C语言,C++,java,python编写出来
以下是使用汇编语言、C语言、C++、Java和Python编写的计数排序算法示例代码:
1.汇编语言(x86)代码示例:
assembly
section .data
arr db 5, 3, 1, 2, 4
n equ $-arr
section .bss
count resb 256
section .text
global _start
_start:
mov ecx, n
xor esi, esi
count_loop:
mov al,
inc byte
inc esi
loop count_loop
mov esi, 0
mov edi, 0
output_loop:
mov al, byte
test al, al
jz skip_output
mov ecx, eax
mov edx, esi
mov eax, 1
mov ebx, 1
int 0x80
skip_output:
**** Hidden Message *****
2.C语言代码示例:
#include <stdio.h>
void countingSort(int arr[], int n) {
int count = {0};
int output;
for (int i = 0; i < n; i++) {
count]++;
}
for (int i = 1; i < 256; i++) {
count += count;
}
for (int i = n - 1; i >= 0; i--) {
output] - 1] = arr;
count]--;
}
for (int i = 0; i < n; i++) {
arr = output;
}
}
**** Hidden Message *****
3.C++代码示例:
#include <iostream>
#include <vector>
void countingSort(std::vector<int>& arr) {
int n = arr.size();
std::vector<int> count(256, 0);
std::vector<int> output(n);
for (int i = 0; i < n; i++) {
count]++;
}
for (int i = 1; i < 256; i++) {
count += count;
}
for (int i = n - 1; i >= 0; i--) {
output] - 1] = arr;
count]--;
}
for (int i = 0; i < n; i++) {
arr = output;
}
}
**** Hidden Message *****
5.Java代码示例:
import java.util.Arrays;
public class CountingSort {
public static void countingSort(int[] arr) {
int n = arr.length;
int[] count = new int;
int[] output = new int;
for (int i = 0; i < n; i++) {
count]++;
}
for (int i = 1; i < 256; i++) {
count += count;
}
for (int i = n - 1; i >= 0; i--) {
output] - 1] = arr;
count]--;
}
for (int i = 0; i < n; i++) {
arr = output;
}
}
}**** Hidden Message *****
5.Python代码示例:
def countingSort(arr):
count = * 256
output = * len(arr)
for num in arr:
count += 1
for i in range(1, 256):
count += count
for num in reversed(arr):
output - 1] = num
count -= 1
for i in range(len(arr)):
arr = output
**** Hidden Message *****
这些示例代码演示了如何使用汇编语言、C语言、C++、Java和Python编写计数排序算法。您可以根据需要运行这些代码,并查看排序后的结果。
http://www.net188.com/data/attachment/forum/202401/09/131700ujdajenvj55tj9ti.jpg.thumb.jpg
扫码关注微信公众号,免费查看完整算法内容。
页:
[1]