Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

DS - Arrays

Download as pdf or txt
Download as pdf or txt
You are on page 1of 67

Introduction to linear

data structure

Array and its operations


compiled by Ms.V.Kala
1 5 17 3 25
1 5 17 3 25
8 2 36 5 3
Definition
Array:
Contiguous area of memory
Definition
Array:
Contiguous area of memory consisting of
equal-size elements
Definition
Array:
Contiguous area of memory consisting of
equal-size elements indexed by contiguous
integers.

0 1 2 3 4 5 6
What’s Special About Arrays?

0 1 2 3 4 5 6
What’s Special About Arrays?

Constant-time access

0 1 2 3 4 5 6
What’s Special About Arrays?

Constant-time access
array_addr

0 1 2 3 4 5 6
What’s Special About Arrays?

Constant-time access
array_addr + elem_size × ( )

0 1 2 3 4 5 6
What’s Special About Arrays?

Constant-time access
array_addr + elem_size × (i − first_index)

0 1 2 3 4 5 6
Multi-Dimensional Arrays
Multi-Dimensional Arrays
(0, 0)
Multi-Dimensional Arrays

(2,3)
Multi-Dimensional Arrays

(2,3)

(2 − 0) × 6
Multi-Dimensional Arrays

(2,3)

(2 − 0) × 6 + (3 − 0)
Multi-Dimensional Arrays

(2,3)

elem_size × ((2 − 0) × 6 + (3 − 0))


Multi-Dimensional Arrays

(2,3)

array_addr +
elem_size × ((2 − 0) × 6 + (3 − 0))
Address Calculation in one Dimensional Array
Address Calculation in one Dimensional Array

Address of an element of an array say “A[ I ]” is


calculated using the following formula:
Address of A [ I ] = B + W * ( I – LB )
Where,
B =Base address
W = Storage Size of one element stored in the array (in
byte)
I = Subscript of element whose address is to be found
LB = Lower limit / Lower Bound of subscript, if not
specified assume 0 (zero)
Example
Given the base address of an array B[1300…..1900] as
1020 and size of each element is 2 bytes in the
memory. Find the address of B[1700].
Example:
Solution:
The given values are: B = 1020, LB = 1300, W = 2,
I = 1700
Address of A [ I ] = B + W * ( I – LB )
= 1020 + 2 * (1700 – 1300)
= 1020 + 2 * 400
= 1020 + 800
= 1820 [Ans]
Address Calculation in Double (Two)
Dimensional Array:
While storing the elements of a 2-D array in
memory, these are allocated contiguous
memory locations.
Therefore, a 2-D array must be linearized so
as to enable their storage.
There are two alternatives to achieve
linearization: Row-Major and Column-
Major.
Two dimensional array
ROW MAJOR AND COLUMN MAJOR
Calculating the address of an array element

Address of an element of any array say “A[ I ][ J ]”


is calculated in two forms as given:
1) Row Major System (2) Column Major System

Row Major System:


The address of a location in Row Major System is
calculated using the following formula:
Address of A [ I ][ J ] = B + W * [ N * ( I – Lr ) + ( J – Lc ) ]
Column Major System:
The address of a location in Column Major
System is calculated using the following
formula:
Address of A [ I ][ J ] = B + W * [( I – Lr ) + M * ( J – Lc )]
Where,
B = Base address
I = Row subscript of element whose address is to be found
J = Column subscript of element whose address is to be found
W = Storage Size of one element stored in the array (in byte)
Lr = Lower limit of row/start row index of matrix, if not given
assume 0 (zero)
Lc = Lower limit of column/start column index of matrix, if not
given assume 0 (zero)
M = Number of rows of the given matrix
N = Number of columns of the given matrix
(0, 0)
(0, 1)
(0, 2)
(0, 3)
(0, 4)
(0, 5)
(1, 0)
.
(0, 0)
(0, 1)
(0, 2)
(0, 3)
Row-major
(0, 4)
(0, 5)
(1, 0)
.
(0, 0) (0, 0)
(0, 1) (1, 0)
(0, 2) (2, 0)
(0, 3) (0, 1)
Row-major
(0, 4) (1, 1)
(0, 5) (2, 1)
(1, 0) (0, 2)
. .
(1, 1) (0, 0)
(1, 2) (1, 0)
(1, 3) (2, 0)
(1, 4) (0, 1)
Row-major Column-major
(1, 5) (1, 1)
(1, 6) (2, 1)
(2, 1) (0, 2)
. .
Summary
Summary
Array: contiguous area of memory
consisting of equal-size elements indexed
by contiguous integers.
Summary
Array: contiguous area of memory
consisting of equal-size elements indexed
by contiguous integers.
Constant-time access to any element.
Summary
Array: contiguous area of memory
consisting of equal-size elements indexed
by contiguous integers.
Constant-time access to any element.
Constant time to add/remove at the
end.
Summary
Array: contiguous area of memory
consisting of equal-size elements indexed
by contiguous integers.
Constant-time access to any element.
Constant time to add/remove at the
end.
Linear time to add/remove at an
arbitrary location.
Array-Basic Operations
 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.
 Sort – sorts the array elements in ascending or
descending order
 Update − Updates an element at the given index.
Traverse Operation
This operation is to traverse through the elements of
an array.
#include <stdio.h>
main()
{
int a[] = {1,3,5,7,8};
printf("The original array elements are :\n");
for(i = 0; i<n; i++)
{
printf(“a[%d] = %d \n", i, a[i]);
}
}
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 the array.
Insertion at the end
Insert 4 at index 4
Insertion at the middle

Example: Consider an array Data[10]


having 6 elements in it.

Index 1 2 3 4 5 6 7 8 9 10

Valu
10 15 2 35 60 45
e
Insertion at the middle

Now we want to place 100 at location 4.


Algorithm to Insert Element in Array starts
with shifting the elements to right as given
in the following slides.
Insertion at the middle
Index 1 2 3 4 5 6 7 8 9 10
Valu
10 15 2 35 60 45
e

Index 1 2 3 4 5 6 7 8 9 10
Value 10 15 2 35 60 45

Index 1 2 3 4 5 6 7 8 9 10
Value 10 15 2 35 60 45

Index 1 2 3 4 5 6 7 8 9 10

Value 10 15 2 35 60 45

After Insertion

Index 1 2 3 4 5 6 7 8 9 10

Value 10 15 2 100 35 60 45
Algorithm to insert an element in Array

INSERT(DATA, N, I, ELEMENT, LOC)

1. Set I:=N
2. Repeat the step 3 and 4 while I>=LOC
3. Set DATA[I+1]:=DATA[I]
4. I=I-1
[End of loop]
5. Set DATA[I]=ELEMENT
6. N=N+1
7. Exit
Deletion Operation

Deletion refers to removing an existing


element from the array and re-organizing all
the elements of an array.
Deletion at the middle
Deletion at the given index
Consider an array Data[10] having 6 elements in it.

Index 1 2 3 4 5 6 7 8 9 10

Value 10 15 20 35 60 45

Now we want to delete 15 from location 2. Algorithm to


delete Element in an Array starts with shifting the
elements to left.
Index 1 2 3 4 5 6 7 8 9 10
Value 10 15 20 35 60 45


Deletion

Index 1 2 3 4 5 6 7 8 9 10

Value 10 20 35 60 45

Index 1 2 3 4 5 6 7 8 9 10

Value 10 20 35 60 45


Deletion

Index 1 2 3 4 5 6 7 8 9 10

Value 10 20 35 60 45

After Deletion

Index 1 2 3 4 5 6 7 8 9 10

Value 10 20 35 60 45
Algorithm to delete an element from an array

DELETE(DATA, N, I, ELEMENT, LOC)

1.Set ELEMENT=DATA[LOC]
2.Repeat for I=LOC to N-1
Set DATA[I]:=DATA[I+1]
[End of loop]
3.N:=N-1
4.Exit
Search Operation
You can perform a search for an array
element based on its value or its index.
Linear search
Linear search /sequential search
#include <stdio.h>

void main() {
int a[] = {1,3,5,7,8};
int item = 7, n = 5;
int i = 0, j = 0;

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

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


printf(“a[%d] = %d \n", i, a[i]);
}

while( j < n){


if( a[j] == item ) {
break;
}

j = j + 1;
}

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


}
Sort Operation
You can arrange the elements in an array
either in ascending order or descending
order.
C program for sorting
#include <stdio.h>
void main()
{
int arr1[100];
int n, i, j, tmp;
printf("\n\nsort elements of array in ascending order :\n ");
printf("----------------------------------------------\n");
printf("Input the size of array : ");
scanf("%d", &n);
printf("Input the elements in the array :\n");
for(i=0;i<n;i++)
{
printf("element - %d : ",i);
scanf("%d",&arr1[i]);
}
Contd..
for(i=0; i<n; i++)
{
for(j=i+1; j<n; j++)
{
if(arr1[j] <arr1[i])
{
tmp = arr1[i];
arr1[i] = arr1[j];
arr1[j] = tmp;
}
}
}
Contd..
printf("\nElements of array in sorted ascending order:\n");
for(i=0; i<n; i++)
{ printf("%d ", arr1[i]);
} printf("\n\n");
}
output
sort elements of array in ascending order :
--------------------------------------------------------
Input the size of array : 5
Input the elements in the array :
element - 0 : 2
element - 1 : 7
element - 2 : 4
element - 3 : 5
element - 4 : 9

Elements of array in sorted ascending order:


2 4 5 7 9
Update Operation
Update operation refers to updating an
existing element from the array at a given
index.
Updating an item
#include <stdio.h>

void main() {
int a[] = {1,3,5,7,8};
int item = 7, n = 5, k=3,x=10;
int i = 0, j = 0;

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

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


printf(“a[%d] = %d \n", i, a[i]);
}
a[k-1]=x;
}
printf("The updated array elements are :\n");

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


printf(“a[%d] = %d \n", i, a[i]);
}

}
Output
The original array elements are :
a[0] = 1
a[1] = 3
a[2] = 5
a[3] = 7
a[4] = 8
The array elements after updating :
a[0] = 1
a[1] = 3
a[2] = 10
a[3] = 7
a[4] = 8

You might also like