update translation radix-sort (#36502)
* update translation radix-sort identified code language and added implementation code python * docs(radix-sort): fixed spacing of comments
This commit is contained in:
committed by
Jonathan Graham
parent
97c5e3fb0b
commit
a5ab6f2d5f
@ -58,50 +58,67 @@ Agora, o array se torna: 10,11,17,21,123,34,44,654 Finalmente, classificamos de
|
||||
A matriz torna-se: 10,11,17,21,34,44,123,654 que é classificada. É assim que nosso algoritmo funciona.
|
||||
|
||||
Uma implementação em C:
|
||||
```
|
||||
```c
|
||||
void countsort(int arr[],int n,int place){
|
||||
int i,freq[range]={0}; // range for integers is 10 as digits range from 0-9
|
||||
int output[n];
|
||||
|
||||
int i,freq[range]={0}; //range for integers is 10 as digits range from 0-9
|
||||
for(i=0;i<n;i++)
|
||||
freq[(arr[i]/place)%range]++;
|
||||
|
||||
int output[n];
|
||||
for(i=1;i<range;i++)
|
||||
freq[i]+=freq[i-1];
|
||||
|
||||
for(i=0;i<n;i++)
|
||||
for(i=n-1;i>=0;i--){
|
||||
output[freq[(arr[i]/place)%range]-1]=arr[i];
|
||||
freq[(arr[i]/place)%range]--;
|
||||
}
|
||||
|
||||
freq[(arr[i]/place)%range]++;
|
||||
for(i=0;i<n;i++)
|
||||
arr[i]=output[i];
|
||||
}
|
||||
|
||||
for(i=1;i<range;i++)
|
||||
void radixsort(ll arr[],int n,int maxx){ // maxx is the maximum element in the array
|
||||
int mul=1;
|
||||
while(maxx){
|
||||
countsort(arr,n,mul);
|
||||
mul*=10;
|
||||
maxx/=10;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
freq[i]+=freq[i-1];
|
||||
Uma implementação em Python :
|
||||
|
||||
for(i=n-1;i>=0;i--){
|
||||
```py
|
||||
def counting_sort(arr, max_value, get_index):
|
||||
counts = [0] * max_value
|
||||
|
||||
output[freq[(arr[i]/place)%range]-1]=arr[i];
|
||||
# Counting - O(n)
|
||||
for a in arr:s
|
||||
counts[get_index(a)] += 1
|
||||
|
||||
freq[(arr[i]/place)%range]--;
|
||||
# Accumulating - O(k)
|
||||
for i, c in enumerate(counts):
|
||||
if i == 0:
|
||||
continue
|
||||
else:
|
||||
counts[i] += counts[i-1]
|
||||
|
||||
}
|
||||
# Calculating start index - O(k)
|
||||
for i, c in enumerate(counts[:-1]):
|
||||
if i == 0:
|
||||
counts[i] = 0
|
||||
counts[i+1] = c
|
||||
|
||||
for(i=0;i<n;i++)
|
||||
ret = [None] * len(arr)
|
||||
# Sorting - O(n)
|
||||
for a in arr:
|
||||
index = counts[get_index(a)]
|
||||
ret[index] = a
|
||||
counts[get_index(a)] += 1
|
||||
|
||||
arr[i]=output[i];
|
||||
|
||||
}
|
||||
|
||||
void radixsort(ll arr[],int n,int maxx){ //maxx is the maximum element in the array
|
||||
|
||||
int mul=1;
|
||||
|
||||
while(maxx){
|
||||
|
||||
countsort(arr,n,mul);
|
||||
|
||||
mul*=10;
|
||||
|
||||
maxx/=10;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
return ret
|
||||
```
|
||||
|
||||
### Mais Informações:
|
||||
|
Reference in New Issue
Block a user