2 - DS - TAM - Lecture - 2 (LS+BS+Buble S)
2 - DS - TAM - Lecture - 2 (LS+BS+Buble S)
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.
14-Oct-21 2
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.
Let, Array name is A then the elements of A is, by the bracket notation A[1], A[2],
A[3],…………., A[n]
The number k in A[k] is called a subscript and A[k] is called a subscripted variable.
14-Oct-21 3
Linear Arrays
Example :
A linear array DATA consisting of the name of six elements
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
14-Oct-21 4
Linear Arrays
Example :
An automobile company uses an array AUTO to record the number of
‘automobile’ sold each year from 1932 through 1984.
14-Oct-21 5
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
The computer does not need to keep track of the
1000
address of every element of LA, but needs to keep
1001
track only of the first element of LA, denoted by
1002
1003 Base(LA)
1004 Called the base address of LA. Using this address
1005 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
the array LA
STUDENT
STUDENT STUDENT
1 Dalia Rahaman
1 Dalia Rahaman Dalia Rahaman
Sumona 1
Sumona 2
2 Milon 2 Milon
Mubtasim Fuad 3
3 Mubtasim Fuad 3 Mubtasim Fuad
Anamul Haque 4
4 Anamul Haque 4 Anamul Haque
5
5 5
6
6 6
14-Oct-21 8
Insertion
INSERTING AN ELEMENT INTO AN ARRAY:
Insert (LA, N, K, ITEM)
Here LA is 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.
ALGORITHM :
Step 1. [Initialize counter] Set J:=N
Step 2. Repeat Steps 3 and 4] while J >= K
Step 3. [Move Jth element downward] Set LA[J+1] := LA[J]
Step 4. [Decrease counter] Set J := J-1
[End of step 2 loop]
Step 5 [Insert element] Set LA [K] := ITEM
Step 6. [Reset N] Set N := N+1
Step 7. Exit
14-Oct-21 9
Deletion
DELETING AN ELEMENT FROM A LINEAR ARRAY
Delete (LA, N, K, ITEM)
ALGORITHM
Step 1. Set ITEM: = LA [K]
Step 2. Repeat for J=K to N-1
[Move J+1st element upward] Set LA [J]: =LA [J+1]
[End of loop]
Step 3 [Reset the number N of elements in LA] Set N:=N-1
Step 4. Exit.
14-Oct-21 10
14-Oct-21 11
Sorting : Bubble sort
1 2 3 4 5 6
77 42 35 12 101 5
1 2 3 4 5 6
5 12 35 42 77 101
14-Oct-21 12
"Bubbling Up" the Largest Element
1 2 3 4 5 6
77 42 35 12 101 5
14-Oct-21 13
"Bubbling Up" the Largest Element
1 2 3 4 5 6
42Swap
77 77
42 35 12 101 5
14-Oct-21 14
"Bubbling Up" the Largest Element
1 2 3 4 5 6
42 35Swap35
77 77 12 101 5
14-Oct-21 15
"Bubbling Up" the Largest Element
1 2 3 4 5 6
42 35 12Swap12
77 77 101 5
14-Oct-21 16
"Bubbling Up" the Largest Element
1 2 3 4 5 6
42 35 12 77 101 5
No need to swap
14-Oct-21 17
"Bubbling Up" the Largest Element
1 2 3 4 5 6
42 35 12 77 5 Swap101
101 5
14-Oct-21 18
"Bubbling Up" the Largest Element
1 2 3 4 5 6
42 35 12 77 5 101
14-Oct-21 20
Items of Interest
1 2 3 4 5 6
42 35 12 77 5 101
• If we have N elements…
14-Oct-21 22
“Bubbling” All the 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
14-Oct-21 23
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
14-Oct-21 24
Summary
14-Oct-21 25
Searching : Linear search
Searching refers to the operation of finding the location LOC of ITEM in DATA, or printing
some message that ITEM does not appear there.
DATA is a linear array with n elements. The most intuitive way to search for a given ITEM in
DATA is to compare ITEM with each element of DATA one by one. That is first we test
whether DATA[1 ]=ITEM, and then we test whether DATA[2 ]=ITEM, and so on. This
method, which traverses DATA sequentially to locate ITEM, is called linear search
or sequential search.
14-Oct-21 28
Multidimensional arrays
Two dimensional, three dimensional arrays and ….. Where elements are
referenced respectively by two, three and ……subscripts.
Two – dimensional Arrays
A Two – dimensional Arrays m x 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.
The element of A with first subscript J and second subscript K will be denoted
by A[J, K] Columns
1 2 3
1 A[1, 1] A[1, 2] A[1, 3]
Rows 2
A[2, 1] A[2, 2] A[2, 3]
3 A[3, 1] A[3, 2] A[3, 3]
14-Oct-21 29
Matrix Multiplication
Algorithm 4.7: MATMUL(A, B, C, M, P, N)
Let A be an MXP matrix array, and let B be a PXN matrix array.
This algorithm stores the product of A and B in an MXN matrix
array.
14-Oct-21 30