Linear Arrays: Memory Representation Traversal Insertion Deletion Linear Search Binary Search Merging 2D Array: Memory Representation
Linear Arrays: Memory Representation Traversal Insertion Deletion Linear Search Binary Search Merging 2D Array: Memory Representation
Traversal
Insertion
Deletion
Linear Search
Binary Search
Merging
2D Array : Memory representation
1
CONTENTS
2.1 Introductions
2.2 Linear Array
2.2.1 Linear Array Representations in Memory
2.2.2 Traversing Algorithm
2.2.3 Insert Algorithms
2.2.4 Delete Algorithms
2.2.5 Sequential and Binary Search Algorithm
2.2.6 Merging Algorithm
2.3 Multidimensional Array
2.3.1 2-D Array
2.3.2 Representations in Memory
2
2.1 Introduction
Data Structure can be classified as:
linear
non-linear
Linear (elements arranged in sequential in memory
location) i.e. array & linear link-list
Non-linear such as a tree and graph.
Operations:
Traversing, Searching, Inserting, Deleting, Sorting, Merging
Array is used to store a fix size for data and a link-list
the data can be varies in size.
3
2.1 Introduction
Advantages of an Array:
Very simple
Economy – if full use of memory
Random accessed at the same time
Disadvantage of an Array:
wasting memory if not fully used
4
2.2 Linear Array
Homogeneous data:
a) Elements are represented through indexes.
b) Elements are saved in sequential in memory locations.
Number of elements, N –> length or size of an array.
If:
UB : upper bound ( the largest index)
LB : lower bound (the smallest index)
Then: N = UB – LB + 1
Length = N = UB when LB = 1
5
2.2.1 Representation of Array in a Memory
The process to determine the address in a memory:
a) First address – base address.
b) Relative address to base address through index function.
6
2.2.1 Representation of Array in a Memory
In general, index function:
x[i]=M+w(i-L)
For real number: 4 byte, integer: 2 byte and character: 1 byte.
M- Base address of array
W- size of the data type of array
i- the index whose address is to find
L- Lower bound (Lower index) of array
Example:
x[3]= 150+2(3-0)
= 156
7
2.2.2 Traversing Algorithm
Traversing operation means visit every element once.
e.g. to print, etc.
Example algorithm:
1. [Assign counter]
set K=LB // LB = 0
2. Repeat step 2.1 and 2.2 while K <= UB // If LB = 0
2.1 [visit element]
do PROCESS on LA[K]
2.2 [add counter]
K=K+1
3. end repeat step 2
4. exit
8
2.2.3 Insertion Algorithm
size 1 2 3 4 5 k+1
MAX_LIST items
INSERT(LA, N, K, ITEM)
//LA is a linear array with N element
//K is integer positive where K <= N and LB = 0
//Insert an element, ITEM in index K
1. [Assign counter]
J = N; // LB = 0
2. Repeat step 3 and 4 while J >= K
3. Move J element downward
set LA[J+1] = LA[J]
4. Decrease the counter J = J – 1
5. Insert the element
set LA[k]= Item
6. [Reset N] N = N + 1
6. Exit
10
2.2.4 Deletion Algorithm
Delete item.
(a)
Array indexes
Delete 19
0 1 2 3 4 k-1 k MAX_LIST-1
12 3 44 100 … 5 10 18 ? … ?
k
1 2 3 4 5 k k+1 MAX_LIST
size
items
(b)
Array indexes
0 1 2 3 k-1 MAX_LIST-1
12 3 44 100 … 5 10 18 ? … ?
k
1 2 3 4 k MAX_LIST
size
items
13
2.2.5 Sequential Search
Compare successive elements of a given list with a search ITEM
until
1. either a match is encountered
2. or the list is exhausted without a match.
0 1 N-1
Algorithm:
SequentialSearch(LA, N, ITEM, LOC)
1. I = 0 // If LB = 0
2. Repeat step 2.1 while (i<N and LA[I] != ITEM )
2.1 I=I+1
3. If LA[I]==ITEM then
Return found at LOC=I
If not
Return not found 14
2.2.5 Binary Search Algorithm
Binary search algorithm is efficient if the array is sorted.
A binary search is used whenever the list starts to become large.
Consider to use binary searches whenever the list contains more
than 16 elements.
The binary search starts by testing the data in the element at the
middle of the array to determine if the target is in the first or
second half of the list.
If it is in the first half, we do not need to check the second half. If
it is in the second half, we do not need to test the first half. In other
words we eliminate half the list from further consideration. We
repeat this process until we find the target or determine that it is
not in the list.
15
2.2.5 Binary Search Algorithm
To find the middle of the list, we need three variables, one to
identify the beginning of the list, one to identify the middle
of the list, and one to identify the end of the list.
We analyze two cases here: the target is in the list (target
found) and the target is not in the list (target not found).
16
2.2.5 Binary Search Algorithm
Target found case: Assume we want to find 22 in a sorted
list as follows:
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9] a[10] a[11]
4 7 8 10 14 21 22 36 62 77 81 91
The three indexes are first, mid and last. Given first as 0 and
last as 11, mid is calculated as follows:
mid = (first + last) / 2
mid = (0 + 11) / 2 = 11 / 2 = 5
17
2.2.5 Binary Search Algorithm
At index location 5, the target is greater than the list value (22 > 21).
Therefore, eliminate the array locations 0 through 5 (mid is automatically
eliminated). To narrow our search, we assign mid + 1 to first and repeat
the search.
Target:
Target:22
22
first mid last
0 5 11
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9] a[10] a[11]
4 7 8 10 14 21 22 36 62 77 81 91
22 > 21
18
2.2.5 Binary Search Algorithm
The next loop calculates mid with the new value for first and
determines that the midpoint is now 8 as follows:
mid = (6 + 11) / 2 = 17 / 2 = 8
Target:
Target:22
22
first mid last
6 8 11
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9] a[10] a[11]
4 7 8 10 14 21 22 36 62 77 81 91
22 < 62
19
2.2.5 Binary Search Algorithm
When we test the target to the value at mid a second time, we discover that the
target is less than the list value (22 < 62). This time we adjust the end of the list
by setting last to mid – 1 and recalculate mid. This step effectively eliminates
elements 8 through 11 from consideration. We have now arrived at index location
6, whose value matches our target. This stops the search.
first mid last Target:
Target:22
22
6 6 7
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9] a[10] a[11]
4 7 8 10 14 21 22 36 62 77 81 91
first mid last
8 6 7 22 equals 22
function terminates 20
2.2.5 Binary Search Algorithm
Target not found case: This is done by testing for first and last crossing:
that is, we are done when first becomes greater than last. Two conditions
terminate the binary search algorithm when (a) the target is found or (b)
first becomes larger than last. Assume we want to find 11 in our binary
search array.
first mid last Target:
Target:11
11
0 5 11
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9] a[10] a[11]
4 7 8 10 14 21 22 36 62 77 81 91
11 < 21
21
2.2.5 Binary Search Algorithm
The loop continues to narrow the range as we saw in the
successful search until we are examining the data at index
locations 3 and 4.
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9] a[10] a[11]
4 7 8 10 14 21 22 36 62 77 81 91
11 > 8
22
2.2.5 Binary Search Algorithm
These settings of first and last set the mid index to 3 as follows:
mid = (3 + 4) / 2 = 7 / 2 = 3
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9] a[10] a[11]
4 7 8 10 14 21 22 36 62 77 81 91
11 > 10
23
2.2.5 Binary Search Algorithm
The test at index 3indicates that the target is greater than the list value, so we set first to mid
+ 1, or 4. We now test the data at location 4 and discover that 11 < 14. The mid is as
calculated as follows:
At this point, we have discovered that the target should be between two adjacent values; in
other words, it is not in the list. We see this algorithmically because last is set to mid – 1,
which makes first greater than last, the signal that the value we are looking for is not in the
list.
first mid last
4 4 4 Target:
Target:11
11
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9] a[10] a[11]
4 7 8 10 14 21 22 36 62 77 81 91
first mid last
11 < 14 4 4 3
Function terminates 24
2.2.5 Binary Search Algorithm
Example algorithm:
DATA – sorted array
ITEM – Info
LB – lower bound
UB – upper bound
ST – start Location
MID – middle Location
LAST – last Location
25
2.2.5 Binary Search Algorithm
1. [Define variables]
ST = LB, LAST= UB;
MID = (ST+LAST)/2;
2. Repeat 3 and 4 DO ST <= LAST & DATA[MID] != ITEM
3. If ITEM < DATA[MID] then
LAST = MID-1
If not
ST = MID+1
4. Set MID = INT((ST + LAST)/2)
[LAST repeat to 2]
5. If DATA[MID] == ITEM then
LOK = MID
If not
LOK = NULL
6. Stop
26
2.2.6 Merging Algorithm
Suppose A is a sorted list with r elements and B is a
sorted list with s elements. The operation that
combines the element of A and B into a single sorted
list C with n=r + s elements is called merging.
27
2.2.6 Merging Algorithm
Algorithm: Merging (A, R,B,S,C)
Here A and B be sorted arrays with R and S elements
respectively. This algorithm merges A and B into an array
C with N=R+ S elements
Step 1: Set NA=1, NB=1 and NC=1
Step 2: Repeat while NA ≤ R and NB ≤ S:
if A[NA] ≤ B[NB], then:
Set C[NC] = A[NA]
Set NA = NA +1
else
Set C[NC] = B[NB]
Set NB = NB +1
[End of if structure]
Set NC= NC +1
[End of Loop]
28
2.2.6 Merging Algorithm
Step 3: If NA >R, then:
Repeat while NB ≤ S:
Set C[NC] = B[NB]
Set NB = NB+1
Set NC = NC +1
[End of Loop]
else
Repeat while NA ≤ R:
Set C[NC] = A[NA]
Set NC = NC + 1
Set NA = NA +1
[End of loop]
[End of if structure]
Step 4: Return C[NC]
29
2.2.6 Merging Algorithm
Complexity of merging: The input consists of the
total number n=r+s elements in A and B. Each
comparison assigns an element to the array C, which
eventually has n elements. Accordingly, the number
f(n) of comparisons cannot exceed n:
f(n) ≤ n = O(n)
30
Exercises
Find where the indicated elements of an array
are stored, if the base address of a is 200* and
LB = 0, Given an array arr[1……10], [1…….15]
a) double a[10]; a[3]?
b) int a[26]; a[2]?
31
Thank You
34