Posted 浠g爜姘戝伐
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了相关的知识,希望对你有一定的参考价值。
馃惙鍩烘湰姒傚康|婕旂ず|绠楁硶浠g爜|鎬ц兘
鍩烘暟鎺掑簭锛圧adix Sort锛夛紝灞炰簬鍒嗛厤寮忔帓搴忥紙Distribution Sort锛夌畻娉曪紝瀹冪殑鍩烘湰鎬濇兂鏄皢鏁存暟鎸変綅杩涜鍒嗗壊锛岄€氳繃渚濇姣旇緝涓綅锛屽崄浣嶇瓑姣忎綅鏁版嵁鐨勫ぇ灏忚繘琛屽娆℃帓搴忥紝鐩磋嚦鏈€楂樹綅鎺掑簭瀹屾垚锛屾暣缁勬暟鎹嵆鎺掑簭瀹屾垚銆?/p>
瀵逛簬濡備笅杩欑粍鏁版嵁锛?br>
馃挳Java浠g爜 馃挳C++浠g爜 馃挳Python浠g爜 鍩烘暟鎺掑簭鏄?span class="mq-432">绋冲畾鐨勬帓搴忕畻娉曪紝鏃堕棿澶嶆潅搴︿负O(d(r+n))锛坮涓哄熀鏁帮紝d涓洪暱搴︼紝n涓哄叧閿瓧涓暟锛?/span>锛岀┖闂村鏉傚害涓?span class="mq-435">O(rd+n) WE WISH YOU HAPPY NEW YEAR 2019 以上是关于的主要内容,如果未能解决你的问题,请参考以下文章 1package bao3;
2public class Test {
3 public static int get_max(int a[]) {
4 int max=a[0];
5 for(int i=1;i<a.length;i++) {
6 if(a[i]>max)max=a[i];
7 }
8 return (int)Math.log10(max)+1;
9 }
10 public static void Sort(int a[]) {
11 int l=get_max(a);
12 int k=0;
13 while(k!=l) {
14 int but[][]=new int[10][a.length] ;
15 int c[]=new int[10];
16 for(int i=0;i<a.length;i++) {
17 int s=((int)(a[i]/(Math.pow(10, k))))%10;
18 but[s][c[s]++]=a[i];
19 }
20 int m=0;
21 for(int i=0;i<10;i++) {
22 for(int j=0;j<c[i];j++) {
23 a[m++]=but[i][j];
24 }
25 }
26 k++;
27 }
28 }
29 public static void main(String[] args) {
30 int a[]= {999,0,12,3,1,45,89,2,255,47,62,231};
31 Sort(a);
32 for(int i=0;i<a.length;i++) {
33 System.out.print(a[i]+" ");
34 }
35 }
36} 1#include<iostream>
2#include<cmath>
3using namespace std;
4int get_max(int a[], int n) {
5 int max = a[0];
6 for (int i = 1; i < n; i++) {
7 if (a[i] > max)max = a[i];
8 }
9 return (int)log10(max) + 1;
10}
11void Sort(int a[], int n) {
12 int l = get_max(a,n);
13 int k = 0;
14 while (k != l) {
15 int **but = new int*[10];
16 for (int i = 0; i < 10; i++) {
17 but[i] = new int[n];
18 }
19 int c[10] = {0};
20 for (int i = 0; i < n; i++) {
21 int s = (a[i] / (int)pow(10, k)) % 10;
22 but[s][c[s]++] = a[i];
23 }
24 int m = 0;
25 for (int i = 0; i < 10; i++) {
26 for (int j = 0; j < c[i]; j++) {
27 a[m++] = but[i][j];
28 }
29 }
30 k++;
31 for (int i = 0; i < 10; i++) {
32 delete[] but[i];
33 }
34 delete[] but;
35 }
36}
37int main() {
38 int a[12] = { 999,0,12,3,1,45,89,2,255,47,62,231 };
39 Sort(a,12);
40
41 for (int i = 0; i < 12; i++) {
42 cout << a[i] << " ";
43 }
44
45 return 0;
46} 1import numpy
2import math
3def get_max(nums):
4 max=nums[0]
5 for i in range(0,len(nums)):
6 if max<nums[i]:max=nums[i]
7 return int(math.log10(max)+1)
8def RadixSort(nums):
9 l=get_max(nums)
10 k=0
11 while k!=l:
12 but=numpy.zeros((10,len(nums)))
13 c=[0 for x in range(10)]
14 for i in range(0,len(nums)):
15 s=int(nums[i]/math.pow(10,k))%10
16 but[s][c[s]]=nums[i]
17 c[s]=c[s]+1
18 m=0
19 for i in range(0,10):
20 j=0
21 while(j<c[i]):
22 nums[m]=but[i][j]
23 m=m+1
24 j=j+1
25 k=k+1
26a=[999,0,12,3,1,45,89,2,255,47,62,231]
27RadixSort(a)
28print(a)