|
基数排序比较特殊,特殊在它只能用在整数(自然数)排序,而且不是基于比较的,其原理是将整数按照位分成不同的数字,按照每个数各位值逐步排序。何为高位,比如 81,1 就是低位, 8 就是高位。 分为高位优先和低位优先,先比较高位就是高位优先,先比较低位就是低位优先。下面我们讲高位优先。 主要的步骤如下: - 将所有元素统一称为统一数位长度,前面补 0。
- 从最高位开始,依次排序,从最高位到最低位遍历完,数组就是有序的。
基数排序,分别用汇编语言,C语言,C++,java,python编写出来
以下是使用汇编语言、C语言、C++、Java和Python编写的基数排序算法示例代码:
1.汇编语言(x86)代码示例:
- assembly
- section .data
- arr dd 170, 45, 75, 90, 802, 24, 2, 66
- n equ $-arr
- temp dd 0
- section .text
- global _start
- _start:
- mov ecx, n
- dec ecx
- mov ebx, 1
- outer_loop:
- xor eax, eax
- mov edx, 10
- inner_loop:
- mov esi, eax
- div edx
- mov eax, edx
- xor edx, edx
- mov edx, esi
- cmp edx, ecx
- jbe skip_swap
- mov esi, [arr + edx * 4]
- mov edi, [arr + eax * 4]
- cmp esi, edi
- jae skip_swap
- mov [arr + edx * 4], edi
- mov [arr + eax * 4], esi
- skip_swap:
-
复制代码 add eax, ebx
cmp eax, ecx
jbe inner_loop
dec ecx
cmp ecx, 0
jge outer_loop
jmp exit
exit:
mov eax, 1 ; 退出系统调用
xor ebx, ebx ; 返回值为0
int 0x80 ; 调用系统调用
2.C语言代码示例:
- #include <stdio.h>
- void countSort(int arr[], int n, int exp) {
- int output[n];
- int count[10] = {0};
- for (int i = 0; i < n; i++) {
- count[(arr[i] / exp) % 10]++;
- }
- for (int i = 1; i < 10; i++) {
- count[i] += count[i - 1];
- }
- for (int i = n - 1; i >= 0; i--) {
- output[count[(arr[i] / exp) % 10] - 1] = arr[i];
- count[(arr[i] / exp) % 10]--;
- }
- for (int i = 0; i < n; i++) {
- arr[i] = output[i];
- }
- }
- void radixSort(int arr[], int n) {
- int max = arr[0];
- for (int i = 1; i < n; i++) {
- if (arr[i] > max) {
- max = arr[i];
- }
- }
- for (int exp = 1; max / exp > 0; exp *= 10) {
- countSort(arr, n, exp);
- }
- }
复制代码 int main() {
int arr[] = {170, 45, 75, 90, 802, 24, 2, 66};
int n = sizeof(arr) / sizeof(arr[0]);
radixSort(arr, n);
printf("Sorted array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr);
}
printf("\n");
return 0;
}
3.C++代码示例:
- #include <iostream>
- using namespace std;
- void countSort(int arr[], int n, int exp) {
- int output[n];
- int count[10] = {0};
- for (int i = 0; i < n; i++) {
- count[(arr[i] / exp) % 10]++;
- }
- for (int i = 1; i < 10; i++) {
- count[i] += count[i - 1];
- }
- for (int i = n - 1; i >= 0; i--) {
- output[count[(arr[i] / exp) % 10] - 1] = arr[i];
- count[(arr[i] / exp) % 10]--;
- }
- for (int i = 0; i < n; i++) {
- arr[i] = output[i];
- }
- }
- void radixSort(int arr[], int n) {
- int max = arr[0];
- for (int i = 1; i < n; i++) {
- if (arr[i] > max) {
- max = arr[i];
- }
- }
- for (int exp = 1; max / exp > 0; exp *= 10) {
- countSort(arr, n, exp);
- }
- }
复制代码 int main() {
int arr[] = {170, 45, 75, 90, 802, 24, 2, 66};
int n = sizeof(arr) / sizeof(arr[0]);
radixSort(arr, n);
cout << "Sorted array: ";
for (int i = 0; i < n; i++) {
cout << arr << " ";
}
cout << endl;
return 0;
}
4.Java代码示例:
- import java.util.Arrays;
- public class RadixSort {
- public static void radixSort(int[] arr) {
- int max = Arrays.stream(arr).max().getAsInt();
- for (int exp = 1; max / exp > 0; exp *= 10) {
- countSort(arr, exp);
- }
- }
- public static void countSort(int[] arr, int exp) {
- int n = arr.length;
- int[] output = new int[n];
- int[] count = new int[10];
- Arrays.fill(count, 0);
- for (int i = 0; i < n; i++) {
- count[(arr[i] / exp) % 10]++;
- }
- for (int i = 1; i < 10; i++) {
- count[i] += count[i - 1];
- }
- for (int i = n - 1; i >= 0; i--) {
- output[count[(arr[i] / exp) % 10] - 1] = arr[i];
- count[(arr[i] / exp) % 10]--;
- }
- for (int i = 0; i < n; i++) {
- arr[i] = output[i];
- }
- }
-
- }
复制代码 public static void main(String[] args) {
int[] arr = {170, 45, 75, 90, 802, 24, 2, 66};
radixSort(arr);
System.out.println("Sorted array: " + Arrays.toString(arr));
}
5.Python代码示例:
- def radixSort(arr):
- max_num = max(arr)
- exp = 1
- while max_num // exp > 0:
- countSort(arr, exp)
- exp *= 10
- def countSort(arr, exp):
- n = len(arr)
- output = [0] * n
- count = [0] * 10
- for i in range(n):
- index = (arr[i] // exp) % 10
- count[index] += 1
- for i in range(1, 10):
- count[i] += count[i - 1]
- for i in range(n - 1, -1, -1):
- index = (arr[i] // exp) % 10
- output[count[index] - 1] = arr[i]
- count[index] -= 1
- for i in range(n):
- arr[i] = output[i]
复制代码 arr = [170, 45, 75, 90, 802, 24, 2, 66]
radixSort(arr)
print("Sorted array:", arr)
这些示例代码演示了基数排序算法的实现。您可以根据需要使用这些代码,并根据实际情况进行调整。
|
|