## Blog Pages

### Program for Counting Sort

def countSort(arr):

output = [0 for i in range(256)]

count = [0 for i in range(256)]

ans = ["" for _ in arr]

for i in arr:

count[ord(i)] += 1

for i in range(256):

count[i] += count[i-1]

for i in range(len(arr)):

output[count[ord(arr[i])]-1] = arr[i]

count[ord(arr[i])] -= 1

for i in range(len(arr)):

ans[i] = output[i]

return ans

arr = "Pythonforengineers"

ans = countSort(arr)

print ("Sorted character array is %s" %("".join(ans)))

def countingSort(arr, exp1):

n = len(arr)

output =  * (n)

count =  * (10)

for i in range(0, n):

index = (arr[i]/exp1)

count[int((index)%10)] += 1

for i in range(1,10):

count[i] += count[i-1]

i = n-1

while i>=0:

index = (arr[i]/exp1)

output[ count[ int((index)%10) ] - 1] = arr[i]

count[int((index)%10)] -= 1

i -= 1

i = 0

for i in range(0,len(arr)):

arr[i] = output[i]

max1 = max(arr)

exp = 1

while max1/exp > 0:

countingSort(arr,exp)

exp *= 10

arr = [ 170, 45, 75, 90, 802, 24, 2, 66]