0% found this document useful (0 votes)
148 views8 pages

Seris

akjhsgdia;kl

Uploaded by

imsachin549
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)
148 views8 pages

Seris

akjhsgdia;kl

Uploaded by

imsachin549
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

Assignment-2

Name: Venkatesh
DSA-2 ID: 2020ucp1048

Q.1 Linear search for searching an element x in array and print.


Ans. (a.) Second occurrence
count=0;
for k =0 to k=n-1
if (A[k]==x) count ++;
end if
if (count==2) return k;
end if
end for
print error message second occurrence of x not found
(b.) last occurrence
int last_oc= o;
for k=o to k=n-1
if (A[k]==x) last_oc=k;
end for
Print last occurrence
If (last_oc==o)
Print error message occurrence of x not founf=d
end if
(c.) Occurrence nearest to p
Int near_oc=n;
For i=0 to i=n-1
If (A[i]==x)
If(pi<near_oc&& p-i=0) near_oc = I;
Else if (i-p<near_oc &&i-p>=0) near_oc=I;
end if
end if
end for
print near oc or error message

(d.) All occurrences between index p and q


Int count=0;
for i=p to q
if(A[i]=x)
print i;
count ++;
end if
end for
if (count==0)
print error message
end if
(e.) All occurrence
int count=0;
for i=0 to n-1
if (A[i]=x)
print i;
count++;
end if
end for
if (count==0)
print error message
end if
Q.2 A is sorted in ascending order, B is sorted in descending order. Merge to get array in:
Ans.
(a.) Ascending Order
A- size m- ascending order
B- size n- descending order
Outcome C size (m+n) ascending order
arr[m+n] // array
i=k=0
j = n-1

while (i < m) && (j >= 0)


If(A[i] < B[j])
arr[k] = A[i]

i++; k++;
else if (A[i] == B[j])

arr[k] = A[i]
k++;

arr[k] = B[j]
i++; j--; k++;

else
arr[k] = B[j]
k++; j--;

endif
endwhile

while(i < m)

arr[k] = A[i]
k++; i++;

endwhile

while (j >= 0)

arr[k] = B[j]
k++; j -- ;
print arr
(b.) Descending Order
m = length(A)
n = length(B)

arr[m+n] // array

j=k=0
i = m-1

while (i >= 0) && (j < n)


If(A[i] < B[j])

arr[k] = B[j]
j++; k++;
else if (A[i] == B[j])
arr[k] = A[i]
k++;

arr[k] = B[j]
i--; j++; k++;
else
arr[k] = A[i]
k++; i--;

endif
endwhile

while (i >= 0)
arr[k] = A[i]
k++; i--;

endwhile
while (j < n)
arr[k] = B[j]
k++; j ++
print arr
Q.3 A is sorted in ascending order, B is sorted in descending order, C is sorted in
ascending order. Merge these three to get an array in:
Ans.
(a.) Ascending Order
m = length(A)
n = length(B)

arr[m+n] // array
i=k=0
j = n-1

Q[m+n+o]//answer
while (i < m) && ( j >= 0 )

If(A[i] < B[j])


arr[k] = A[i]
i++; k++;

elseif (A[i] == B[j])


arr[k] = A[i]
k++ ;
arr[k] = B[j]
i++ ; j-- ; k++ ;

else

arr[k] = B[j]
k++ ; j-- ;

endif
endwhile

while(i < m)
arr[k] = A[i]
k++ ; i++ ;
endwhile
while (j >= 0)
arr[k] = B[j]
k++ ; j -- ;
I=0,j=0,k=0;

while ( i < m+n) && ( j<o )


If( arr[i] < C[j])
Q[k] = arr[i]
i++ ; k++ ;

elseif (arr[i] == C[j])


Q[k] = arr[i]
k++ ;

Q[k] = C[j]
i++ ; j++ ; k++ ;

else
Q[k] = C[j]
k++ ; j++;
endif
endwhile
while(i < m+n)
Q[k] = arr[i]
k++ ; i++ ;

endwhile
while (j < O)

Q[k] = C[j]
k++ ; j ++ ;
End while

(b.) Descending Order


m = length(A)
n = length(B)

arr[m+n] // array

i=k=0
j = n-1
Q[m+n+o]//answer

while ( i < m) && ( j >= 0 )

If( A[i] < B[j])


arr[k] = A[i]

i++ ; k++ ;
elseif (A[i] == B[j])
arr[k] = A[i]
k++ ;

arr[k] = B[j]
i++ ; j-- ; k++ ;

else

arr[k] = B[j]
k++ ; j-- ;

endif
endwhile

while(i < m)
arr[k] = A[i]
k++ ; i++ ;

endwhile
while (j >= 0)
arr[k] = B[j]
k++ ; j -- ;

I=0,j=0,k=m+n+o-1;

while ( i < m+n) && ( j<o )

If( arr[i] < C[j])

Q[k] = arr[i]
i++ ; k-- ;
elseif (arr[i] == C[j])

Q[k] = arr[i]
k--;

Q[k] = C[j]
i++ ; j++ ; k-- ;

else
Q[k] = C[j]
k-- ; j++;

endif
endwhile
while(i < m+n)
Q[k] = arr[i]
K-- ; i++ ;

endwhile

while (j < O)
Q[k] = C[j]
k-- ; j ++ ;

end while

THE END
------------o------------

You might also like