Data Structure and Algorithms
Data Structure and Algorithms
Data Structure and Algorithms
Algorithms - Arrays
Data Structures and Algorithms - Arrays
Array is a container which can hold a fix number of items and these
items should be of the same type.
Most of the data structures make use of arrays to implement their
algorithms.
Following are the important terms to understand the concept of Array.
• 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.
Array Representation
• Arrays can be declared in various ways in different languages. For
illustration, let's take C++ array declaration.
Array Representation – cont’d
• Arrays can be declared in various ways in different languages. For
illustration, let's take C++ array declaration.
float 0.0
double 0.0f
void
Insertion Operation
• Insert operation is to insert one or more data elements into an array.
Based on the requirement, a new element can be added at the
beginning, end, or any given index of array.
• Here, we see a practical implementation of insertion operation,
where we add data at the end of the array −
Insertion
• Let A be a collection of data elements in the memory of the
computer.
• “Inserting” refers to the operation of adding another element to the
collection A, Inserting an element at the ‘end’ of a linear array can be
easily done.
• On the other hand, suppose we need to insert an element in the
middle of the array.
• Then on the average, half of the elements must be moved downward
to new locations to accommodate the new element and keep an
order of the order of the other elements.
Algorithm: (Inserting into a linear array.)
INSERT (A, N, K, ITEM).
Here, A is a linear array with N elements and K is a positive integer such that K <= N.
This algorithm inserts an element ITEM into the Kth position in A.
Step 1: [Initialize counter.] Set I:= N.
Step 2: Repeat steps 3 and 4 while I >= K:
Step 3: [Move element downward.] Set A[I+1] := A[I].
Step 4: [Decrease counter.] I:= I-1.
[End of step 2 loop.]
Step 5: [Insert element.] Set A[K] := ITEM.
Step 6: [Reset N.] Set N:= N+1.
Step 7: Exit.
Algorithm
To sort an array of size n in ascending order:
1: Iterate from arr[1] to arr[n] over the array.
2: Compare the current element (key) to its predecessor.
3: If the key element is smaller than its predecessor, compare it to the
elements before. Move the greater elements one position up to make
space for the swapped element.
Example:
Another Example:
12, 11, 13, 5, 6
Let us loop for i = 1 (second element of the array) to 4 (last element of the
array)
i = 1. Since 11 is smaller than 12, move 12 and insert 11 before 12
11, 12, 13, 5, 6
i = 2. 13 will remain at its position as all elements in A[0..I-1] are smaller than
13
11, 12, 13, 5, 6
i = 3. 5 will move to the beginning and all other elements from 11 to 13 will
move one position ahead of their current position.
5, 11, 12, 13, 6
i = 4. 6 will move to position after 5, and elements from 11 to 13 will move one
position ahead of their current position.
5, 6, 11, 12, 13
Insertion Operation - Algorithm
Example: Following is the implementation of n = n + 1;
the above algorithm: insertarray1.cpp while( j >= k) {
#include <iostream> LA[j+1] = LA[j];
using namespace std; j = j - 1;
}
int main() {
LA[k] = item;
int LA[] = {1,3,5,7,8}; cout<<("The array elements after
int item = 10, k = 3, n = 5; insertion :\n");
for(i = 0; i<n; i++) {
int i = 0, j = n;
cout<<(i, LA[i]) << endl;
cout<<("The original array elements are :\n"); }
for(i = 0; i<n; i++) { }
cout<<(i, LA[i]) <<endl;
}
• When we compile and execute the above program, it produces the
following result
Deletion
• Let A be a collection of data elements in the memory of the
computer. “Deleting” refers to the operation of removing one of the
elements from A.
• Deleting an element at the ‘end’ of an array presents no difficulties,
but deleting an element somewhere in the middle of the array would
require that each subsequent element is moved one location upward
in order to ‘fill up’ the 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.
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 – Delete array
Following is the implementation of the above while( j < n) {
algorithm − deletearray1.cpp LA[j-1] = LA[j];
#include <iostream> j = j + 1;
using namespace std; }
int main() { n = n -1;
int LA[] = {1,3,5,7,8}; cout<<("The array elements after deletion
int k = 3, n = 5; :\n");
int i, j; for(i = 0; i<n; i++) {
cout<<("The original array elements are :\n"); cout<<(i, LA[i])<<endl;
for(i = 0; i<n; i++) { }
cout<<(i, LA[i]) <<endl; }
}
j = k;
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
sequential search.
1. Start
2. Set J = 0
3. Repeat steps 4 and 5 while J < N
4. IF LA[J] is equal ITEM THEN GOTO STEP 6
5. Set J = J +1
6. PRINT J, ITEM
7. Stop
Example – Search Array
Following is the implementation of the above while( j < n){
algorithm searcharray.cpp if( LA[j] == item ) {
using namespace std; break;
int main() { }
int LA[] = {1,3,5,7,8}; j = j + 1;
int item = 5, n = 5; }
int i = 0, j = 0; cout<<"The array elements after serached
cout<<("The original array elements are :\n";
:\n"); cout<<(item, j+1)<<endl;
for(i = 0; i<n; i++) { }
cout<<(i, LA[i])<<endl;
}
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.
1. Start
2. Set LA[K-1] = ITEM
3. Stop
Example: implementation of the above algorithm
#include <iostream> larrayupdate1.cpp
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] << endl;
}
LA[k-1] = item;
cout << "The array elements after updation :\n";
for(i = 0; i<n; i++) {
cout << LA[i] << endl;
}
}
Thank You.