Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 23

LECTURE WEEK 2 Arrays & searching

WHAT WE WILL LEARN


Arrays
Operation on arrays
Searching
Linear search
Binary search
ARRAYS
An array is a collection of elements of the same type placed in contiguous memory
locations that can be individually referenced by using an index to a unique identifier.
Declaration + Initialization:
REPRESENTATION OF LINEAR
ARRAYS IN MEMORY
Let A be a linear array

The elements of A are stored in successive memory cells. So the computer does not need to keep track of the
address of every element of A, but needs to keep track only of the address of the first element of A denoted
by:
Base(A)

Using this address the computer can calculate the address of any element of A by the following formula:
LOC(LA[k])=Base(A)+w(K-LB)
where:
K is the array index
W is the width of the record
TASKS
* Taking input in an array
* Sum of array elements
* Average of array elements
* Even and odd
MULTI-DIMENSIONAL ARRAYS
TASKS
Finding the sum of 2 matrix
Finding the transpose
INSERTING AND DELETING
Let A be collection of data elements stored in the memory of the computer.

Inserting refers to the operation of adding another element to the collection A.

Deleting refers to the operation of removing one of the element from A.

Inserting an element at the end of a linear array can be easily done provided the memory
space allocated for the array is large enough.
INSERTING ELEMENTS
INSERTION IN AN ARRAY
Let LA is a Linear Array unordered with N elements and K is a positive integer such
that K<=N. Below is the algorithm where ITEM is inserted into the Kth position of
LA .
1.Start
2. Set J=N
3. Set N = N+1
4. Repeat steps 5 and 6 while
J >= K
5. Set LA[J+1] = LA[J]
6. Set J = J-1
7. Set LA[K] = ITEM
8. Stop
EXAMPLE-DRY RUN
0 We have to insert
10 30 at position 2
1 50

25 30
2

3 60

4 15

5
EXAMPLE-DRY RUN
J K
1.Start
10 4 2 2. Set J=N
50
3. Set N = N+1
4. Repeat steps 5 and 6
25
while J >= K
5. Set LA[J+1] = LA[J]
LA[4+1]=LA[4] 6. Set J = J-1
60 LA[5]=LA[4] 7. Set LA[K] = ITEM
LA[5]=15 8. Stop
15

15
EXAMPLE-DRY RUN
J K 1.Start
0
10 2. Set J=N
4 2
3. Set N = N+1
1 50 3 2
4. Repeat steps 5 and 6
while J >= K
25
2 5. Set LA[J+1] = LA[J]
LA[3+1]=LA[3] 6. Set J = J-1
3
LA[4]=LA[3] 7. Set LA[K] = ITEM
60 LA[4]=60 8. Stop
4

5
15
EXAMPLE-DRY RUN
J K 1.Start
0
10 2. Set J=N
4 2
3. Set N = N+1
1 50 3 2
4. Repeat steps 5 and 6
2 2 while J >= K
2 5. Set LA[J+1] = LA[J]
25 LA[2+1]=LA[2] 6. Set J = J-1
3 60
LA[3]=LA[2] 7. Set LA[K] = ITEM
LA[3]=25 8. Stop
4 60
15

5
15
EXAMPLE-DRY RUN
J K 1.Start
0
10 4 2 2. Set J=N
3. Set N = N+1
1 50
3 2
4. Repeat steps 5 and 6
2 2
while J >= K
30 1(loop
2 5. Set LA[J+1] = LA[J]
terminate
25 s) 6. Set J = J-1
3 60
7. Set LA[K] = ITEM
60 8. Stop
4 15
LA[K]=ITEM
LA[2]=30
5
15
DELETION IN AN ARRAY
Consider LA is a linear array with N elements and K is a positive integer such that K<=N.
Below is the algorithm to delete an element available at the K th position of LA.

1. Start
2. Set J=K
3. Repeat steps 4 and 5 while J < N
4. Set LA[J] = LA[J+1]
5. Set J = J+1
6. Set N = N-1
7. Stop
EXAMPLE-DRY RUN
1.Start
J K 2. Set J=K
0
10 2 2
3. Repeat steps 4 and 5 while J < N
1 50
4. Set LA[J] = LA[J+1]
30 5. Set J = J+1
2
6. Set N = N-1
3 25 LA[2]=LA[3]
7. Stop
4 60

5
15
EXAMPLE-DRY RUN
1.Start
J K 2. Set J=K
0
10 2 2
3. Repeat steps 4 and 5 while J < N
1 50 3 2
4. Set LA[J] = LA[J+1]
25 5. Set J = J+1
2
6. Set N = N-1
3 LA[3]=LA[4]
7. Stop
4 60

5
15
EXAMPLE-DRY RUN
1.Start
J K 2. Set J=K
0
10 2 2
3. Repeat steps 4 and 5 while J < N
1 50 3 2
4. Set LA[J] = LA[J+1]
4 2
25 5. Set J = J+1
2
6. Set N = N-1
3 60 LA[4]=LA[5]
7. Stop
4

5
15
EXAMPLE-DRY RUN
1.Start
J K 2. Set J=K
0
10 2 2
3. Repeat steps 4 and 5 while J < N
1 50 3 2
4. Set LA[J] = LA[J+1]
4 2
25 5. Set J = J+1
2
6. Set N = N-1
3 60 LA[4]=LA[5]
7. Stop
4 15
N=4

5
BENEFITS OF USING ARRAY
We can retrieve an array element from its index in constant time, meaning it costs us
asymptotically nothing to look up a record.

Consist solely of data, no space wasted on links.

Physical continuity/memory locality: if we look up element i, there is a high


probability we will look up element i+1 next
DRAWBACKS OF USING
ARRAY
Inflexible: we have to decide in advance how much
space we want when the array is allocated. Once the
block of memory for the array has been allocated, that’s
it –we’re stuck with the size we’ve got.
If we try to write past the end of the array (overflow),
we’ll be intruding on memory allocated for something
else causing a segmentation fault.
We can compensate by always allocating arrays larger
than we think we’ll need, but this wastes a lot of space.

You might also like