1. traversing a linear array here a is a linear array with lower bound lb and upper bound ub. this...

30
1

Upload: derrick-simpson

Post on 01-Jan-2016

227 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: 1. Traversing a linear array Here A is a linear array with lower bound LB and upper bound UB. This algorithm traverses A applying an operation PROCESS

1

Page 2: 1. Traversing a linear array Here A is a linear array with lower bound LB and upper bound UB. This algorithm traverses A applying an operation PROCESS

Traversing a linear arrayHere A is a linear array with lower bound LB and

upper bound UB. This algorithm traverses A applying an operation PROCESS to each element of A.

1.[Initilize counter] Set K = LB.2.Repeat steps 3 and 4 while K ≤ UB.3. [Visit element] Apply PROCESS to A[K].4. [Increase counter] Set K = K + 1. [End of step 2 loop]5.Exit

2

Page 3: 1. Traversing a linear array Here A is a linear array with lower bound LB and upper bound UB. This algorithm traverses A applying an operation PROCESS

Inserting into a linear arrayINSERT (A, N, K, ITEM)Here A is a linear array with N elements and K is a

positiveinteger such that K ≤ N. This algorithm inserts an

elementITEM into the Kth position in A.1.[Initialize counter] Set J = N.2.Repeat steps 3 and 4 while J ≥ K.3. [Move Jth element downward] Set A[J + 1 ] = A[J]4. [Decrease counter] Set J = J – 1[End of step 2 loop]5.[Insert element] Set A[K] == ITEM6.[Reset N] Set N = N + 17.Exit

3

Page 4: 1. Traversing a linear array Here A is a linear array with lower bound LB and upper bound UB. This algorithm traverses A applying an operation PROCESS

Deleting from a linear arrayDELETE (A, N, K, ITEM)Here A is a linear array with N elements and K is a

positiveinteger such that K ≤ N. This algorithm deletes the Kth element from A.1.Set ITEM = A[K]2.Repeat fro J = K to N – 1 [Move J + 1st element upward] Set A[J] = A[J +

1] [End of loop]3.[Reset the number N of elements in A] Set N = N – 14.Exit

4

Page 5: 1. Traversing a linear array Here A is a linear array with lower bound LB and upper bound UB. This algorithm traverses A applying an operation PROCESS

Bubble SortBUBBLE(DATA, N)Here DATA is an array with N elements. This algorithm

sorts the elements in DATA.1.Repeat steps 2 and 3 for K = 1 to N – 12.Set PTR = 1 [Initialize pass pointer PTR]3.Repeat while PTR ≤ N – K [Executes pass]

a) If DATA[PTR] > DATA[PTR + 1], then Interchange DATA[PTR] and DATA[PTR + 1] [End of If structure]b) Set PTR = PTR + 1 [End of inner loop][End of step 1 outer loop]

4.Exit

5

Page 6: 1. Traversing a linear array Here A is a linear array with lower bound LB and upper bound UB. This algorithm traverses A applying an operation PROCESS

Linear SearchLINEAR(DATA, N, ITEM, LOC)Here DATA is a linear array with N elements, and ITEM is a given item of information. This algorithm finds the location LOC of ITEM in DATA, or sets LOC = 0 if the search is unsuccessful.1.[Insert ITEM at the end of DATA] Set DATA[N+1] =

ITEM2.[Initialize counter] Set LOC = 13.[Search for ITEM]

Repeat while DATA[LOC] ≠ ITEMSet LOC = LOC + 1

[End of loop]4.[Successful?] If LOC = N+1, then: Set Loc = 05.Exit

6

Page 7: 1. Traversing a linear array Here A is a linear array with lower bound LB and upper bound UB. This algorithm traverses A applying an operation PROCESS

Binary SearchBINARY(DATA, LB, UB, ITEM, LOC)Here DATA is a sorted array with lower bound LB and upper bound UB, and ITEM is a given item of information. The variables BEG, END and MID denote, respectively, the beginning, end and middle locations of a segment of elements of DATA. This algorithm finds the location LOC 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] ≠ ITEM3. If ITEM < DATA[MID], then:

Set END = MID – 1 Else

Set BEG = MID + 1 [End of If structure]

7

Page 8: 1. Traversing a linear array Here A is a linear array with lower bound LB and upper bound UB. This algorithm traverses A applying an operation PROCESS

Binary Search (Contd.)4. Set MID = INT((BEG + END)/2)

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

Set LOC = MIDElse

Set LOC = NULL[End of If structure]

6. Exit

8

Page 9: 1. Traversing a linear array Here A is a linear array with lower bound LB and upper bound UB. This algorithm traverses A applying an operation PROCESS

Multidimensional ArraysMost programming languages allow two-

dimensional and three-dimensional arrays, i.e., arrays where elements are referenced, respectively, by two and three subscripts. In fact, some programming languages allow the number of dimensions for an array to be as high as 7.

A two dimensional m×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, with the property that

1 ≤ J ≤ m and 1 ≤ K ≤ n

9

Page 10: 1. Traversing a linear array Here A is a linear array with lower bound LB and upper bound UB. This algorithm traverses A applying an operation PROCESS

Multidimensional Arrays (Contd.)The element of A with first subscript j and second

subscript k will be denoted byAJ,K or A[J,K]Two dimensional arrays are called matrices in

mathematics and tables in business applications; hence two dimensional arrays are sometimes called matrix arrays.

10

Page 11: 1. Traversing a linear array Here A is a linear array with lower bound LB and upper bound UB. This algorithm traverses A applying an operation PROCESS

Multidimensional Arrays (Contd.)There is a standard way of drawing a two-

dimensional m×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.

1 2 3 4

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

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

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

Columns

Rows

Two-dimensional 3×4 array A

11

Page 12: 1. Traversing a linear array Here A is a linear array with lower bound LB and upper bound UB. This algorithm traverses A applying an operation PROCESS

Multidimensional Arrays (Specification)Suppose A is a two-dimensional m×n array. The

first dimension of A contains the index set 1, …….., m, with lower bound 1 and upper bound m; and the second dimension of A contains the index set 1, 2, …….., n, with lower bound 1 and upper bound n. the length of a dimension is the number of integers in its index set. The pair of lengths m×n (read “m by n”) is called the size of the array.

12

Page 13: 1. Traversing a linear array Here A is a linear array with lower bound LB and upper bound UB. This algorithm traverses A applying an operation PROCESS

Multidimensional Arrays (Length)The length of a given dimension (i.e., the number

of integers in its index set) can be obtained from the formula

Length = upper bound – lower bound + 1

13

Page 14: 1. Traversing a linear array Here A is a linear array with lower bound LB and upper bound UB. This algorithm traverses A applying an operation PROCESS

Representation of Two-Dimensional Arrays in memory

Let A be a two-dimensional m×n array. Although A is pictured as a rectangular array of elements with m rows and n columns, the array will be represented in memory by a block of m.n sequential memory locations. Specifically, the programming language will store the array A either

Column by column, what is called column major order, or

Row by row, in row major order.The figure shows these two ways when A is a two-

dimensional 3×4 array. We emphasize that the particular representation used depends upon the programming language, not the user.

14

Page 15: 1. Traversing a linear array Here A is a linear array with lower bound LB and upper bound UB. This algorithm traverses A applying an operation PROCESS

Representation of Two-Dimensional Arrays in memorySubscript

(1, 1)

(2, 1) Col 1

(3, 1)

(1, 2)

(2, 2) Col 2

(3, 2)

(1, 3)

(2, 3) Col 3

(3, 3)

(1, 4)

(2, 4) Col 4

(3, 4)

A Subscript

(1, 1)

(1, 2) Row 1

(1, 3)

(1, 4)

(2, 2)

(2, 2) Row 2

(2, 3)

(2, 4)

(3, 1)

(3, 2) Row 3

(3, 2)

(3, 4)

A

a) Column major order b) Row major order

A A

15

Page 16: 1. Traversing a linear array Here A is a linear array with lower bound LB and upper bound UB. This algorithm traverses A applying an operation PROCESS

Drawbacks of ArrayThe address of an element in an array can be

easily computed because it occupies successive memory locations.

It is relatively expensive to insert and delete elements in an array.

Since an array usually occupies a block of memory space, one cannot simply double or triple the size of an array when additional space is required.

For this reason, arrays are called dense lists and are said to be static data structures.

16

Page 17: 1. Traversing a linear array Here A is a linear array with lower bound LB and upper bound UB. This algorithm traverses A applying an operation PROCESS

Linked ListsAnother way of storing a list in memory is to have

each element in the list contain a field, called a link or pointer, which contains the address of the next element in the list.

Thus successive elements in the list need not occupy adjacent space in memory. This will make it easier to insert and delete elements in the list.

This type of data structure is called linked list.

17

Page 18: 1. Traversing a linear array Here A is a linear array with lower bound LB and upper bound UB. This algorithm traverses A applying an operation PROCESS

Linked Lists (Contd.)A linked list, or one-way list, is a linear collection

of data elements, called nodes, where the linear order is given by means of pointers.

That is, each node is divided into two parts; the first part contains the information of the element, and the second part, called the link field or nestpointer field, contains the address of the nest node in the list.

In particular, a linked list implementation associates with each list element Si a pointer LINKi to indicate the address at which Si+1 is stored.

18

Page 19: 1. Traversing a linear array Here A is a linear array with lower bound LB and upper bound UB. This algorithm traverses A applying an operation PROCESS

Linked Lists (Contd.)• Figure on slide 20 is a schematic diagram of a linked list with 6

nodes. Each node is pictured with two parts. • The left part represents the information part of the node, which

may contain an entire record of data items (e.g., NAME, ADDRESS, …..).

• The right part represents the nextpointer field of the node, and there is an arrow drawn from it to the next node in the list.

• The pointer of the last node contains a special value, called the null pointer, which is any invalid address, denoted by × which signals the end of the list.

• The linked list also contains a list pointer variable – called START or NAME – which contains the address of the first node in the list; hence there is an arrow drawn from START to the first node.

• A special case is the list that has no nodes. Such a list is called the null list or empty list and is denoted by the null pointer in the variable START.

19

Page 20: 1. Traversing a linear array Here A is a linear array with lower bound LB and upper bound UB. This algorithm traverses A applying an operation PROCESS

Linked Lists (Contd.)

20

Page 21: 1. Traversing a linear array Here A is a linear array with lower bound LB and upper bound UB. This algorithm traverses A applying an operation PROCESS

Linked Lists (Example)

21

Bed # Patient

1 Kirk

2

3 Dean

4 Maxwell

5 Adams

6

7 Lane

8 Green

9 Samuels

10

11 Fields

12 Nelson

Next

7

11

12

3

4

1

0

8

9

5

START

Page 22: 1. Traversing a linear array Here A is a linear array with lower bound LB and upper bound UB. This algorithm traverses A applying an operation PROCESS

Representation of Linked Lists in MemoryLet LIST be a linked list. Then LIST will be maintained in

memory, unless otherwise specified or implied, as follows.First of all, LIST requires two linear arrays – we will call

them here INFO and LINK – such that INFO[K] and LINK[K] contain, respectively, the information part and the nextpointer field of a node of LIST.

LIST also requires a variable name – such as START – which contains the location of the beginning of the list, and a nextpointer sentinel – denoted by NULL – which indicates the end of the list.

The nodes of a list need not occupy adjacent elements in the arrays INFO and LINK, and that more than one list may be maintained in the same linear arrays INOF and LINK. However, each list must have its own pointer variable giving the location of its first node.

22

Page 23: 1. Traversing a linear array Here A is a linear array with lower bound LB and upper bound UB. This algorithm traverses A applying an operation PROCESS

Linked Lists (Examples)

23

O

T

X

N

I

E

6

0

11

10

3

4

7

9

START START LINK

1

2

3

4

5

6

7

8

9

10

11

12

Page 24: 1. Traversing a linear array Here A is a linear array with lower bound LB and upper bound UB. This algorithm traverses A applying an operation PROCESS

Linked Lists (Examples)

24

74

82

84

78

74

100

88

62

74

93

14

0

12

0

8

13

2

7

6

4

11ALG

TEST LINK

1

2

3

4

5

6

7

8

9

10

11

12

13

14

5

GEOM

Page 25: 1. Traversing a linear array Here A is a linear array with lower bound LB and upper bound UB. This algorithm traverses A applying an operation PROCESS

Traversing a Linked ListLet LIST be a linked list in memory. This algorithm

traverses LIST, applying an operation PROCESS to each element of LIST. The variable PTR points to the node currently

being processed.1.Set PTR = START [Initializes pointer PTR]2.Repeat steps 3 and 4 while PTR ≠ NULL3. Apply PROCESS to INFO[PTR]4. Set PTR = LINK[PTR] [ PTR now points to the nest node]

[End of step 2 loop]5.Exit

25

Page 26: 1. Traversing a linear array Here A is a linear array with lower bound LB and upper bound UB. This algorithm traverses A applying an operation PROCESS

Searching a linked list (List is unsorted)SEARCH(INFO, LINK, START, ITEM, LOC)

LIST is a linked list in memory. This algorithm finds the location LOC of

the node where ITEM first appears in LIST, or sets LOC = NULL.

1. Set PTR = START

2. Repeat step 3 while PTR ≠ NULL

3. If ITEM = INFO[PTR], then

Set LOC = PTR and Exit

Else

Set PTR = LINK[PTR] [ PTR now points to the

next node]

4. [Search is unsuccessful] Set LOC = NULL

5. Exit

26

Page 27: 1. Traversing a linear array Here A is a linear array with lower bound LB and upper bound UB. This algorithm traverses A applying an operation PROCESS

Searching a linked list (List is sorted)SRCHSL(INFO, LINK, START, ITEM, LOC)LIST is a sorted list in memory. This algorithm finds the location

LOC of the node where ITEM first appears in LIST, or sets LOC = NULL.

1. Set PTR =START2. Repeat step 3 while PTR ≠ NULL3. If ITEM > INFO[PTR], then

Set PTR = LINK[PTR] [PTR now points to the next node] Else

Set LOC = NULL and Exit [ITEM now exceeds INFO[PTR]]

[End of If structure] [End of step 2 loop]4. Set LOC = NULL5. Exit

27

Page 28: 1. Traversing a linear array Here A is a linear array with lower bound LB and upper bound UB. This algorithm traverses A applying an operation PROCESS

28

Page 29: 1. Traversing a linear array Here A is a linear array with lower bound LB and upper bound UB. This algorithm traverses A applying an operation PROCESS

29

Page 30: 1. Traversing a linear array Here A is a linear array with lower bound LB and upper bound UB. This algorithm traverses A applying an operation PROCESS

30