0% found this document useful (0 votes)
150 views

Arrays Records and Pointers

Arrays, Records and Pointers discusses different data structures including linear and nonlinear data structures. Linear data structures include arrays and linked lists, while nonlinear structures include trees and graphs. The document describes linear arrays and how their elements are stored consecutively in memory locations based on a base address. Algorithms for traversing, inserting, deleting from, and sorting linear arrays are presented. Multidimensional arrays and how their elements are accessed using multiple subscripts are also covered. The document concludes with a discussion of pointers, pointer arrays, records, and record structures.

Uploaded by

towha elahi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
150 views

Arrays Records and Pointers

Arrays, Records and Pointers discusses different data structures including linear and nonlinear data structures. Linear data structures include arrays and linked lists, while nonlinear structures include trees and graphs. The document describes linear arrays and how their elements are stored consecutively in memory locations based on a base address. Algorithms for traversing, inserting, deleting from, and sorting linear arrays are presented. Multidimensional arrays and how their elements are accessed using multiple subscripts are also covered. The document concludes with a discussion of pointers, pointer arrays, records, and record structures.

Uploaded by

towha elahi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Arrays, Records and Pointers

Arrays, Records and Pointers


• Data structures are classified as either linear
or nonlinear.
• Normally arrays and linked lists fall into linear
data structures.
• Trees and graphs are treated as nonlinear
structures.
Linear Arrays
• A linear array is a list of a finite number n
of homogeneous data elements.
• The length of data elements of the array
can be obtained by the following
formula:
Length = UB – LB + 1
UB is upper bound (Largest Index) and LB is
the lower bound (Smallest Index).
Representation of a Linear Array in
Memory
Representation of a Linear Array in
Memory
• Elements of linear array are stored in consecutive memory
locations.
• Computer does not keep track of address of each element of array.
• Only keeps track of the base address of the array and on the basis
of this base address the address or location of any element can be
found by using following formula:
LOC (LA [K]) = Base (LA) + w (K – LB)
Here,
– LOC (LA [K]) is the location of the Kth element of LA.
– Base (LA) is the base address of LA.
– w is the number of bytes taken by one element.
– K is the Kth element.
– LB is the lower bound.
Representation of a Linear Array in
Memory (Example)
• Suppose we want to find out LOC (A [3]).
• For it, we have:
Base (A) = 1000
w = 2 bytes (Because an integer takes two bytes in the memory).
K=3
LB = 1

• After putting these values in the given formula, we get:


LOC (A [3]) = 1000 + 2 (3 – 1)
= 1000 + 2 (2)
= 1000 + 4
= 1004
Algorithm 4.1(Traversing a Linear
Array)
Here, LA is a linear array with lower bound LB
and upper bond UB. This algorithm traverses LA
applying an operation PROCESS to each
element of LA.
1. [Initialize counter] Set K := LB
2. Repeat steps 3 and 4 while K ≤ UB
3. [Visit element] Apply PROCESS to LA[K]
4. [Increase counter] Set K := K + 1
5. [End of Step 2 loop]
Algorithm 4.2(Inserting into a Linear Array)
INSERT(LA, N, K, ITEM)
//Here LA is a linear array with N elements and K is a positive
integer such that K ≤ N. This algorithm inserts an element ITEM
into the Kth position in LA.
1. [Initialize counter] Set J := N
2. Repeat steps 3 and 4 while J ≥ K
[Move Jth element downward]
3. Set LA[J+1] := LA[J]
4.[Decrease counter] Set J := J – 1
[End of Step 2 loop]
5. [Insert element] Set LA[K] := ITEM
6. [Reset N] Set N := N + 1
7. Exit
Algorithm 4.3(Deleting from a Linear Array)
• DELETE(LA, N, K, ITEM)
//Here LA is a linear array with N elements and K is a
positive integer such that K ≤ N. this algorithm
deletes the Kth element from LA.
1. Set ITEM := LA[K]
2. Repeat steps for J = K to N - 1
[Move J + 1st element upward]
Set LA[J] := LA[J + 1]
[End of Step 2 loop]
3. [Reset the number N of elements in LA]
Set N := N - 1
4. Exit
Sorting; Bubble Sort
• Let A be a list of n numbers. Sorting A refers to
the operation of rearranging the elements of
A so they are in increasing order, i.e., so that
• A[1] < A[2]<………A[N]
How Bubble sort works?
• Traverse a collection of elements
– Move from the front to the end
– “Bubble” the largest value to the end using pair-
wise comparisons and swapping
Algorithm 4.4(Bubble Sort)
BUBBLE (DATA, N)
1. Repeat steps 2 and 3 for K = 1 to N – 1
2. Set PTR := 1
3. Repeat while PTR ≤ N – K
(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 outer loop]
4. Exit
Complexity of Bubble Sort Algorithm
• 𝑓 𝑛 = 𝑛 − 1 + 𝑛 − 2 + ⋯…..2 + 1 =
𝑛(𝑛−1)
= O(n2)
2
Searching

•Linear Search
•Binary Search
Multidimensional Arrays
• The arrays whose elements are accessed by more
than one subscript are termed as
multidimensional arrays.
• Two-Dimensional array
– 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≤m
– The element of A with first subscript J and second
subscript K will be denoted by A[J, K]
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.
• The programming language stores the array A
either as column by column, or what is called
column-major order, Or row by row, in row
major order.
Representation of Two-Dimensional
Arrays in Memory
Representation of Two-Dimensional
Arrays in Memory
• Here, w is the number of words per memory
cell for the array LA, and 1 is the lower bound
of the index set of LA. M is number of rows in
array and N is number of columns in array.
• For Column-major order
LOC(A[J, K]) = Base(A) + w[M(K-1) + (J-1)]
• For Row-major order
LOC(A[J, K]) = Base(A) + w[N(J-1) + (K-1)]
Example
• FIND OUT THE LOCATION OF A[3][2] of a 3 X 4
INTERGER MATRIX WITH BASE ADDRESS 1000
Base (A) : 1000
w : 2 (because an integer takes 2 bytes in memory)
N:4
J:3
K:2
Now put these values in the given formula as below:
LOC (A [3, 2]) = 1000 + 2 [4 (3-1) + (2-1)]
= 1000 + 2 [4 (2) + 1]
= 1000 + 2 [8 + 1]
= 1000 + 2 [9]
= 1000 + 18 = 1018
Algorithm 4.7 (Matrix Multiplication)
MATMUL(A, B, C, M, P, N)
Let A be an M × P matrix array, and let B be a P × N matrix array.
This algorithm stores the product of A and B in an M × N matrix
array C.
1. Repeat steps 2 to 4 for I = 1 to M:
2. Repeat steps 3 and 4 for J = 1 to N:
3. Set C[I, J] = 0
4. Repeat for K = 1 to P:
C[I, J] = C[I, J] + A[I, K] * B[K, J]
[End of inner loop]
[End of step 2 middle loop]
[End of step 1 outer loop]
5. Exit
MATMUL(A, B, C, M, P, N)

• 1. Repeat steps 2 to 4 for I = 1 to M:


• 2. Repeat steps 3 and 4 for J = 1 to N:
• 3. Set C[I, J] = 0
• 4. Repeat for K = 1 to P:
• C[I, J] = C[I, J] + A[I, K] * B[K, J]
• [End of inner loop]
• [End of step 2 middle loop]
• [End of step 1 outer loop]
• 5. Exit
Pointers and Pointer Arrays
• Let DATA be an array.
• A variable P is called a pointer if P “points” to
an element in DATA, i.e. if P contains the
address of an element in DATA.
• An array PTR is called a pointer array if each
element of PTR is a pointer.
• Pointers and pointer arrays used to facilitate
the processing of information in DATA.
Example
Example
Records, Record Structure
• Collection of data are frequently organized into a hierarchy
of fields, records and files.
• A record is a collection of related data items, each of which
is called a field or attributes, and a file is collection of
similar records. Each data item itself may be a group item
composed of sub items, those items which are
indecomposable are called elementary item or scalars. The
names given to various data items are called identifier.
• Although a record is a collection of data item, it is differ
from linear array in the following ways:
– A record may be a collection of nonhomogeneous data.
– The data item in a record are indexed by attribute names, so
there may not be a natural ordering of its elements.
Example
Thank
You

You might also like