Professional Documents
Culture Documents
3. Take user defined 10 numbers. Make a list out of this. Arrange them in
ascending and descending order.
lst1 = []
#sorting
for i in range(len(lst1)) :
for j in range(i+1, len(lst1)) :
if lst1[i] > lst1[j] :
lst1[i], lst1[j] = lst1[j], lst1[i]
Enter 10 numbers :
10
0
4
7
21
23
67
54
34
2
[10, 0, 4, 7, 21, 23, 67, 54, 34, 2]
Ascending Order : [0, 2, 4, 7, 10, 21, 23, 34, 54, 67]
Descending Order : [67, 54, 34, 23, 21, 10, 7, 4, 2, 0]
4. Take another list of 5 numbers and insert the numbers to the previous list at
their proper position. (Insertion sort)
lst1 = []
lst2 = []
#sorting
for i in range(len(lst1)) :
for j in range(i+1, len(lst1)) :
if lst1[i] > lst1[j] :
lst1[i], lst1[j] = lst1[j], lst1[i]
#insertion
print("Enter more 5 numbers : ")
for i in range(5) :
lst2.append(int(input()))
print(lst2)
for i in range(len(lst2)) :
for j in range(len(lst1)) :
if lst2[i] <= lst1[j] and lst2[i] > lst1[j-1] :
lst1.insert(j, lst2[i])
print(lst1)
Enter 10 numbers :
10
0
21
24
31
6
3
9
12
16
[10, 0, 21, 24, 31, 6, 3, 9, 12, 16]
Enter more 5 numbers :
7
8
15
2
1
[7, 8, 15, 2, 1]
[0, 1, 2, 3, 6, 7, 8, 9, 10, 12, 15, 16, 21, 24, 31]
5. Write a python code to implement Selection Sort, Quick Sort and Merge Sort.
Selection Sort :-
a = list(map(int, input("Enter the numbers : ").split()))
for i in range(len(a)) :
temp = a[i]
temp_index = i
for j in range(i+1, len(a)) :
if temp > a[j] :
temp = a[j]
temp_index = j
a[i],a[temp_index] = temp, a[i]
print(a)
lst[i + 1], lst[high] = lst[high], lst[i + 1]
return i + 1
def quickSort(lst, low, high):
if low < high:
pi = partition(lst, low, high)
quickSort(lst, low, pi - 1)
quickSort(lst, pi + 1, high)
data = list(map(int, input("Enter the numbers : ").split()))
print(data)
quickSort(data, 0, len(data) - 1)
print("Sorted Array in Ascending Order : ")
print(data)
Merge Sort :-
def merge(lst, l, m, r):
n1 = m - l + 1
n2 = r - m
L = [0] * (n1)
R = [0] * (n2)
for i in range(0, n1):
L[i] = lst[l + i]
for j in range(0, n2):
R[j] = lst[m + 1 + j]
i = 0
j = 0
k = l
while i < n1 and j < n2:
if L[i] <= R[j]:
lst[k] = L[i]
i += 1
else:
lst[k] = R[j]
j += 1
k += 1
while i < n1:
lst[k] = L[i]
i += 1
k += 1
while j < n2:
lst[k] = R[j]
j += 1
k += 1
def mergeSort(lst, l, r):
if l < r:
m = l+(r-l)//2
mergeSort(lst, l, m)
mergeSort(lst, m+1, r)
merge(lst, l, m, r)
lst = list(map(int, input("Enter the numbers : ").split()))
n = len(lst)
print("Given List is")
print(lst)
mergeSort(lst, 0, n-1)
print("\n\nSorted List is")
print(lst)
Sorted List is
[0, 2, 3, 5, 6, 17, 23]
while b:
a, b = b, a % b
print(a)
def bit_calc(a,b,operation) :
if operation == "and" :
for i in range(max(len(a), len(b))) :
if a[i]==b[i] and a[i]==1 :
c.append(int(1))
else :
c.append(int(0))
print("And operation is :", c)
elif operation == "or" :
for i in range(max(len(a), len(b))) :
if a[i]==b[i] and a[i]==0 :
c.append(int(0))
else :
c.append(int(1))
print("Or operation is :", c)
elif operation == "xor" :
for i in range(max(len(a), len(b))) :
if a[i]==b[i] :
c.append(int(0))
else :
c.append(int(1))
print("Xor operation is :", c)
print("List a is :",a)
print("List b is :",b)
oper = input("Enter operation to perform : ")
bit_calc(a,b,oper)
def add_op(a,b) :
carry = 0
for i in range(max(len(a),len(b))-1, -1, -1) :
sum = a[i] + b[i] + carry
if sum>1 :
sum = 0
carry = 1
c.insert(0,sum)
print("Addition is :", c)
def sub_op(a,b) :
borrow = 0
for i in range(max(len(a),len(b))-1, -1, -1) :
sub = a[i] + borrow - b[i]
if sub<0 :
sub += 2
borrrow = 1
c.insert(0,sub)
print("Subtraction is :", c)
def mul_op(a,b) :
if len(a)>len(b) :
b = (len(a)-len(b)) + b
else:
a = (len(b)-len(a)) + a
ans = len(a)
for i in range(len(a)-1,-1,-1):
if b[i]==0:
temp = len(a) + (len(a)-1-i)
temp_ans = ans
ans = add_op(temp,temp_ans)
else:
temp = a + (len(a)-1-i)
temp_ans = ans
ans = add_op(temp,temp_ans)
print("Multiplication is :", ans)
def div_op(dividend, divisor) :
def binary_compare(a, b):
"""
Compares two binary numbers a and b and returns:
1 if a > b
0 if a == b
-1 if a < b
"""
diff = len(a) - len(b)
if diff > 0:
b = diff + b
elif diff < 0:
a = (-diff) + a
for i in range(len(a)):
if a[i] > b[i]:
return 1
elif a[i] < b[i]:
return -1
return 0
quotient = []
remainder = []
for i in range(len(dividend)):
remainder += dividend[i]
if binary_compare(remainder, divisor) >= 0:
remainder = sub_op(remainder, divisor)
quotient.append(1)
else:
quotient.append(0)
#quotient = quotient.lstrip('0')
if quotient == []:
quotient.append(0)
print(“Division quotient is :”,quotient,”and remainder
is :”, remainder)
print("List a is :",a)
print("List b is :",b)
add_op(a,b)
sub_op(a,b)
mul_op(a,b)
div_op(a,b)