源码资源网 发表于 2024-1-10 10:49:20

基数排序,分别用汇编语言,C语言,C++,java,python编写出来

基数排序比较特殊,特殊在它只能用在整数(自然数)排序,而且不是基于比较的,其原理是将整数按照位分成不同的数字,按照每个数各位值逐步排序。何为高位,比如 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,
    mov edi,
    cmp esi, edi
    jae skip_swap
    mov , edi
    mov , esi
skip_swap:
    **** Hidden Message *****

2.C语言代码示例:
#include <stdio.h>
void countSort(int arr[], int n, int exp) {
    int output;
    int count = {0};
    for (int i = 0; i < n; i++) {
      count[(arr / exp) % 10]++;
    }
    for (int i = 1; i < 10; i++) {
      count += count;
    }
    for (int i = n - 1; i >= 0; i--) {
      output / exp) % 10] - 1] = arr;
      count[(arr / exp) % 10]--;
    }
    for (int i = 0; i < n; i++) {
      arr = output;
    }
}
void radixSort(int arr[], int n) {
    int max = arr;
    for (int i = 1; i < n; i++) {
      if (arr > max) {
            max = arr;
      }
    }
    for (int exp = 1; max / exp > 0; exp *= 10) {
      countSort(arr, n, exp);
    }
}

**** Hidden Message *****

3.C++代码示例:
#include <iostream>
using namespace std;
void countSort(int arr[], int n, int exp) {
    int output;
    int count = {0};
    for (int i = 0; i < n; i++) {
      count[(arr / exp) % 10]++;
    }
    for (int i = 1; i < 10; i++) {
      count += count;
    }
    for (int i = n - 1; i >= 0; i--) {
      output / exp) % 10] - 1] = arr;
      count[(arr / exp) % 10]--;
    }
    for (int i = 0; i < n; i++) {
      arr = output;
    }
}
void radixSort(int arr[], int n) {
    int max = arr;
    for (int i = 1; i < n; i++) {
      if (arr > max) {
            max = arr;
      }
    }
    for (int exp = 1; max / exp > 0; exp *= 10) {
      countSort(arr, n, exp);
    }
}

**** Hidden Message *****

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;
      int[] count = new int;
      Arrays.fill(count, 0);
         for (int i = 0; i < n; i++) {
            count[(arr / exp) % 10]++;
      }
         for (int i = 1; i < 10; i++) {
            count += count;
      }
         for (int i = n - 1; i >= 0; i--) {
            output / exp) % 10] - 1] = arr;
            count[(arr / exp) % 10]--;
      }
         for (int i = 0; i < n; i++) {
            arr = output;
      }
    }
   
}**** Hidden Message *****

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 = * n
    count = * 10
   for i in range(n):
      index = (arr // exp) % 10
      count += 1
   for i in range(1, 10):
      count += count
   for i in range(n - 1, -1, -1):
      index = (arr // exp) % 10
      output - 1] = arr
      count -= 1
   for i in range(n):
      arr = output

**** Hidden Message *****

这些示例代码演示了基数排序算法的实现。您可以根据需要使用这些代码,并根据实际情况进行调整。

http://www.net188.com/data/attachment/forum/202401/09/131700ujdajenvj55tj9ti.jpg.thumb.jpg
扫码关注微信公众号,免费查看完整算法内容。

页: [1]
查看完整版本: 基数排序,分别用汇编语言,C语言,C++,java,python编写出来

免责声明:
1、本站提供的所有资源仅供参考学习使用,版权归原著所有,禁止下载本站资源参与商业和非法行为,请在24小时之内自行删除!
2、本站所有内容均由互联网收集整理、网友上传,并且以计算机技术研究交流为目的,仅供大家参考、学习,请勿任何商业目的与商业用途。
3、若您需要商业运营或用于其他商业活动,请您购买正版授权并合法使用。
4、论坛的所有内容都不保证其准确性,完整性,有效性,由于源码具有复制性,一经售出,概不退换。阅读本站内容因误导等因素而造成的损失本站不承担连带责任。
5、用户使用本网站必须遵守适用的法律法规,对于用户违法使用本站非法运营而引起的一切责任,由用户自行承担
6、本站所有资源来自互联网转载,版权归原著所有,用户访问和使用本站的条件是必须接受本站“免责声明”,如果不遵守,请勿访问或使用本网站
7、本站使用者因为违反本声明的规定而触犯中华人民共和国法律的,一切后果自己负责,本站不承担任何责任。
8、凡以任何方式登陆本网站或直接、间接使用本网站资料者,视为自愿接受本网站声明的约束。
9、本站以《2013 中华人民共和国计算机软件保护条例》第二章 “软件著作权” 第十七条为原则:为了学习和研究软件内含的设计思想和原理,通过安装、显示、传输或者存储软件等方式使用软件的,可以不经软件著作权人许可,不向其支付报酬。若有学员需要商用本站资源,请务必联系版权方购买正版授权!
10、本网站如无意中侵犯了某个企业或个人的知识产权,请来信【站长信箱312337667@qq.com】告之,本站将立即删除。
郑重声明:
本站所有资源仅供用户本地电脑学习源代码的内含设计思想和原理,禁止任何其他用途!
本站所有资源、教程来自互联网转载,仅供学习交流,不得商业运营资源,不确保资源完整性,图片和资源仅供参考,不提供任何技术服务。
本站资源仅供本地编辑研究学习参考,禁止未经资源商正版授权参与任何商业行为,违法行为!如需商业请购买各资源商正版授权
本站仅收集资源,提供用户自学研究使用,本站不存在私自接受协助用户架设游戏或资源,非法运营资源行为。