Data Structures and Algorithms - Arrays - Tutorialspoint
Data Structures and Algorithms - Arrays - Tutorialspoint
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.
Arrays can be declared in various ways in different languages. For illustration, let's take C array declaration.
As per the above illustration, following are the important points to be considered.
Index starts with 0.
Array length is 10 which means it can store 10 elements.
https://www.tutorialspoint.com/data_structures_algorithms/array_data_structure.htm 1/10
8/9/2021 Data Structures and Algorithms - Arrays - Tutorialspoint
Each element can be accessed via its index. For example, we can fetch an element at index 6 as 9.
Basic Operations
Following are the basic operations supported by an array.
Traverse − print all the array elements one by one.
Insertion − Adds an element at the given index.
Deletion − Deletes an element at the given index.
Search − Searches an element using the given index or by the value.
Update − Updates an element at the given index.
In C, when an array is initialized with size, then it assigns defaults values to its elements in following order.
bool false
char 0
int 0
float 0.0
double 0.0f
void
wchar_t 0
Traverse Operation
This operation is to traverse through the elements of an array.
https://www.tutorialspoint.com/data_structures_algorithms/array_data_structure.htm 2/10
8/9/2021 Data Structures and Algorithms - Arrays - Tutorialspoint
Example
Following program traverses and prints the elements of an array:
#include <stdio.h>
main() {
int i = 0, j = n;
When we compile and execute the above program, it produces the following result −
Output
LA[0] = 1
LA[1] = 3
LA[2] = 5
LA[3] = 7
LA[4] = 8
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 −
Example
https://www.tutorialspoint.com/data_structures_algorithms/array_data_structure.htm 3/10
8/9/2021 Data Structures and Algorithms - Arrays - Tutorialspoint
Live Demo
#include <stdio.h>
main() {
int i = 0, j = n;
n = n + 1;
while( j >= k) {
LA[j+1] = LA[j];
j = j - 1;
LA[k] = item;
When we compile and execute the above program, it produces the following result −
https://www.tutorialspoint.com/data_structures_algorithms/array_data_structure.htm 4/10
8/9/2021 Data Structures and Algorithms - Arrays - Tutorialspoint
Output
LA[0] = 1
LA[1] = 3
LA[2] = 5
LA[3] = 7
LA[4] = 8
LA[0] = 1
LA[1] = 3
LA[2] = 5
LA[3] = 10
LA[4] = 7
LA[5] = 8
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
5. Set J = J+1
6. Set N = N-1
7. Stop
https://www.tutorialspoint.com/data_structures_algorithms/array_data_structure.htm 5/10
8/9/2021 Data Structures and Algorithms - Arrays - Tutorialspoint
Example
Following is the implementation of the above algorithm −
Live Demo
#include <stdio.h>
void main() {
int k = 3, n = 5;
int i, j;
j = k;
while( j < n) {
LA[j-1] = LA[j];
j = j + 1;
n = n -1;
https://www.tutorialspoint.com/data_structures_algorithms/array_data_structure.htm 6/10
8/9/2021 Data Structures and Algorithms - Arrays - Tutorialspoint
When we compile and execute the above program, it produces the following result −
Output
LA[0] = 1
LA[1] = 3
LA[2] = 5
LA[3] = 7
LA[4] = 8
LA[0] = 1
LA[1] = 3
LA[2] = 7
LA[3] = 8
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
5. Set J = J +1
6. PRINT J, ITEM
7. Stop
https://www.tutorialspoint.com/data_structures_algorithms/array_data_structure.htm 7/10
8/9/2021 Data Structures and Algorithms - Arrays - Tutorialspoint
Example
Following is the implementation of the above algorithm −
Live Demo
#include <stdio.h>
void main() {
int item = 5, n = 5;
int i = 0, j = 0;
break;
j = j + 1;
When we compile and execute the above program, it produces the following result −
Output
https://www.tutorialspoint.com/data_structures_algorithms/array_data_structure.htm 8/10
8/9/2021 Data Structures and Algorithms - Arrays - Tutorialspoint
LA[0] = 1
LA[1] = 3
LA[2] = 5
LA[3] = 7
LA[4] = 8
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
3. Stop
Example
Following is the implementation of the above algorithm −
Live Demo
#include <stdio.h>
void main() {
int i, j;
https://www.tutorialspoint.com/data_structures_algorithms/array_data_structure.htm 9/10
8/9/2021 Data Structures and Algorithms - Arrays - Tutorialspoint
LA[k-1] = item;
When we compile and execute the above program, it produces the following result −
Output
LA[0] = 1
LA[1] = 3
LA[2] = 5
LA[3] = 7
LA[4] = 8
LA[0] = 1
LA[1] = 3
LA[2] = 10
LA[3] = 7
LA[4] = 8
https://www.tutorialspoint.com/data_structures_algorithms/array_data_structure.htm 10/10