lecture 4 on data structure

12
Lecture 4 on Data Structure Array

Upload: rufus

Post on 07-Jan-2016

32 views

Category:

Documents


3 download

DESCRIPTION

Lecture 4 on Data Structure. Array. Searching : Linear search. Searching refers to the operation of finding the location LOC of ITEM in DATA, or printing some message that ITEM does not appear there. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Lecture 4 on  Data Structure

Lecture 4on

Data Structure

Array

Page 2: Lecture 4 on  Data Structure

Prepared by, Jesmin Akhter, Lecturer, IIT, JU

Searching : Linear searchSearching refers to the operation of finding the location LOC of ITEM in DATA, or printing some message that ITEM does not appear there.

DATA is a linear array with n elements. The most intuitive way to search for a given ITEM in DATA is to compare ITEM with each element of DATA one by one. That is first we test whether DATA[1 ]=ITEM, and then we test whether DATA[2 ]=ITEM, and so on. This method, which traverses DATA sequentially to locate ITEM, is called linear search or sequential search.

Algorithm 4.5 : A linear array DATA with N elements and a specific ITEM of information are given. This algorithm finds the location LOC of ITEM in the array DATA or sets LOC = 0.

1. Set DATA[N+1]:=ITEM.2. Set LOC:=1.3. Repeat while DATA[LOC]! =ITEM :

Set LOC := LOC +1.[End of loop]

4. If LOC = N+1, then :Write : ITEM is not in the array DATA.Else :Write : LOC is the location of ITEM. 5.Exit.

Page 3: Lecture 4 on  Data Structure

Prepared by, Jesmin Akhter, Lecturer, IIT, JU

Complexity of Linear search

• Measured by the number f(n) of comparisons required to find ITEM in the DATA array. Two important case:– Average case:

• Suppose pk is the probability that ITEM appears in DATA[k], and • q is the probability that ITEM does not appears in DATA.

– Then p1+ p2+ p3+ p4+ … pn+ q = 1 (Total probability)• Average number of comparisons can be calculated by-

– f(n) = 1. p1 + 2. p1+ 3. p1+ ……………….+ n . pn+ (n+1). q– Let, q is very small q0 and item appears in equal probability then pi= 1/n

– Worse case: when the search occurs through the entire array, DATA. i.e. When the ITEM does not appar in the array DATA

• It requires f(n)= n+1• In this case, the running time is proportional to n

2

11

2

)1(1)321(

0)1(11

21

1)(

n

n

nn

nn

nn

nnn

nf

Page 4: Lecture 4 on  Data Structure

Prepared by, Jesmin Akhter, Lecturer, IIT, JU

Binary Search Algorithm

BINARY(DATA, LB, UB, ITEM, LOC)

1. Set BEG=LB; END=UB; and MID=INT((BEG+END)/2).2. Repeat step 3 and 4 while BEG ≤ END and DATA[MID] ≠ ITEM3. 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)

[End of step 2 loop]5. If ITEM = DATA[MID] then

Set LOC= MID Else: Set LOC= NULL

[end of if structure]6. Exit.

Page 5: Lecture 4 on  Data Structure

Prepared by, Jesmin Akhter, Lecturer, IIT, JU

Binary Search example (Seek for 123)

Page 6: Lecture 4 on  Data Structure

Prepared by, Jesmin Akhter, Lecturer, IIT, JU

Binary Search - Complexity

• Often not interested in best case.• Worst case:

– Loop executes until BEG <= END– Size halved in each iteration– N, N/2, N/4, …N/2K…. 1– How many steps ?

Page 7: Lecture 4 on  Data Structure

Prepared by, Jesmin Akhter, Lecturer, IIT, JU

Binary Search - Complexity

• Worst case:– N/2K = 1 i.e. 2K = N

– Which gives K=log2N steps, which is O(log2(N))

– This is considered very fast when compared to linear

Page 8: Lecture 4 on  Data Structure

Prepared by, Jesmin Akhter, Lecturer, IIT, JU

Binary Search - Complexity

• Average case:– 1st iteration: 1 possible value– 2nd iteration: 2 possible values (left or right half)– 3rd iteration: 4 possible values (left of left, right of left, right of

right, right of left)– ith iteration: 2i-1 possible values

Page 9: Lecture 4 on  Data Structure

Prepared by, Jesmin Akhter, Lecturer, IIT, JU

Binary Search - Complexity

• Average Case:1 + 2 + 2 + 3 + 3 + 3 + 3 + … (upto log N steps)

1 element can be found with 1 comparison 2 elements 2 4 elements 3 Above Sum = sum over all possibilities

= i=0 to log N (i*2i-1) = O (log N)

Page 10: Lecture 4 on  Data Structure

Prepared by, Jesmin Akhter, Lecturer, IIT, JU

Multidimensional arrays

Two dimensional, three dimensional arrays and ….. Where elements are referenced respectively by two, three and ……subscripts.

Two – dimensional Arrays

A Two – dimensional Arrays m x n array A is a collection of m . n data elements such that each element is specified by a pair of integers (such as J, K), called subscripts.

The element of A with first subscript J and second subscript K will be denoted by A[J, K]

A[3, 1] A[3, 2] A[3, 3]

A[1, 1] A[1, 2] A[1, 3]

A[2, 2]A[2, 1] A[2, 3]

1

2

3

1 2 3

Rows

Columns

Fig: Two dimensional 3 x 3 array A

Page 11: Lecture 4 on  Data Structure

Prepared by, Jesmin Akhter, Lecturer, IIT, JU

Matrix MultiplicationAlgorithm 4.7: MATMUL(A, B, C, M, P, N)Let A be an MXP matrix array, and let B be a PXN matrix array. This algorithm stores the product of A and B in an MXN matrix array.

1. Repeat steps 2 to 4 for I =1 to M:2. Repeat steps 3 to 4 for J =1 to N:3. Set C[I, J]:=04. Repeat for K =1 to P:5. C[I, J]:= C[I, J]:+A[I, K]*B[K, J]6. Exit

See solved problem 4.12.

Page 12: Lecture 4 on  Data Structure

Prepared by, Jesmin Akhter, Lecturer, IIT, JU

Solved Problem

4.2, 4.3, 4.4, 4.5, 4.6, 4.7, 4.8, 4.9, 4.10,4.12

Supplementary problems:

4.25