Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

E2008: Data Structure & Algorithms Tutorial 1 1. Write an algorithm that returns the sum of first m elements of an array S.

ver 1.1

2. Write an algorithm that outputs the smallest and largest values in the array S which has m unique elements. 3. Write an algorithm that returns the index of the first occurrence of the value key in the array s[1], , s[n]. If key is not in the array, the algorithm returns the value 0. 4. Write an algorithm that reverses the array s[1], , s[n]. 5. Given an array s[1], , s[n] such that n > 1 and s[i] s[i+1] for all i. Write an algorithm that insert an input value x into the array so that s[i] s[i+1] for all i. 6. The algorithm for finding the maximum element of an array is in the following Algorithm arrayMax(A, n) Input array A of n integers Output maximum element of A currentMax = A[0] for i = 1 to n 1 do if A[i] > currentMax then currentMax = A[i] return currentMax Determine the number of times that the statement currentMax = A[i] will be executed in the best case and in the worst case.

You might also like