Lecture - 3 On Data Structures: Array
Lecture - 3 On Data Structures: Array
on
Data structures
Array
Array
Data structures are classified as either linear or nonlinear.
A data structure is said to be linear if its elements form a sequence or a linear
list.
There are two basic ways of representing such linear structures in memory.
One way is to have the linear relationship between the elements represented
by means of sequential memory locations. These linear structures are called
arrays.
The other way is to have the linear relationship between the elements
represented by means of pointers or links. These linear structures are called
linked lists.
Nonlinear structures are trees and graphs.
Linear Arrays
A linear array is a list of finite number n of homogeneous data elements such that :
a) The elements of the array are referenced respectively by an index set
consisting of n consecutive numbers.
b) The elements of the array are stored respectively in successive memory
locations.
The number n of elements is called the length or size of the array.
DATA
DATA[1] = 247
1 247
DATA[2] = 56
2 56
DATA[3] = 429
3 429
4 135
DATA[4] = 135
5 87 DATA[5] = 87
6 156 DATA[6] = 156
Linear Arrays
Example :
An automobile company uses an array AUTO to record the number of auto mobile
sold each year from 1932 through 1984.
AUTO[k] = Number of auto mobiles sold in the year K
LB = 1932
UB = 1984
Length = UB – LB+1 = 1984 – 1932+1 =53
Representation of linear array in memory
Let LA be a linear array in the memory of the computer. The memory of the
computer is a sequence of addressed locations.
LA
1000
1001 The computer does not need to keep track of the
1002 address of every element of LA, but needs to keep
1003 track only of the first element of LA, denoted by
1004 Base(LA)
1005
Called the base address of LA. Using this address
Base(LA), the computer calculates the address of
any element of LA by the following formula :
LOC(LA[k]) = Base(LA) + w(K – lower bound)
Where w is the number of words per memory cell for
Fig : Computer memory the array LA
Representation of linear array in memory
200
Example :
201
An automobile company uses an array AUTO to record
202 the number of auto mobile sold each year from 1932
AUTO[1932]
203 through 1984. Suppose AUTO appears in memory as
204 pictured in fig A . That is Base(AUTO) = 200, and w = 4
words per memory cell for AUTO. Then,
205 LOC(AUTO[1932]) = 200, LOC(AUTO[1933]) =204
206 AUTO[1933] LOC(AUTO[1934]) = 208
207 the address of the array element for the year K = 1965
208 can be obtained by using :
LOC(AUTO[1965]) = Base(AUTO) + w(1965 – lower
209
AUTO[1934] bound)
210 =200+4(1965-1932)=332
211
212
Fig : A
Traversing linear arrays
Print the contents of each element of DATA or Count the number of
elements of DATA with a given property. This can be accomplished by
traversing DATA, That is, by accessing and processing (visiting) each
element of DATA exactly once.
Algorithm 2.3: Given DATA is a linear array with lower bound LB and
upper bound UB . This algorithm traverses DATA applying an operation
PROCESS to each element of DATA.
1. Set K : = LB.
2. Repeat steps 3 and 4 while K<=UB:
3. Apply PROCESS to DATA[k]
4. Set K : = K+1.
5. Exit.
Traversing linear arrays
Example :
An automobile company uses an array AUTO to record the number of auto
mobile sold each year from 1932 through 1984.
a) Find the number NUM of years during which more than 300 automobiles
were sold.
b) Print each year and the number of automobiles sold in that year
1. Set NUM : = 0.
2. Repeat for K = 1932 to 1984: 1. Repeat for K = 1932 to 1984:
if AUTO[K]> 300, then : set NUM : = NUM+1 Write : K, AUTO[K]
3. Exit. 2. Exit.
Inserting and Deleting
Inserting refers to the operation of adding another element to the Array
Deleting refers to the operation of removing one element from the Array
Inserting an element somewhere in the middle of the array require that each
subsequent element be moved downward to new locations to accommodate the
new element and keep the order of the other elements.
Deleting an element somewhere in the middle of the array require that each
subsequent element be moved one location upward in order to “fill up” the
array. Fig shows E Inserted, B deleted.
STUDENT
STUDENT STUDENT
1 A
1 A A
B 1
B 2
2 E 2
E
C 3 C
3 D
C
3
4 D D
4 4
5
5 5
6
6 6
Insertion
1 2 3 4 5 6
77 42 35 12 101 5
1 2 3 4 5 6
5 12 35 42 77 101
"Bubbling Up" the Largest Element
1 2 3 4 5 6
77 42 35 12 101 5
"Bubbling Up" the Largest Element
1 2 3 4 5 6
42Swap77 12 101
77 42 35 5
"Bubbling Up" the Largest Element
1 2 3 4 5 6
42 35Swap35
77 77 12 101 5
"Bubbling Up" the Largest Element
1 2 3 4 5 6
42 35 12Swap12
77 77 101 5
"Bubbling Up" the Largest Element
1 2 3 4 5 6
42 35 12 77 101 5
No need to swap
"Bubbling Up" the Largest Element
1 2 3 4 5 6
42 35 12 77 5 Swap101
101 5
"Bubbling Up" the Largest Element
1 2 3 4 5 6
42 35 12 77 5 101
1 2 3 4 5 6
42 35 12 77 5 101
• If we have N elements…
1 2 3 4 5 6
42 35 12 77 5 101
1 2 3 4 5 6
35 12 42 5 77 101
1 2 3 4 5 6
N-1
12 35 5 42 77 101
1 2 3 4 5 6
12 5 35 42 77 101
1 2 3 4 5 6
5 12 35 42 77 101
Reducing the Number of Comparisons
1 2 3 4 5 6
77 42 35 12 101 5
1 2 3 4 5 6
42 35 12 77 5 101
1 2 3 4 5 6
35 12 42 5 77 101
1 2 3 4 5 6
12 35 5 42 77 101
1 2 3 4 5 6
12 5 35 42 77 101
Summary
INSERTION(A,N)
This algorithm sorts the array A with N elements
1 REPEAT STEPS 2 TO 4 FOR K=2,3 4…….N.
2 SET TEMP=A[K] AND PTR=K-1
3 REPEAT WHILE TEMP<A[PTR] && PTR>0
I) SET A[PTR+1]=A[PTR] [moves element forward]
II) SET PTR=PTR-1
[end of loop]
4 SET A[PTR+1]=TEMP [insert element in proper place]
[end of step 1 loop]
5 Exit