0% found this document useful (0 votes)
17 views

Array

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Array

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

CS1-3 Sem

rd

ARRAYS
Definition
• Array 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.

• 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
Representation of Array in Memory
• Computer doesn’t need to keep track of address of
every element of LA(linear array),but needs track
only the address of first element of LA, denoted by
Base(LA) //base address
Using base address computer calculates the address of
any element of LA by formula
LOC(LA[K])=Base(LA)+w(K-Lower bound)
w: no. of words per memory cell of array
LOC(LA[K]): location to find
K:location to find
Lower bound: start value
Representation of 2D Array in Memory

• Row Major
LOC(LA[i][j])=Base(LA)+w[N(i-1)+(j-1)]

• Column Major
LOC(LA[j][i])=Base(LA)+w[M(j-1)+(i-1)]
Array 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.
• Update − Updates an element at the given index.
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.
Algorithm of Insertion Let LA be a Linear Array (unordered) with N elements
K is a positive integer such that K<=N.
Following is the algorithm where ITEM is inserted into the Kth position of LA

1. Set J := N [Initialize Counter]


2. Repeat steps 3 and 4 while J >= K
3. Set LA[J+1] := LA[J] [Move Jth element downward]
4. Set J := J-1 [Decrease Counter]
[End of Step 2 Loop]
5. Set LA[K] := ITEM
6. Set N := N+1 [Reset N]
7. Exit
void main()
{int A[10],n,i,j,k,item;
cout<<"enter no of elements";
cin>>n;
for(i = 0; i<n; i++) {
cin>>A[i]; }
j=n;
while( j >= k)
{
A[j+1] = A[j];
j = j - 1;
}
cout<<"enter position";
cin>>k;
cout<<"enter item";
cin>>item;
A[k] = item;
n=n+1;
cout<<"The array elements after insertion :\n";
for(i = 0; i<n; i++) {
cout<<A[i];
}
getch();
Algorithm of Deletion Let LA be a Linear Array (unordered) with N elements
K is a positive integer such that K<=N.

Following is the algorithm where ITEM is delete the Kth element of LA

1. Set ITEM:=LA[K]
2. Repeat for J=K to N-1
Set LA[J] := LA[J+1] [Move J+1st element upward]
[End of Loop]
3. Set N := N-1 [Reset N]
4. Exit
Algorithm of Searching Consider LA is a linear array with N elements
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. Set J = 0
2. Repeat steps 3 and 4 while J < N
3. IF LA[J] is equal ITEM THEN GOTO STEP 5
4. Set J = J +1
[End of loop]
5. PRINT J, ITEM
6. Exit
Update Program
void main()
{int a[10],k, item, n, i, j;
cout<<"enter no of elements:\n";
cin>>n;
for(i =0; i<n; i++)
{ cin>>a[i]; }
for(i = 0; i<n; i++)
{ cout<<a[i]; }
cout<<"enter position:\n";
cin>>k;
cout<<"enter item:";
cin>>item;
for(i=0;i<n;i++)
{ a[k]=item; }
cout<<"The array elements after updation :\n";
for(i = 0; i<n; i++)
{cout<<a[i];}
Sparse Matrix
• A matrix is a two-dimensional data object made
of m rows and n columns, therefore having
total m x n values. If most of the elements of
the matrix have 0 value, then it is called a
sparse matrix.
00304
00570
00000
02600
Why to use Sparse Matrix instead of simple matrix ?

• Storage: There are lesser non-zero elements


than zeros and thus lesser memory can be
used to store only those elements.
• Computing time: Computing time can be
saved by logically designing a data structure
traversing only non-zero elements..
• Representing a sparse matrix by a 2D array
leads to wastage of lots of memory as zeroes
in the matrix are of no use in most of the
cases.
• So, instead of storing zeroes with non-zero
elements, we only store non-zero elements.
• This means storing non-zero elements
with triples- (Row, Column, value).
Sparse Matrix Representations can be
done in many ways following are two
common representations:
1. Array representation
2. Linked list representation
Using Arrays
• Row: Index of row, where non-zero element is located
• Column: Index of column, where non-zero element is
located
• Value: Value of the non zero element located at index
– (row,column)
• Using Linked Lists
• In linked list, each node has four fields. These four
fields are defined as:
• Row: Index of row, where non-zero element is located
• Column: Index of column, where non-zero element is
located
• Value: Value of the non zero element located at index
– (row,column)
• Next node: Address of the next node

You might also like