- Notifications
You must be signed in to change notification settings - Fork 366
/
Copy pathSorting-using-trivial-hash-function.py
46 lines (33 loc) · 905 Bytes
/
Sorting-using-trivial-hash-function.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# Python3 program to sort an array
# using hash function
defsortUsingHash(a, n):
# find the maximum element
Max=max(a)
# create a hash function upto
# the max size
Hash= [0] * (Max+1)
# traverse through all the elements
# and keep a count
foriinrange(0, n):
Hash[a[i]] +=1
# Traverse upto all elements and check
# if it is present or not. If it is
# present, then print the element the
# number of times it's present. Once we
# have printed n times, that means we
# have printed n elements so break out
# of the loop
foriinrange(0, Max+1):
# if present
ifHash[i] !=0:
# print the element that number
# of times it's present
forjinrange(0, Hash[i]):
print(i, end=" ")
# Driver Code
if__name__=="__main__":
a= [9, 4, 3, 2, 5, 2, 1, 0, 4,
3, 5, 10, 15, 12, 18, 20, 19]
n=len(a)
sortUsingHash(a, n)
#This code is given by Aman.