You are on page 1of 16

Two Dimensional arrays And searching

Two Dimensional Array:


A two dimensional m X n array A is a collection of m . N data elements such that each element is specified by a pair of integers. Two dimensional arrays are called matrices in mathematics and tables in business applications. There is a standard way of drawing 2-dimensional m X n array A where the elements of A form a rectangular array with m rows and n columns and where the element A[j,k] appears in row j and column k. A row is a horizontal list and column is a vertical list of elements.
2

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

Representation of 2-Dimensional Arrays in Memory 0,1


1,1 2,1 3,1 1,2 2,2 3,2 1,3 2,3 3,3 1,2 1,3 2,1 2,2 2,3 3,1 3,2 3,3

Column Major order

Row Major order


4

Column Major order:


Loc(A[j,k])= Base(A)+w([m(k-1)+(j-1)]) Loc(a[j,k]=base(A)+w([m(k)+j])

Row Major order:


Loc(A[j,k])= Base(A)+w([n(j-1)+(k-1)]) Loc(A[j,k]=base(A)+w([n(j)+k])

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)

Repeat steps 3 and 4 while i<=n-1

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

Binary Search: Algorithm(this algorithm


finds the location of item in data or sets loc=null)
1) [Initialize segment variables] Set BEG:=LB,END=UB and mid=INT((BEG+END)/2) 2) Repeat Steps 3 and 4 while BEG<=END and DATA[MID] != Item. 3) If item<DATA[MID] then Set End :=Mid-1 Else Set Beg:=Mid+1 [End of if structure] 4) Set MID:=INT((BEG+END)/2) 5)If DATA[MID] =Item then Set LOC:= MID Else Set LOC:=NULL 6) EXIT

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

You might also like