|
计数排序,不是基于比较,而是基于计数。 计数排序步骤如下: - 遍历数组,找出最大值和最小值。
- 根据最大值和最小值,初始化对应的统计元素数量的数组。
- 遍历元素,统计元素个数到新的数组。
- 遍历统计的数组,按照顺序输出排序的数组元素。
计数排序,分别用汇编语言,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, [arr + esi]
- inc byte [count + eax]
- inc esi
- loop count_loop
- mov esi, 0
- mov edi, 0
- output_loop:
- mov al, byte [count + esi]
- test al, al
- jz skip_output
- mov ecx, eax
- mov edx, esi
- mov eax, 1
- mov ebx, 1
- int 0x80
- skip_output:
-
复制代码 inc esi
loop output_loop
exit:
mov eax, 1
xor ebx, ebx
int 0x80
2.C语言代码示例:
- #include <stdio.h>
- void countingSort(int arr[], int n) {
- int count[256] = {0};
- int output[n];
- for (int i = 0; i < n; i++) {
- count[arr[i]]++;
- }
- for (int i = 1; i < 256; i++) {
- count[i] += count[i - 1];
- }
- for (int i = n - 1; i >= 0; i--) {
- output[count[arr[i]] - 1] = arr[i];
- count[arr[i]]--;
- }
- for (int i = 0; i < n; i++) {
- arr[i] = output[i];
- }
- }
复制代码 int main() {
int arr[] = {5, 3, 1, 2, 4};
int n = sizeof(arr) / sizeof(arr[0]);
countingSort(arr, n);
printf("Sorted array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr);
}
printf("\n");
return 0;
}
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[arr[i]]++;
- }
- for (int i = 1; i < 256; i++) {
- count[i] += count[i - 1];
- }
- for (int i = n - 1; i >= 0; i--) {
- output[count[arr[i]] - 1] = arr[i];
- count[arr[i]]--;
- }
- for (int i = 0; i < n; i++) {
- arr[i] = output[i];
- }
- }
复制代码 int main() {
std::vector<int> arr = {5, 3, 1, 2, 4};
countingSort(arr);
std::cout << "Sorted array: ";
for (int num : arr) {
std::cout << num << " ";
}
std::cout << std::endl;
return 0;
}
4.Java代码示例:
- import java.util.Arrays;
- public class CountingSort {
- public static void countingSort(int[] arr) {
- int n = arr.length;
- int[] count = new int[256];
- int[] output = new int[n];
- for (int i = 0; i < n; i++) {
- count[arr[i]]++;
- }
- for (int i = 1; i < 256; i++) {
- count[i] += count[i - 1];
- }
- for (int i = n - 1; i >= 0; i--) {
- output[count[arr[i]] - 1] = arr[i];
- count[arr[i]]--;
- }
- for (int i = 0; i < n; i++) {
- arr[i] = output[i];
- }
- }
-
- }
复制代码 public static void main(String[] args) {
int[] arr = {5, 3, 1, 2, 4};
countingSort(arr);
System.out.print("Sorted array: ");
for (int num : arr) {
System.out.print(num + " ");
}
System.out.println();
}
5.Python代码示例:
- def countingSort(arr):
- count = [0] * 256
- output = [0] * len(arr)
- for num in arr:
- count[num] += 1
- for i in range(1, 256):
- count[i] += count[i - 1]
- for num in reversed(arr):
- output[count[num] - 1] = num
- count[num] -= 1
- for i in range(len(arr)):
- arr[i] = output[i]
复制代码 arr = [5, 3, 1, 2, 4]
countingSort(arr)
print("Sorted array:", arr)
这些示例代码演示了如何使用汇编语言、C语言、C++、Java和Python编写计数排序算法。您可以根据需要运行这些代码,并查看排序后的结果。
|
|