11542_2-dimensional arrays and searching

Upload: rishabgulati

Post on 07-Apr-2018

217 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/3/2019 11542_2-Dimensional Arrays and Searching

    1/12

    1

    Two Dimensional arrays

    And searching

  • 8/3/2019 11542_2-Dimensional Arrays and Searching

    2/12

    2

    Two Dimensional Array:

    A two dimensional m X n array A is a collection ofm . N data elements such that each element isspecified 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-dimensionalm X n array A where the elements of A form arectangular array with m rows and n columns and

    where the element A[j,k] appears in row j andcolumn k.

    A row is a horizontal list and column is a vertical listof elements.

  • 8/3/2019 11542_2-Dimensional Arrays and Searching

    3/12

    3

    A[0,0] A[0,1]

    A[1,0] A[1,1]

    A[2,2]

    Suppose a is a 2-dimensional array. The firstdimension of A Contains the index set 1---m, withLB=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 calledthe size of the array

    Student Test1 Test2

    1 56 44

    2 55 78

    3 44 34

  • 8/3/2019 11542_2-Dimensional Arrays and Searching

    4/12

    4

    0,1

    2,1

    3,1

    1,2

    2,2

    3,2

    1,3

    2,3

    3,3

    Representation of 2-Dimensional Arrays

    in Memory 1,11,2

    1,3

    2,1

    2,2

    2,3

    3,1

    3,2

    3,3

    Column Major

    order

    Row Major

    order

  • 8/3/2019 11542_2-Dimensional Arrays and Searching

    5/12

    5

    Searching

    Searching refers to the operation of finding thelocation LOC of ITEM in DATA, or printing somemessage that item does not appear there. Thesearch is said to be successful if item does

    appear in DATA and unsuccessful otherwise

    Two techniques for searching the data.

    Linear Search

    Binary Search

  • 8/3/2019 11542_2-Dimensional Arrays and Searching

    6/12

  • 8/3/2019 11542_2-Dimensional Arrays and Searching

    7/127

    Linear Search: Algo(Linear Search)1) Set i=0

    2) Repeat steps 3 and 4 while i

  • 8/3/2019 11542_2-Dimensional Arrays and Searching

    8/128

    In the best case the item may occur atfirst position. In this case the searchoperation terminates in success with just

    one comparison. In worst case i.e either the item is

    present at last position or missing fromthe array the search terminates in afailure or either n comparisons.

  • 8/3/2019 11542_2-Dimensional Arrays and Searching

    9/129

    Binary Search

    Prerequisite for applying this technique

    is that the data should be in sorted orderi.e in increasing numerical order forexample telephone directory .

  • 8/3/2019 11542_2-Dimensional Arrays and Searching

    10/1210

    Binary Search: Algorithm(this algorithmfinds the location of item in data or setsloc=null)

    1) [Initialize segment variables]

    Set BEG:=LB,END=UB and mid=INT((BEG+END)/2)

    2) Repeat Steps 3 and 4 while BEG

  • 8/3/2019 11542_2-Dimensional Arrays and Searching

    11/12

    11

    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]=40Item

  • 8/3/2019 11542_2-Dimensional Arrays and Searching

    12/12