Presentation Array Data Structure 1516908360 277498
Presentation Array Data Structure 1516908360 277498
AND ALGORITHM
AK
1
DATA STRUCTURE
VS
STORAGE STRUCTURE
2
CLASSIFICATION
• Data Structure
– Linear
– Non-Linear
3
REPRESENTATION IN
MEMORY
• Two basic representation in memory
– Have a linear relationship between the elements represented by means of
sequential memory locations [ Arrays]
4
OPERATION ON LINEAR
STRUCTURE
• Traversal : Processing each element in the list
• Search : Finding the location of the element with a given value or the
record with a given key
• Insertion: Adding a new element to the list
• Deletion: Removing an elements from the list
• Sorting : Arranging the elements in some type of order
• Merging : Combining two list into a single list
5
Array
6
LINEAR ARRAYS
7
LINEAR ARRAYS
8
LINEAR ARRAYS
9
REPRESENTATION OF LINEAR
ARRAY IN MEMORY
1000
1001
1002
1003
1004
1005
:
Computer Memory
10
REPRESENTATION OF LINEAR
ARRAY IN MEMORY
• Let LA be a linear array in the memory of the computer
• LOC(LA[K]) = address of the element LA[K] of the array LA
• The element of LA are stored in the successive memory cells
• Computer does not need to keep track of the address of every element
of LA, but need to track only the address of the first element of the
array denoted by Base(LA) called the base address of LA
11
REPRESENTATION OF LINEAR
ARRAY IN MEMORY
• LOC(LA[K]) = Base(LA) + w(K – lower bound) where w is the
number of words per memory cell of the array LA [w is aka size of the
data type]
12
EXAMPLE 1
200 LA[1]
Find the address for LA[6] 201 LA[2]
Each element of the array occupy 202
1 byte LA[3]
203
LA[4]
204
LA[5]
205 LA[6]
206 LA[7]
207
LA[8]
204
205 LA[3]
206
207
LA[4]
15
TRAVERSING LINEAR
ARRAYS
• Traversing is accessing and processing (aka visiting ) each element of
the data structure exactly ones
Linear Array
•••
16
TRAVERSING LINEAR
ARRAYS
• Traversing is accessing and processing (aka visiting ) each element of
the data structure exactly ones
Linear Array
•••
17
TRAVERSING LINEAR
ARRAYS
• Traversing is accessing and processing (aka visiting ) each element of
the data structure exactly ones
Linear Array
•••
18
TRAVERSING LINEAR
ARRAYS
• Traversing is accessing and processing (aka visiting ) each element of
the data structure exactly ones
Linear Array
•••
19
TRAVERSING LINEAR
ARRAYS
• Traversing is accessing and processing (aka visiting ) each element of
the data structure exactly ones
Linear Array
•••
20
TRAVERSING LINEAR
ARRAYS
• Traversing is accessing and processing (aka visiting ) each element of
the data structure exactly ones
Linear Array
•••
21
TRAVERSING LINEAR
ARRAYS
• Traversing is accessing and processing (aka visiting ) each element of
the data structure exactly ones
Linear Array
•••
1. Repeat for K = LB to UB
Apply PROCESS to LA[K]
[End of Loop]
2. Exit 22
INSERTING AND DELETING
• Insertion: Adding an element
– Beginning
– Middle
– End
23
INSERTION
1 Brown 1 Brown
2 Davis 2 Davis
3 Johnson 3 Johnson
4 Smith 4 Smith
5 Wagner 5 Wagner
6 6 Ford
7 7
8 8
24
INSERTION
1 Brown 1 Brown 1 Brown 1 Brown
2 Davis 2 Davis 2 Davis 2 Davis
3 Johnson 3 Johnson 3 Johnson 3 Ford
4 Smith 4 Smith 4 4 Johnson
5 Wagner 5 5 Smith 5 Smith
6 6 Wagner 6 Wagner 6 Wagner
7 7 7 7
8 8 8 8
2 Davis 2 Davis
3 Ford 3 Ford
4 Johnson 4 Johnson
5 Smith 5 Smith
6 Taylor 6 Taylor
7 Wagner 7
8 8
26
DELETION
1 Brown 1 Brown 1 Brown
1 Brown
2 Davis 2 2 Ford
2 Ford
3 Ford 3 Ford 3
3 Johnson
4 Johnson 4 Johnson 4 Johnson
4
5 Smith 5 Smith 5 Smith
5 Smith
6 Taylor 6 Taylor 6 Taylor
6 Taylor
7 Wagner 7 Wagner 7 Wagner
7 Wagner
8 8 8
8
27
DELETION
1 Brown
2 Ford
3 Johnson
4 Smith
5 Taylor
6 Wagner
28
INSERTION ALGORITHM
• INSERT (LA, N , K , ITEM) [LA is a linear array with N elements
and K is a positive integers such that K ≤ N. This algorithm
insert an element ITEM into the Kth position in LA ]
1. [Initialize Counter] Set J := N
2. Repeat Steps 3 and 4 while J ≥ K
3. [Move the Jth element downward ] Set LA[J + 1] :=
LA[J]
4. [Decrease Counter] Set J := J -1
5 [Insert Element] Set LA[K] := ITEM
6. [Reset N] Set N := N +1;
7. Exit
29
DELETION ALGORITHM
• DELETE (LA, N , K , ITEM) [LA is a linear array with N elements
and K is a positive integers such that K ≤ N. This algorithm
deletes Kth element from LA ]
1. Set ITEM := LA[K]
2. Repeat for J = K to N -1:
[Move the J + 1st element upward] Set LA[J] := LA[J +
1]
3. [Reset the number N of elements] Set N := N - 1;
4. Exit
30
MULTIDIMENSIONAL ARRAY
• One-Dimensional Array
• Two-Dimensional Array
• Three-Dimensional Array
• Some programming Language allows as many as 7 dimension
31
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 integer (such as J, K) called
subscript with property that
1 ≤ J ≤ m and 1 ≤ K ≤ n
The element of A with first subscript J and second subscript K will be denoted
by AJ,K or A[J][K]
32
2D ARRAYS
T H E E L E M E N T S O F A 2 - D I M E N S I O N A L A R R AY A I S S H O W N A S
BELOW
36
2D ARRAY IN MEMORY
A Subscript A Subscript
(1,1) (1,1)
(2,1) (1,2)
Column 1 Row 1
(3,1) (1,3)
(1,2) (1,4)
(2,2) (2,1)
Column 2
(3,2) (2,2)
(1,3) (2,3)
Row 2
(2,3) Column 3 (2,4)
(3,3) (3,1)
(1,4) (3,2)
Row 3
(2,4) Column 4 (3,3)
(3,4) (3,4)
• LOC(A[J,K]) of A[m,n]
Column-Major Order
LOC(A[J,K]) = Base(A) + w[m(K-1) + (J-1)]
Row-Major Order
LOC(A[J,K]) = Base(A) + w[n(J-1) + (K-1)]
38
2D ARRAY EXAMPLE
39
MULTIDIMENSIONAL ARRAY
40
MULTIDIMENSIONAL ARRAY
41
MULTIDIMENSIONAL ARRAY
• Address LOC(C[K1,K2, …., Kn]) of an arbitrary element of C can be obtained as
Column-Major Order
Base( C) + w[((( … (ENLN-1 + EN-1)LN-2) + ….. +E3)L2+E2)L1+E1]
Row-Major Order
Base( C) + w[(… ((E1L2 + E2)L3 + E3)L4 + ….. +EN-1)LN +EN]
42
EXAMPLE
43
EXAMPLE CONTD ..
44
POINTER, POINTER ARRAY
45
Group 1 Group 2 Group 3 Group 4
Penn Jones
Silver Reed
Troy
Wagner
King
46
1 Evan
2 Harris
3 Lewis Group 1
4 Shaw
5 Conrad
: : Group 2
13 Wagner
14 Davis
15 Segal Group 3
16 Baker
: : Group 4
21 Reed
47
1 Evan
: : Group 1
4 Shaw
5 $$$
6 Conrad Group are not index in
: : Group 2 this representation
14 Wagner
15 $$$
16 Davis Group 3
17 Segal
18 $$$
19 Baker
: : Group 4
24 Reed 48
1 Evan
2 Harris Group 1
Group 3 Lewis
1 1 4 Shaw
2 5
5 Conrad
3 14
4 16
: : Group 2
5 22 13 Wagner
14 Davis
15 Segal Group 3
16 Baker
: : Group 4
21 Reed
22 $$$
49