0% found this document useful (0 votes)
11 views5 pages

DAA Programs

Appa

Uploaded by

dhananjeyans41
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views5 pages

DAA Programs

Appa

Uploaded by

dhananjeyans41
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

MAX OF N ELEMENTS

Aim:
To find the maximum of n elements
Algorithm:

Program :
def findmax(a,n):
if (n%2==1):
max=a[0]
min=a[0]
i=1
else:
if a[0]<a[1]:
max=a[1]
min=a[0]
else:
max=a[0]
min=a[1]
i=2
while i<n:
if a[i]<a[i+1]:
if a[i]<min:
min=a[i]
if a[i+1]> max:
max=a[i+1]
else:
if a[i]>max:
max=a[i]
if a[i+1]<min:
min=a[i+1]
i=i+2
return max
n=int(input("enter size"))
l=[]
for i in range (n):
x=int(input("enter element"))
l.append(x)
print("max is", findmax(l,n))
traversing 2d array
def traverse_2darray(mat,row,col):
for i in range (row):
for j in range(col):
print(mat[i][j], end=' ')
return mat

r=int(input("enter the number of rows"))


c=int(input("enter the number of columns"))
m=[]
print("enter the elements to be added")
for i in range(1,r+1):
a=[]
for j in range(1,c+1):
x=int(input())
a.append(x)
m.append(a)
print(traverse_2darray(m,r,c))
BUBBLE SORT

a=[]
n=int(input("number of elements"))
print("enter the elements to be added")
for i in range(n):
x=int(input())
a.append(x)
for i in range(n):
for j in range(i+1):
if a[j]>a[i]:
a[i],a[j]=a[j],a[i]
print(a)
LINEAR SEARCH

def linearsrch(n,a,l):
for i in range(n):
if l[i]==a:
print("element found at:")
return i
else:
print("element not found")
n=int(input("num of elements"))
print("enter the elements")
l=[]
for i in range (n):
x=int(input())
l.append(x)
y=int(input("enter element to be searched"))
print(linearsrch(x,y,l))

You might also like