0% found this document useful (0 votes)
37 views12 pages

2-Dimensional Arrays and Searching

This document discusses two-dimensional arrays and searching algorithms. It defines two-dimensional arrays as collections of data elements specified by row and column indices. It also explains how two-dimensional arrays are represented in memory in row-major and column-major order. The document then discusses linear and binary search algorithms, noting that linear search is used for unsorted data while binary search requires sorted data and fewer comparisons on average.

Uploaded by

rishab_gulati
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views12 pages

2-Dimensional Arrays and Searching

This document discusses two-dimensional arrays and searching algorithms. It defines two-dimensional arrays as collections of data elements specified by row and column indices. It also explains how two-dimensional arrays are represented in memory in row-major and column-major order. The document then discusses linear and binary search algorithms, noting that linear search is used for unsorted data while binary search requires sorted data and fewer comparisons on average.

Uploaded by

rishab_gulati
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 12

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

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

A[0,1]

Student 1

Test1 56 55 44

Test2 44 78 34

A[1,1]

2 3

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 2,1 3,1 1,1 1,2

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

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

Column Major order

Row Major order


4

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) Set i=0

Algo(Linear Search)

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

3)

If(a[i]=item) then
Set loc=I 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 in a failure or either n comparisons.

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 .

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
10

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)
11

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.

12

You might also like