0% found this document useful (0 votes)
402 views10 pages

Array Operations in Data Structures

An array is a data structure that stores fixed-length sequential collection of elements of the same type. It allows fast access of elements using indices. Key operations on arrays include traversing elements, inserting/deleting elements at a given index, and searching for an element by its value or index. Arrays are implemented using contiguous memory locations.

Uploaded by

Tania CE
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)
402 views10 pages

Array Operations in Data Structures

An array is a data structure that stores fixed-length sequential collection of elements of the same type. It allows fast access of elements using indices. Key operations on arrays include traversing elements, inserting/deleting elements at a given index, and searching for an element by its value or index. Arrays are implemented using contiguous memory locations.

Uploaded by

Tania CE
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
  • Array Definition and Representation
  • Basic Operations on Arrays
  • Traverse Operation
  • Insertion Operation
  • Deletion Operation
  • Search Operation
  • Update Operation

8/9/2021 Data Structures and Algorithms - Arrays - Tutorialspoint

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.

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.

[Link] 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.

Data Type Default Value

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.

[Link] 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 LA[] = {1,3,5,7,8};

int item = 10, k = 3, n = 5;

int i = 0, j = n;

printf("The original array elements are :\n");

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

printf("LA[%d] = %d \n", i, LA[i]);

When we compile and execute the above program, it produces the following result −

Output

The original array elements are :

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
[Link] 3/10
8/9/2021 Data Structures and Algorithms - Arrays - Tutorialspoint

Following is the implementation of the above algorithm −

Live Demo
#include <stdio.h>

main() {

int LA[] = {1,3,5,7,8};

int item = 10, k = 3, n = 5;

int i = 0, j = n;

printf("The original array elements are :\n");

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

printf("LA[%d] = %d \n", i, LA[i]);

n = n + 1;

while( j >= k) {

LA[j+1] = LA[j];

j = j - 1;

LA[k] = item;

printf("The array elements after insertion :\n");

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

printf("LA[%d] = %d \n", i, LA[i]);

When we compile and execute the above program, it produces the following result −

[Link] 4/10
8/9/2021 Data Structures and Algorithms - Arrays - Tutorialspoint

Output

The original array elements are :

LA[0] = 1

LA[1] = 3

LA[2] = 5

LA[3] = 7

LA[4] = 8

The array elements after insertion :

LA[0] = 1

LA[1] = 3

LA[2] = 5

LA[3] = 10

LA[4] = 7

LA[5] = 8

For other variations of array insertion operation click here

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

[Link] 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 LA[] = {1,3,5,7,8};

int k = 3, n = 5;
int i, j;

printf("The original array elements are :\n");

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

printf("LA[%d] = %d \n", i, LA[i]);

j = k;

while( j < n) {

LA[j-1] = LA[j];

j = j + 1;

n = n -1;

printf("The array elements after deletion :\n");

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

printf("LA[%d] = %d \n", i, LA[i]);

[Link] 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

The original array elements are :

LA[0] = 1

LA[1] = 3

LA[2] = 5

LA[3] = 7

LA[4] = 8

The array elements after deletion :

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

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

[Link] 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 LA[] = {1,3,5,7,8};

int item = 5, n = 5;

int i = 0, j = 0;

printf("The original array elements are :\n");

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

printf("LA[%d] = %d \n", i, LA[i]);

while( j < n){


if( LA[j] == item ) {

break;

j = j + 1;

printf("Found element %d at position %d\n", item, j+1);

When we compile and execute the above program, it produces the following result −

Output

[Link] 8/10
8/9/2021 Data Structures and Algorithms - Arrays - Tutorialspoint

The original array elements are :

LA[0] = 1

LA[1] = 3

LA[2] = 5

LA[3] = 7

LA[4] = 8

Found element 5 at position 3

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
Following is the implementation of the above algorithm −

Live Demo
#include <stdio.h>

void main() {

int LA[] = {1,3,5,7,8};

int k = 3, n = 5, item = 10;

int i, j;

[Link] 9/10
8/9/2021 Data Structures and Algorithms - Arrays - Tutorialspoint

printf("The original array elements are :\n");

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

printf("LA[%d] = %d \n", i, LA[i]);

LA[k-1] = item;

printf("The array elements after updation :\n");

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

printf("LA[%d] = %d \n", i, LA[i]);

When we compile and execute the above program, it produces the following result −

Output

The original array elements are :

LA[0] = 1

LA[1] = 3

LA[2] = 5

LA[3] = 7

LA[4] = 8

The array elements after updation :

LA[0] = 1

LA[1] = 3

LA[2] = 10

LA[3] = 7

LA[4] = 8

[Link] 10/10

Common questions

Powered by AI

The basic operations supported by an array data structure include traversal, insertion, deletion, search, and update. Traversal involves accessing each element sequentially, which maintains the array's structure . Insertion adds an element at a specific index, potentially shifting subsequent elements to accommodate the new entry . Deletion removes an element at a specified index, requiring a shift of elements to fill the gap, thus modifying the array's structure . Search locates an element by index or value without altering the array . Update changes the value of an element at a specified index, which does not affect the array's overall structure .

Array indexing impacts the performance of operations by allowing direct access to elements, as opposed to sequential access. This direct access model enables efficient retrieval and update operations, as each element can be accessed in constant time using its index . However, insertion and deletion operations may require shifts of multiple elements to maintain the order, which can be costly in terms of time complexity, especially in large arrays . The zero-based index used in many programming languages simplifies calculations for accessing elements but requires careful management during operations to avoid off-by-one errors .

The update operation in an array involves replacing the value of an existing element at a given index without modifying the array's length or structure . In contrast, the insertion operation requires making space by potentially shifting other elements and adding an element at a specified position, which can change the array's structure and requires additional memory space if the array's capacity is exceeded . Update is typically more efficient, working in constant time since no elements need to be shifted, while insertion can have time complexity up to O(n) due to element shifts and potential reallocation .

Array representation varies significantly across different programming languages, mainly concerning initialization, bounds checking, and memory management. For example, C requires explicit bounds management, offering performance efficiency at the cost of potential errors like buffer overflows . Java, on the other hand, provides built-in bounds checking and automatically initializes elements, enhancing safety but potentially reducing performance due to the overhead . These differences are significant because they influence the trade-offs between performance and safety, thereby affecting error handling and ease of use when choosing a language for a specific application .

The search operation using sequential search involves starting at the first element and checking each element in turn until the target value is found or the end of the array is reached . Although straightforward and easy to implement, sequential search has a time complexity of O(n), which can be inefficient for large datasets as it may require examining every element in the array . The inability to quickly skip elements or narrow down the search scope without prior sorting of the array makes this method less efficient compared to other search algorithms like binary search, which require less time under specific conditions .

In C, when an array is initialized with a defined size, it is automatically filled with default values based on the data type, which simplifies memory allocation but requires explicit handling during usage. For instance, 'int' and 'char' arrays are filled with zeros, 'float' and 'double' with 0.0, and 'bool' with 'false' . These default values ensure that the array has valid initial content without explicit initialization, which reduces the risk of undefined behavior from uninitialized memory access. However, relying entirely on default values may lead to logic errors if programmers assume meaningful initial values .

Deleting an element from an array involves first identifying the element to be removed based on either its index or value, then shifting all elements that come after this position to fill the vacated space . This process effectively reduces the array's logical size by one, reorganizing the elements to maintain order. Although this ensures contiguous memory and consistent indexing, it increases access time complexity post-deletion as the shift operation has a time complexity of O(n) in the worst case, affecting performance in large arrays . Subsequent element accesses remain consistent as the array is densely packed, but shifts can impact performance during the operation itself .

Array traversal is a fundamental operation because it allows access to all elements in the array, enabling insertions, deletions, and updates. The operation typically has a time complexity of O(n), where n is the number of elements because each element must be individually accessed, either for processing or modification . Effective traversal implementations have significant implications for performance, especially concerning operations applied during a traversal loop, such as condition checks or calculations. The overall efficiency of an algorithm can be highly dependent on the efficiency of its array traversals, impacting application speed and responsiveness .

Choosing a data type for an array during initialization impacts memory usage, performance, and the available operations on array elements. For instance, 'int' arrays use more memory than 'char' arrays but support a broader set of arithmetic operations . Additionally, the default values assigned to elements upon initialization differ by type, which influences initial program behavior and debugging. A 'float' array might initialize to 0.0, indicating precision of operations required, whereas 'int' arrays initialize to 0, providing natural number operations . The choice affects the program's operational range, precision, and complexity, impacting performance and correctness .

Inserting an element into an array involves specifying the position of insertion, shifting elements to the right from that position to make room, and then placing the new element at the desired index . The constraints affecting this operation include the static nature of arrays, where the size is predefined and cannot be dynamically expanded, making additions possible only within existing bounds. If the array is full, insertion requires resizing or replacing a current element, which may not be feasible in statically-sized arrays . Additionally, carefully managing index boundaries is necessary to prevent overflow errors .

8/9/2021
Data Structures and Algorithms - Arrays - Tutorialspoint
https://www.tutorialspoint.com/data_structures_algorithms/a
8/9/2021
Data Structures and Algorithms - Arrays - Tutorialspoint
https://www.tutorialspoint.com/data_structures_algorithms/a
8/9/2021
Data Structures and Algorithms - Arrays - Tutorialspoint
https://www.tutorialspoint.com/data_structures_algorithms/a
8/9/2021
Data Structures and Algorithms - Arrays - Tutorialspoint
https://www.tutorialspoint.com/data_structures_algorithms/a
8/9/2021
Data Structures and Algorithms - Arrays - Tutorialspoint
https://www.tutorialspoint.com/data_structures_algorithms/a
8/9/2021
Data Structures and Algorithms - Arrays - Tutorialspoint
https://www.tutorialspoint.com/data_structures_algorithms/a
8/9/2021
Data Structures and Algorithms - Arrays - Tutorialspoint
https://www.tutorialspoint.com/data_structures_algorithms/a
8/9/2021
Data Structures and Algorithms - Arrays - Tutorialspoint
https://www.tutorialspoint.com/data_structures_algorithms/a
8/9/2021
Data Structures and Algorithms - Arrays - Tutorialspoint
https://www.tutorialspoint.com/data_structures_algorithms/a
8/9/2021
Data Structures and Algorithms - Arrays - Tutorialspoint
https://www.tutorialspoint.com/data_structures_algorithms/a

You might also like