Professional Documents
Culture Documents
Student 1 2 3
Test1 56 55 44
Test2 44 78 34
Suppose a is a 2-dimensional array. The first dimension of A Contains the index set 1---m, with LB=1 and UB=m and second with LB=1 and UB=n. the length of a dimension is the number of integers in its index set. The pair of lengths m x n is called the size of the array
Here w is the number of words per memory cell for the array LA
Q1:Find the location for a[3,3] element from a 3x3 Matrix where the base address Starts from 1020 ,each element occupy 2 bytes and the matrix is stored in a Column MajorOrder and location for a[2,3]when element stored in rowmajor order. Q2: Consider the Linear arrays AAA(5:50), BBB(-5:10), CCC(18). Find the number of elements in each array and also find the address of AAA[15], BBB[11], CCC[17] where the starting address is 300 and words per memory cell is 4 bytes.
Searching
Searching refers to the operation of finding the location LOC of ITEM in DATA, or printing some message that item does not appear there. The search is said to be successful if item does appear in DATA and unsuccessful otherwise Two techniques for searching the data. Linear Search Binary Search
The algorithm one chooses generally depends on organization of the array elements. If the elements are in random order, then one must use linear search technique and if the array elements are sorted then it is preferable to use binary search technique
Linear Search:
1) 2) 3) Set i=0 If(a[i]=item) then Set loc=I
Algo(Linear Search)
Exit //element found at location I 4) Set i=i+1 [end loop] 5) Set loc=-1 //element not found 6) exit
In the best case the item may occur at first position. In this case the search operation terminates in success with just one comparison. In worst case i.e either the item is present at last position or missing from the array the search terminates ina failure eith n comparisons.
10
Binary Search Prerequisite for applying this technique is that the data should be in sorted order i.e in increasing numerical order for example telephone directory .
11
12
EXAMPLE: Data given 22,44,40,11,30,55,66,77,60,3 Search :30 3,11,22,30,40,44,55,60,66,77, Mid=INT((1+10)/2)=5 A[5]=40 Item<40 Set end=mid-1(5-1=4)
13
Limitation of the Binary Search: 1)The list must be sorted (Expensive) 2)One must have direct access to the middle element in any sub list.
14
15
In general, if N is the size of the list to be searched and C is the number of comparisons to do so in the worst case, C = log2N.
16