Lecture 3 Arrays Operations

Embed Size (px)

Citation preview

  • 8/11/2019 Lecture 3 Arrays Operations

    1/18

    Lecture 2

    Arrays

  • 8/11/2019 Lecture 3 Arrays Operations

    2/18

    Linear Data structures

    Linear form a sequence Linear relationship b/w the elements

    represented by means of sequentialmemory location

    Link list and arrays are linear relationship

    Non-Linear Structure are Trees and Graphs

  • 8/11/2019 Lecture 3 Arrays Operations

    3/18

    Operation performed by LinearStructure

    Traversal: Processing each element in the list Search : Find the location of the element with a

    given value or the record with a given

    key Insertion : Adding new element to the list Deletion : Removing an element from the list

    Sorting : Arranging the elements in some typeof order

    Merging : Combining two list into single list

  • 8/11/2019 Lecture 3 Arrays Operations

    4/18

    Linear Array

    List of finite number N of homogenousdata elements (i.e. data elements of sametype) such that The elements of the array are referenced

    respectively by an index set consisting of Nconsecutive number

    The elements of the array are storedrespectively in successive memory location

  • 8/11/2019 Lecture 3 Arrays Operations

    5/18

    Length of Array

    N = length of array

    Length = UB LB + 1

    UB = Upper Bound or Largest Index

    LB= Lower Bound or smallest Index

  • 8/11/2019 Lecture 3 Arrays Operations

    6/18

    Representation in Memory

    Address of any element in Array =LOC(LA[k])=Base (LA) + w (k - LB)

    LOC(LA[k]) =Address of element LA[k] of the Array LA

    Base (LA) = Base Address of LA

    w = No. of words per memory cell for the Array LA k = Any element of Array

  • 8/11/2019 Lecture 3 Arrays Operations

    7/18

    Multidimensional Arrays

    Two Dimensional Arrays Matrices in Mathematics Tables in Business Applications

    Matrix Arrays1 2 3

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

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

  • 8/11/2019 Lecture 3 Arrays Operations

    8/18

    Column and Row Major Order 2 Dimension

    Loc(A[J,K])= Base(A) + w [M(K-LB)+(J-LB)] (Column Major Order)

    Loc(A[J,K])= Base(A) + w [N(J-LB)+(K-LB)] (Row Major Order)

    1,1

    2,1

    3,1

    1,2

    2,2

    3,2

    1,32,3

    3,3

    1,4

    2,43,4

    1,1

    1,2

    1,3

    1,4

    2,1

    2,2

    2,32,4

    3,1

    3,2

    3,33,4

  • 8/11/2019 Lecture 3 Arrays Operations

    9/18

    Example: Row and Column Major Order, 2 Dimension Array.

    AAA (4:13,-7:10)

    How many elements can be stored in the array. Calculate the address of AAA(6,-6) by Row Major Order and Column Major Order Base address = 1500 and word size = 4

    Length of first dimension = UB LB + 1 => 13 4 + 1 = 10 (M) Length of second dimension = UB LB + 1 => 10 (-7) + 1 = 18 (N)

    Size = M * N => 10 * 18 = 180 elements can be stored in the array.

    Address of (6,-6) by Row Major Order: Loc(A[J,K]) = Base(A) + w [N(J-LB)+(K-LB)] Loc(6,-6) = 1500 + 4 [18(6-4)+(-6-(-7))] => 1500 + 4(37) => 1500 + 148 = 1648

    Address of (6,-6) by Column Major Order: Loc(A[J,K]) = Base(A) + w [M(K-LB)+(J-LB)] Loc(6,-6) = 1500 + 4 [10(-6-(-7))+(6-4)] => 1500 + 4(12) => 1500 + 48 = 1548

  • 8/11/2019 Lecture 3 Arrays Operations

    10/18

    Column and Row Major Order 3 Dimension

    Length = Upper Bound Lower Bound +1

    Effective Index = Index Number Lower Bound

    1,1,1

    2,1,1

    1,2,1

    2,2,1

    1,3,1

    2,3,1

    1,4,1

    2,4,1

    1,4,32,4,3

    1,1,1

    1,1,2

    1,1,3

    1,2,1

    1,2,2

    1,2,3

    1,3,1

    1,3,2

    2,4,22,4,3

  • 8/11/2019 Lecture 3 Arrays Operations

    11/18

    Column and Row Major Order 3 Dimension

    Length = Upper Bound Lower Bound +1

    Effective Index = Index Number Lower Bound

    Row Major Order:Loc = Base + w(E1L2+E2)L3+E3)

    Column Major Order:

    Loc = Base + w(E3L2+E2)L1+E1)

  • 8/11/2019 Lecture 3 Arrays Operations

    12/18

    Column and Row Major Order 3 DimensionRow Major Order:

    B ( 2 : 8 , - 4 : 1 , 6 : 10 )

    L1 = 8 2 + 1 = 7L2 = 1 - ( - 4 ) + 1 = 6L3 = 10 - 6 + 1 = 5

    E1 = 5 2 = 3E2 = - 1 - ( - 4 ) = 3E3 = 8 - 6 = 2

    E1 . L2 = 3 . 6 = 18E1 . L2 + E2 = 18 + 3 = 21

    ( E1 . L2 + E2 ) . L3 = 21 . 5 = 105(E1.L2 + E2).L3 + E3 = 105+2 =107

    Loc = Base + w(E1L2+E2)L3+E3)

    Loc [5,-1,8] = 200 + 4 (107) = 628

    Column Major Order:

    B ( 1 : 8 , - 5 : 5 , - 10 : 5 )

    L1 = 8 1 + 1 = 8L2 = 5 - ( - 5 ) + 1 = 11L3 = 5 - ( - 10 ) + 1 = 16

    E1 = 3 1 = 2E2 = 3 - ( - 5 ) = 8E3 = 3 - ( - 10 ) = 13

    E3 . L2 = 13 . 11 = 143E3 . L2 + E2 = 143 + 8 = 151

    ( E3 . L2 + E2 ) . L1 = 151 . 8 = 1208(E3.L2 + E2).L1 + E1 = 1208+2 =1210

    Loc = Base + w(E3L2+E2)L1+E1)

    Loc [3,3,3] = 400 + 4 (1210) = 5240

  • 8/11/2019 Lecture 3 Arrays Operations

    13/18

    Operations on Array

    Traversing a Linear Array

    TraverseArray (LA, LB, UB)Function: This algorithm traverse LA

    applying an operation PROCESS

    to each element of LAInput: LA is a Linear Array with Lower BoundLB and Upper bound UB

  • 8/11/2019 Lecture 3 Arrays Operations

    14/18

    Algorithm:1. [Initialize Counter] Set K:=LB2. Repeat Steps 3 and 4 while K UB 3. [Visit element] Apply PROCESS to

    LA[K]4. [Increase counter] Set K:=K+1

    [End of Step 2 loop]5. Exit

  • 8/11/2019 Lecture 3 Arrays Operations

    15/18

    Operations Cont.

    Insert an element in Linear ArrayInsertElement (LA, ITEM, N, K)Function: This algorithm insert an element in

    a Linear Array at required positionInput: LA is a Linear Array having N

    elementsITEM is the element to be inserted atgiven position K

    Precondition: K N where K is a +ve integer

  • 8/11/2019 Lecture 3 Arrays Operations

    16/18

    Algorithm:

    1. [Initialize Counter] Set J:=N2. Repeat Steps 3 and 4 while J K 3. [Move Jth element downward]

    Set LA[J+1] := LA[J]4. [Decrease counter] Set J:=J-1

    [End of Step 2 loop]

    5. [Insert element] Set LA[K]:=ITEM6. [Reset N] N:= N+17. Exit

  • 8/11/2019 Lecture 3 Arrays Operations

    17/18

    Operation Cont. Delete an element from a Linear Array

    DeleteElement (LA, ITEM, N, K)Function: This algorithm delete an element

    from a given position in Linear Array

    Input: LA is a Linear Array having N elementsK is the position given from which ITEM

    needs to be deletedOutput: ITEM is the element deleted fromthe given position K

    Precondition: K N where K is a +ve integer

  • 8/11/2019 Lecture 3 Arrays Operations

    18/18

    Algorithm:

    1. Set ITEM:=LA[K]2. Set J:= K3. Repeat Steps 4 and 5 while J