0% found this document useful (0 votes)
29 views

Second Lecture

Array definition. • Array representation. • Memory allocation for an Array. • Advantages and Disadvantages. • Basic operations. • Time complexity

Uploaded by

Rami Rami
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Second Lecture

Array definition. • Array representation. • Memory allocation for an Array. • Advantages and Disadvantages. • Basic operations. • Time complexity

Uploaded by

Rami Rami
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

Syrian Arab Republic

Albaath university
Faculty of Mechanical and Electrical Engineering
Department of automatic control and computers
Department of Electronic and Communication

Algorithms and data structures

Second lecture

By: Dr. Hayyan Hasan

Algorithms and data structures - First semester 2023-2024 1


Outline

• Array definition.

• Array representation.

• Memory allocation for an Array.

• Advantages and Disadvantages.

• Basic operations.

• Time complexity.

Algorithms and data structures - First semester 2023-2024 2


Array definition

• Arrays are defined as the collection of similar types of data


items stored at contiguous memory locations.
• It is one of the simplest data structures where each data
element can be randomly accessed by using its index number.
• It has two important terms
• Element: Each item stored in an array is called an element.
• Index: Each location of an element in an array has a numerical index,
which is used to identify the element.

Algorithms and data structures - First semester 2023-2024 3


Array Representation

Algorithms and data structures - First semester 2023-2024 4


Memory allocation for an Array

• 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.

Algorithms and data structures - First semester 2023-2024 5


Advantages and Disadvantages

• 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.

Algorithms and data structures - First semester 2023-2024 6


Basic Operations

• Traverse: print all the array elements one by one.


• Insertion: Adds an element to the array.
• Deletion: Deletes an element at the given index.
• Search: Searches an element using the given index.
• Update: Updates an element at the given index.

Algorithms and data structures - First semester 2023-2024 7


Basic Operations Cont.

• 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;
}

Algorithms and data structures - First semester 2023-2024 8


Basic Operations Cont.

• 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.

Algorithms and data structures - First semester 2023-2024 9


Basic Operations Cont.

• Insertion at the Beginning of an Array


• When the insertion happens at the beginning, it causes all the existing
data items to shift one step downward.
• Algorithm
• We assume A is an array with N elements.
• The maximum numbers of elements it can store is defined by MAX.
• We shall first check if an array has any empty space to store any element and then
we proceed with the insertion process.

Algorithms and data structures - First semester 2023-2024 10


Basic Operations Cont.

Algorithm 1, Insert_AtF (A[N], New_Element)


INPUT: New_Element, A[N], OUTPUT: A[N].
1. IF N = MAX, return
2. ELSE
3. N = N + 1
4. For All Elements in A
5. Move to next adjacent location
6. A[FIRST] = New_Element
7. endif
8. Return A[N]

Algorithms and data structures - First semester 2023-2024 11


#include <iostream>
Basic Operations Cont.
using namespace std;
#define MAX 5
int main() {
int array[MAX] = {2, 3, 4, 5};
int N = 4; // number of elements in array
int i = 0; // loop variable
int value = 1; // new data element to be stored in array
// check if N = MAX
if (N==MAX) {
return 0;
} else {
// print array before insertion
cout << "Printing array before insertion −\n";
for(i = 0; i < N; i++) {
cout << "array[" << i <<"] = "<< array[i] << "\n";
}
// increase N to reflect number of elements
N++;
// now shift rest of the elements downwards
for(i = N; i > 0; i--) {
array[i] = array[i-1];
}
// add new element at first position
array[0] = value;
// print to confirm
cout <<"Printing array after insertion −\n";
for(i = 0; i < N; i++) {
cout << "array[" << i << "] = " << array[i] << "\n";
}
return 0;
}} Algorithms and data structures - First semester 2023-2024 12
Basic Operations Cont.

• Insertion at the Given Index of an Array


• In this scenario, we are given the exact location (index) of an array
where a new data element (value) needs to be inserted.
• First we shall check if the array is full, if it is not, then we shall move
all data elements from that location one step downward. This will
make room for a new data element.
• Algorithm
• We assume A is an array with N elements.
• The maximum numbers of elements it can store is defined by MAX.
• We shall first check if an array has any empty space to store any element and then
we proceed with the insertion process.

Algorithms and data structures - First semester 2023-2024 13


Basic Operations Cont.

Algorithm 2, Insert_AtIN (A[N], New_Element)


INPUT: New_Element, A[N], OUTPUT A[N].
1. IF N = MAX, return
2. ELSE
3. N = N + 1
4. SEEK Location index
5. For All Elements from A[index] to A[N]
6. Move to next adjacent location
7. A[index] = New_Element
8. Endif
9. Return A[N]

Algorithms and data structures - First semester 2023-2024 14


Basic Operations Cont.
#include <iostream>
using namespace std;
#define MAX 5
int main() {
int array[MAX] = {1, 2, 4, 5};
int N = 4; // number of elements in array
int i = 0; // loop variable
int index = 2; // index location to insert new value
int value = 3; // new data element to be inserted
// check if N = MAX
if (N==MAX) {
return 0;
} else {
// print array before insertion
cout << "Printing array before insertion −\n";
for(i = 0; i < N; i++) {
cout << "array[" << i << "] = " << array[i] << "\n";
}
// increase N to reflect number of elements
N++;
// now shift rest of the elements downwards
for(i = N; i > index; i--) {
array[i] = array[i-1];
}
// add new element at index position
array[index] = value;
// print to confirm
cout << "Printing array after insertion −\n";

for(i = 0; i < N; i++) {


cout << "array[" << i << "] = " << array[i] << "\n";
}
return 0;}} Algorithms and data structures - First semester 2023-2024 15
Basic Operations Cont.

• Insertion After the Given Index of an Array


• In this scenario we are given a location (index) of an array after which a new
data element (value) has to be inserted.
• Only the seek process varies, the rest of the activities are the same as in the
previous operation.
• Algorithm
• We assume A is an array with N elements.
• The maximum numbers of elements it can store is defined by MAX.
• We shall first check if an array has any empty space to store any element and then we proceed
with the insertion process.

Algorithms and data structures - First semester 2023-2024 16


Basic Operations Cont.

Algorithm 3, Insert_AfIN (A[N], New_Element)


INPUT: New_Element, A[N], OUTPUT A[N].
1. IF N = MAX, return
2. ELSE
3. N = N + 1
4. SEEK Location index
5. For All Elements from A[index+1] to A[N]
6. Move to next adjacent location
7. A[index+1] = New_Element
8. Endif
9. Return A[N]

Algorithms and data structures - First semester 2023-2024 17


Basic Operations Cont.
#include <iostream>
using namespace std;
#define MAX 5
int main() {
int array[MAX] = {1, 2, 4, 5};
int N = 4; // number of elements in array
int i = 0; // loop variable
int index = 1; // index location after which value will be inserted
int value = 3; // new data element to be inserted
// check if N = MAX
if (N==MAX) {
return 0;
} else {
// print array before insertion
cout << "Printing array before insertion −\n";
for(i = 0; i < N; i++) {
cout << "array[" << i << "] = " << array[i] << "\n";
}
// increase N to reflect number of elements
N++;
// now shift rest of the elements downwards
for(i = N; i > index + 1; i--) {
array[i] = array[i-1];
}
// add new element at after index position
array[index + 1] = value;
// print to confirm
cout << "Printing array after insertion −\n";
for(i = 0; i < N; i++) {
cout << "array[" << i << "] = " << array[i] << "\n";
}
return 0;}} Algorithms and data structures - First semester 2023-2024 18
Basic Operations Cont.

• Insertion Before the Given Index of an Array


• In this scenario we are given a location (index) of an array before
which a new data element (value) has to be inserted.
• Algorithm
• We assume A is an array with N elements.
• The maximum numbers of elements it can store is defined by MAX.
• We shall first check if an array has any empty space to store any element and then
we proceed with the insertion process.

Algorithms and data structures - First semester 2023-2024 19


Basic Operations Cont.

Algorithm 4, Insert_BeIN (A[N], New_Element)


INPUT: New_Element, A[N], OUTPUT A[N].
1. IF N = MAX, return
2. ELSE
3. N = N + 1
4. SEEK Location index
5. For All Elements from A[index-1] to A[N]
6. Move to next adjacent location
7. A[index-1] = New_Element
8. Endif
9. Return A[N]

Algorithms and data structures - First semester 2023-2024 20


Basic Operations Cont.
#include <iostream>
using namespace std;
#define MAX 5
int main() {
int array[MAX] = {1, 2, 4, 5};
int N = 4; // number of elements in array
int i = 0; // loop variable
int index = 3; // index location before which value will be inserted
int value = 3; // new data element to be inserted
// check if N = MAX
if (N==MAX) {
return 0;
} else {
// print array before insertion
cout << "Printing array before insertion −\n";
for(i = 0; i < N; i++) {
cout << "array[" << i << "] = " << array[i] << "\n";
}
// increase N to reflect number of elements
N++;
// now shift rest of the elements downwards
for(i = N; i > index-1; i--) {
array[i] = array[i-1];
}
// add new element before index position
array[index - 1] = value;
// print to confirm
cout << "Printing array after insertion −\n";
for(i = 0; i < N; i++) {
cout << "array[" << i << "] = " << array[i] << "\n";
}
return 0; }}
Algorithms and data structures - First semester 2023-2024 21
Basic Operations Cont.

• 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

Algorithms and data structures - First semester 2023-2024 22


Basic Operations Cont.
#include <iostream>
using namespace std;
int main() {
int LA[] = {1,3,5,7,8};
int k = 3, n = 5;
int i, j;
cout << "The original array elements are :\n";
for(i = 0; i<n; i++) {
cout << "LA[" << i << "] = " << LA[i] << "\n";
}
j = k;
n = n -1;
while( j < n) {
LA[j] = LA[j+1];
j = j + 1;
}
cout << "The array elements after deletion :\n";
for(i = 0; i<n; i++) {
cout << "LA[" << i << "] = " << LA[i] << "\n";
}
return 0;
}
Algorithms and data structures - First semester 2023-2024 23
Basic Operations Cont.

• 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

Algorithms and data structures - First semester 2023-2024 24


Basic Operations Cont.
#include <iostream>
using namespace std;
int main() {
int LA[] = {1,3,5,7,8};
int item = 5, n = 5;
int i = 0, j = 0, pos;
cout <<"The original array elements are :\n";
for(i = 0; i<n; i++) {
cout << "LA[" << i << "] = " << LA[i] << "\n";
}
while( j < n){
if( LA[j] == item ) {
pos = j;
break;
}
j = j + 1;
}
cout << "Found element " << item << " at position " << pos <<
"\n";
return 0;
}

Algorithms and data structures - First semester 2023-2024 25


Basic Operations Cont.

• 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

Algorithms and data structures - First semester 2023-2024 26


Basic Operations Cont.
#include <iostream>
using namespace std;
int main() {
int LA[] = {1,3,5,7,8};
int k = 3, n = 5, item = 10;
int i, j;
cout << "The original array elements are :\n";
for(i = 0; i<n; i++) {
cout << "LA[" << i << "] = " << LA[i] << "\n";
}
LA[k] = item;
cout << "The array elements after updation :\n";
for(i = 0; i<n; i++) {
cout << "LA[" << i << "] = " << LA[i] << "\n";
}
return 0;
}

Algorithms and data structures - First semester 2023-2024 27


Time complexity

• 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).

Algorithms and data structures - First semester 2023-2024 28

You might also like