Professional Documents
Culture Documents
Lecture Week 2: Arrays & Searching
Lecture Week 2: Arrays & Searching
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 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.