8000 Merge pull request #122 from ramvikrams/nw · cation03/Basic-Python-Programs@8846b68 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8846b68

Browse files
Merge pull request souravjain540#122 from ramvikrams/nw
radixSort.py
2 parents ff518c2 + 072fb80 commit 8846b68

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

GrayCodetoBinary.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
def flip_num(my_nu):
2+
return '1' if(my_nu == '0') else '0';
3+
4+
def gray_to_binary(gray):
5+
binary_code=""
6+
binary_code += gray[0]
7+
for i in range(1,len(gray)):
8+
9+
if (gray[i]=='0'):
10+
binary_code += binary_code[i-1]
11+
else:
12+
binary_code += flip_num(binary_code[i-1])
13+
14+
return binary_code
15+
16+
# gray_code="01101001"
17+
18+
19+
gray_code=input("please enter the gray code\n")
20+
print("the gray code is : ")
21+
print(gray_code)
22+
# x=gray_to_binary(gray_code)
23+
print("binary code of", gray_code, "is",gray_to_binary(gray_code))
24+
25+
# for converting binary numb to decimal
26+
value=0
27+
b_num=list(gray_to_binary(gray_code))
28+
29+
for i in range(len(b_num)):
30+
digit=b_num.pop()
31+
if digit =='1':
32+
value = value + pow(2,i)
33+
34+
print("the decimal value of the number is ", value)
35+
36+
37+
# print(12//5)

radixSort.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Python program for implementation of Radix Sort
2+
3+
# A function to do counting sort of arr[] according to
4+
# the digit represented by exp.
5+
def countingSort(arr, exp1):
6+
7+
n = len(arr)
8+
9+
# The output array elements that will have sorted arr
10+
output = [0] * (n)
11+
12+
# initialize count array as 0
13+
count = [0] * (10)
14+
15+
# Store count of occurrences in count[]
16+
for i in range(0, n):
17+
index = (arr[i]/exp1)
18+
count[int((index)%10)] += 1
19+
20+
# Change count[i] so that count[i] now contains actual
21+
# position of this digit in output array
22+
for i in range(1,10):
23+
count[i] += count[i-1]
24+
25+
# Build the output array
26+
i = n-1
27+
while i>=0:
28+
index = (arr[i]/exp1)
29+
output[ count[ int((index)%10) ] - 1] = arr[i]
30+
count[int((index)%10)] -= 1
31+
i -= 1
32+
33+
# Copying the output array to arr[],
34+
# so that arr now contains sorted numbers
35+
i = 0
36+
for i in range(0,len(arr)):
37+
arr[i] = output[i]
38+
39+
# Method to do Radix Sort
40+
def radixSort(arr):
41+
42+
# Find the maximum number to know number of digits
43+
max1 = max(arr)
44+
45+
# Do counting sort for every digit. Note that instead
46+
# of passing digit number, exp is passed. exp is 10^i
47+
# where i is current digit number
48+
exp = 1
49+
while max1/exp > 0:
50+
countingSort(arr,exp)
51+
exp *= 10
52+
53+
# Driver code to test above
54+
arr = [ 170, 45, 75, 90, 802, 24, 2, 66]
55+
radixSort(arr)
56+
57+
for i in range(len(arr)):
58+
print(arr[i],end=" ")
59+
60+

0 commit comments

Comments
 (0)
0