11 - Data Structures and Algorithms - Arrays
11 - Data Structures and Algorithms - Arrays
Arrays
The difference between an array index and a memory address is that the array
index acts like a key value to label the elements in the array. However, a
memory address is the starting address of free memory available.
Syntax
or,
data_type array_name[array_size];
Creating an array in JAVA programming language −
or,
Arrays are used as solutions to many problems from the small sorting
problems to more complex problems like travelling salesperson problem.
There are many data structures other than arrays that provide efficient time
and space complexity for these problems, so what makes using arrays better?
The answer lies in the random access lookup time.
Arrays provide O(1) random access lookup time. That means, accessing the
1st index of the array and the 1000th index of the array will both take the same
time. This is due to the fact that array comes with a pointer and an offset value.
The pointer points to the right location of the memory and the offset value
shows how far to look in the said memory.
array_name[index]
| |
Pointer Offset
Array Representation
Each element can be accessed via its index. For example, we can fetch
an element at index 6 as 23.
The basic operations in the Arrays are insertion, deletion, searching, display,
traverse, and update. These operations are usually performed to either modify
the data in the array or to report the status of the array.
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
Insertion Operation
In the insertion operation, we are adding one or more elements to the array.
Based on the requirement, a new element can be added at the beginning, end,
or any given index of array. This is done using input statements of the
programming languages.
Algorithm
1. Start
5. Increment i by 1.
7. Stop
Example
#include <stdio.h>
int main(){
int LA[3], i;
LA[i] = i + 2;
return 0;
Output
LA[0] = 587297216
LA[0] = 2
LA[1] = 3
LA[2] = 4
Algorithm
1. Start
2. Set J = K
5. Set J = J+1
6. Set N = N-1
7. Stop
Example
#include <stdio.h>
void main(){
int i;
LA[i] = LA[i+1];
n = n – 1;
Output
LA[0] = 1
LA[1] = 3
LA[2] = 5
LA[1] = 5
Search Operation
Searching an element in the array using a key; The key element sequentially
compares every value in the array to check if the key is present in the array or
not.
Algorithm
1. Start
2. Set J = 0
5. Set J = J +1
6. PRINT J, ITEM
7. Stop
Example
#include <stdio.h>
void main(){
int item = 5, n = 5;
int i = 0, j = 0;
Output
LA[0] = 1
LA[1] = 3
LA[2] = 5
LA[3] = 7
LA[4] = 8
Traversal Operation
This operation traverses through all the elements of an array. We use loop
statements to carry this out.
Algorithm
1 Start
6. End
Example
#include <stdio.h>
int main(){
int LA[] = {1,3,5,7,8};
int i = 0, j = n;
Output
LA[0] = 1
LA[1] = 3
LA[2] = 5
LA[3] = 7
LA[4] = 8
Update Operation
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
#include <stdio.h>
void main(){
int i, j;
LA[k-1] = item;
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
Display Operation
This operation displays all the elements in the entire array using a print
statement.
Algorithm
1. Start
3. Stop
Example
#include <stdio.h>
int main(){
int n = 5;
int i;
Output
LA[0] = 1
LA[1] = 3
LA[2] = 5
LA[3] = 7
LA[4] = 8