Docsity

RadixSort Código python, Transcripciones de Informática

Vista previa parcial del texto

¡Descarga RadixSort Código python y más Transcripciones en PDF de Informática solo en Docsity!

CÓDIGO RADIX SORT.

CÓDIGO RADIX SORT. 1 f Python program for implementation of Radix Sort 2 $ A function to do counting sort of arr[] according to 3 $ the digit represented by exp. 4 5 Edef countingSort (A, expl): 6 7 n= len(a) B 3 Í The output array elements that will have sorted arr 10 B= [0] * (n) 11 ne É initialize count array as Ú 13 2 = [0] * (10) 14 15 $ Store count of occurrences in count [] 16 E for i in range(0, n): 17 index = (A[i] / expl) 18 + Clint (index $ 10)] =c[int(index $ 10)]+ 1 19 20 F Change count[i] so that count[i] now contains actual 21 ÍF position of this digit in output array 22 y for i in range(1, 10): O elil= erij+oti - 1] 24 25 $ Build the output array 26 i=n- 1 22 4 while 1 >= 0: 28 index = (a[i] / expl) 29 B[C[int (index $ 10)] - 1] = A[il 30 Clint (index $ 10)] =c[int(index $ 10)]- 1 Ei E i-=1 32 32 sa EE ES] 36 37 38 39 40 41 42 pl] 44 as 46 47 48 49 50 51 52 Ss 54 se 56 ST 58 59 60 6l 62 E 5 Í Copying the output array to arr[], É so that arr now contains sorted numbers i=0 for i in range(0, len(A)): A[i] = B[il $ Method to do Radix Sort def radixSort(A): $ Find the maximum number to know number of digits maxl — max(A) 3 Do counting sort for every digit. Note that instead d of passing digit number, exp is passed. exp i where i is current digit number exp = 1 while maxl / exp > 0: countingSort(A, exp) exp *= 10 $ Driver code A= [170, 45, 75, 90, B02, 24, 2, 66] $ Function Call radixSort (A) for i in rangs(len(a)): print (a[il)