Second Lecture
Second Lecture
Albaath university
Faculty of Mechanical and Electrical Engineering
Department of automatic control and computers
Department of Electronic and Communication
Second lecture
• Array definition.
• Array representation.
• Basic operations.
• Time complexity.
• All the data elements of an array are stored at contiguous locations in the
main memory.
• The name of the array represents the base address or the address of the
first element in the main memory. Each element of the array is
represented by proper indexing.
• As illustrated in the following figure, the base address of the array is 100
bytes. It is the address of arr[0]. Here, the size of the data type used is 4
bytes; therefore, each element will take 4 bytes in the memory.
• Advantages of Arrays
• Arrays are good for storing multiple values in a single variable.
• Sorting and searching a value in an array is easier.
• Any element in the array can be directly accessed by using the index.
• Disadvantages of Arrays
• Array is homogenous. It means that the elements with similar data
type can be stored in it.
• In array, there is static memory allocation that is size of an array
cannot be altered.
• There will be wastage of memory if we store less number of elements
than the declared size.
• Traverse Operation
#include <iostream>
using namespace std;
int main() {
int LA[] = {1,3,5,7,8};
int i = 0;
cout << "The original array elements are :\n";
for(i = 0; i<5; i++) {
cout << "LA["<<i<<"]= "<< LA[i] << "\n";
}
return 0;
}
• Insertion Operation
• Insert operation is to insert one or more data elements into
an array.
• Many types
• Insertion at the beginning of an array.
• Insertion at the given index of an array.
• Insertion after the given index of an array.
• Insertion before the given index of an array.
• Deletion Operation
• Deletion refers to removing an existing element from the array and re-
organizing all elements of an array.
• Algorithm
• Consider LA is a linear array with N elements and K is a positive integer such
that K<=N.
• Following is the algorithm to delete an element available at the Kth position of
LA.
Step 1: Start
Step 2: Set J = K
Step 3: Set N = N-1
Step 4: Repeat steps 4 and 5 while J < N
Step 5: Set LA[J] = LA[J + 1]
Step 6: Set J = J+1
Step 7: Stop
• Search Operation
• You can perform a search for an array element based on its value or its
index.
• Algorithm
• Consider LA is a linear array with N elements and K is a positive
integer such that K<=N.
• Following is the algorithm to find an element with a value of ITEM
using linear search.
Step 1: Start
Step 2: Set J = 0
Step 3: Repeat steps 4 and 5 while J < N
Step 4: IF LA[J] is equal to ITEM THEN GOTO STEP 6
Step 5: Set J = J +1
Step 6: PRINT J, ITEM
Step 7: Stop
• Update Operation
• Update operation refers to updating an existing element from the array at a
given index.
• Algorithm
• Consider LA is a linear array with N elements and K is a positive integer such
that K<=N.
• Following is the algorithm to update an element available at the Kth position
of LA.
Step 1: Start
Step 2: Set LA[K] = ITEM
Step 3: Stop
• For the Traversal operation the worst case time complexity is O(n).
• For the Insertion operation in general (Not the first element) the worst
case time complexity is O(n).
• For the Deletion operation the worst case time complexity is O(n).
• For the Search operation the worst case time complexity is O(n).
• For the update operation the worst case time complexity is O(1).