Skip to content

Commit ec90d91

Browse files
authored
Added Codes to Repo
1 parent 99cf7d7 commit ec90d91

3 files changed

Lines changed: 183 additions & 0 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# To demonstrate Basic Euclidean Algorithm
2+
3+
def gcd(a, b):
4+
if a == 0 :
5+
return b
6+
return gcd(b%a, a)
7+
8+
a = 55
9+
b = 20
10+
print("gcd(", a , "," , b, ") = ", gcd(a, b))
11+
12+
c = 94
13+
d = 6
14+
print("gcd(", c , "," , d, ") = ", gcd(a, b))
15+
16+
e = 34
17+
f = 69
18+
print("gcd(", e , "," , f, ") = ", gcd(a, b))

Max no. in Array.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#Program to find maximum in arr[] of size n
2+
3+
def largest(arr,n):
4+
max = arr[0]
5+
for i in range(1, n):
6+
if arr[i] > max:
7+
max = arr[i]
8+
return max
9+
10+
arr = [2, 24, 54, 121, 2201, 662, 313, 535]
11+
n = len(arr)
12+
ans = largest(arr,n)
13+
print ("Largest in given array is",ans)

Sorting Codes.py

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
# Selection Sort
2+
import sys
3+
a = [ 170, 40, 72, 90, 802, 24, 2, 66, 32]
4+
string = "SUBSCRIBETOSEEDITSOLUTIONS"
5+
6+
for i in range(len(a)):
7+
min_idx = i
8+
for j in range(i+1, len(a)):
9+
if a[min_idx] > a[j]:
10+
min_idx = j
11+
12+
a[i], a[min_idx] = a[min_idx], a[i]
13+
14+
print ("Selection Sorted array: ")
15+
for i in range(len(a)):
16+
print("%d \t" %a[i], sep=' ', end='', flush=True)
17+
18+
# Bubble Sort
19+
def bubbleSort(a):
20+
n = len(a)
21+
for i in range(n-1):
22+
for j in range(0, n-i-1):
23+
if a[j] > a[j+1] :
24+
a[j], a[j+1] = a[j+1], a[j]
25+
26+
27+
# Insertion Sort
28+
def insertionSort(a):
29+
for i in range(1, len(a)):
30+
key = a[i]
31+
j = i-1
32+
while j >=0 and key < a[j] :
33+
a[j+1] = a[j]
34+
j -= 1
35+
a[j+1] = key
36+
37+
38+
# Heap Sort
39+
def heapify(a, n, i):
40+
largest = i
41+
l = 2 * i + 1
42+
r = 2 * i + 2
43+
if l < n and a[i] < a[l]:
44+
largest = l
45+
46+
if r < n and a[largest] < a[r]:
47+
largest = r
48+
49+
if largest != i:
50+
a[i],a[largest] = a[largest],a[i]
51+
heapify(a, n, largest)
52+
53+
def heapSort(a):
54+
n = len(a)
55+
for i in range(n // 2 - 1, -1, -1):
56+
heapify(a, n, i)
57+
58+
for i in range(n-1, 0, -1):
59+
a[i], a[0] = a[0], a[i]
60+
heapify(a, i, 0)
61+
62+
# Radix Sort
63+
def countingSort(a, exp1):
64+
n = len(a)
65+
output = [0] * (n)
66+
count = [0] * (10)
67+
for i in range(0, n):
68+
index = (a[i]/exp1)
69+
count[int((index)%10)] += 1
70+
71+
for i in range(1,10):
72+
count[i] += count[i-1]
73+
i = n-1
74+
while i>=0:
75+
index = (a[i]/exp1)
76+
output[ count[ int((index)%10) ] - 1] = a[i]
77+
count[int((index)%10)] -= 1
78+
i -= 1
79+
i = 0
80+
for i in range(0,len(a)):
81+
a[i] = output[i]
82+
83+
def radixSort(a):
84+
max1 = max(a)
85+
exp = 1
86+
while max1/exp > 0:
87+
countingSort(a,exp)
88+
exp *= 10
89+
90+
# Shell Sort
91+
def shellSort(a):
92+
n = len(a)
93+
gap = n/2
94+
while int(gap) > 0:
95+
for i in range(int(gap),int(n)):
96+
temp = a[i]
97+
j = int(i)
98+
while int(j) >= int(gap) and a[int(j)-int(gap)] >temp:
99+
a[j] = a[j-gap]
100+
j -= gap
101+
a[j] = temp
102+
gap /= 2
103+
104+
# Counting sort
105+
def countSort(string):
106+
output = [0 for i in range(256)]
107+
count = [0 for i in range(256)]
108+
ans = ["" for _ in string]
109+
for i in string:
110+
count[ord(i)] += 1
111+
112+
for i in range(256):
113+
count[i] += count[i-1]
114+
115+
for i in range(len(string)):
116+
output[count[ord(string[i])]-1] = string[i]
117+
count[ord(string[i])] -= 1
118+
119+
for i in range(len(string)):
120+
ans[i] = output[i]
121+
return ans
122+
123+
124+
bubbleSort(a)
125+
print ("\nBubble Sorted array:")
126+
for i in range(len(a)):
127+
print ("%d \t" %a[i], sep=' ', end='', flush=True)
128+
129+
insertionSort(a)
130+
print ("\nInsertion Sorted array:")
131+
for i in range(len(a)):
132+
print ("%d\t" %a[i], sep=' ', end='', flush=True)
133+
134+
heapSort(a)
135+
n = len(a)
136+
print ("\nHeap Sorted array:")
137+
for i in range(n):
138+
print ("%d\t" %a[i], sep=' ', end='', flush=True)
139+
140+
radixSort(a)
141+
print ("\nRadix Sorted array:")
142+
for i in range(len(a)):
143+
print("%d\t" %a[i], sep=' ', end='', flush=True)
144+
145+
shellSort(a)
146+
print ("\nShell Sorted array:")
147+
for i in range(n):
148+
print("%d\t" %a[i], sep=' ', end='', flush=True)
149+
150+
b = countSort(string)
151+
print("\nSorted character array is %s" %(" ".join(b)))
152+

0 commit comments

Comments
 (0)