Вы находитесь на странице: 1из 4

Data Structure (ARRAYS)

A mathematical and logical model of data is known as Data Structure.

Primitive data structure: The data structure, which is available in the compiler

Non-primitive data structure: The data structure, which is not available in the compiler

Linear Data Structure: The data structure in which each element has access to maximum of one
predecessor element and maximum of one successor element is known as linear data structure. Example:
Stack, Queue, etc.

Non-linear Data Structure: The data structure in which each element can access any number of
predecessor elements and any number of successor elements is known as Non-linear data structure.
Example: Tree, Graphs, etc.

Static Data Structure: The data structure in which the number of elements is fixed, is known as Static
Data Structure. Example: Arrays

Dynamic Data Structure: The data structure in which the number of elements is not fixed, is known as
Dynamic Data Structure. Example: Linked List.

Array
It is a static data structure. It is a homogeneous collection of data. The elements in the array are stored on
consecutive memory locations. Array is also known as a subscripted variable, e.g., A[i] is ith element of the
array A (i.e., i is the subscript with variable A). As we know that the elements of the array are stored on
consecutive memory locations, it becomes convenient to find out the address of memory location of ith
element, for given base address (address of first element) and W (i.e. the number of memory locations
required by one element).

One dimensional array


Number of elements N= UB-LB+1 Where LB-Lower Bound UB-Upper Bound
Memory Location of A[i]ƒ« Address of (A[i])=Base(B)+W*(i-LB)
Address of (A[i])=Base(B)+W*I
Where N is given as in C++, LB is assumed as 0

Two dimensional array


Number of elements = M x N=(UB1-LB1+1)x(UB2-LB2+1)

Row Major: Address of(A[I][J])=Base(B) + W*(N *(I-LB1)+(J-LB2))


where Base(A) is address of first element’s memory location
N is number of columns = UB2LB2+1
W is number of memory locations required by one element
LB1is Lower Bound of Row
LB2 is Lower bound of Column
Address of (A[I][J])=Base(B) + W*(N*I+J)
where N is number of Columns, LB1=0 and LB2=0

Column Major:Address of (A[I][J])=Base(B) + W*((I-LB1)+ N*(J-LB2))


where Base(B) is address of first element’s memory location
M is number of Rows = UB1-LB1+1
W is number of memory locations required by one element
LB2 is Lower Bound of Column
UB1 is upper bound of Row
LB1 is Lower bound of Row
Loc(A[I][J])=Base(A) + W*(I+N*J)
where N is number of Rows, LB1=0 and LB2=0
Function to Search for an element from ARR by Function to Sort the array ARR by Selection
Linear Search Sort(Ascending order)
void Lsearch(int ARR[], int L) void SelectionSort(int ARR[], int L)
{ {
int Data,Found=0,C=0; for (int I=0;I<L-1;I++)
cout<<”Enter Data to be searched:”;cin>>Data; {
while (C<L && Found!=1) int Small=ARR[I];
{ if (ARR[C]==Data) int pos = I;
Found++; for (int J=I+1;J<L;J++)
else if (Small>ARR[J])
C++; { Small=ARR[J];
} POS=J;
if (Found) }
cout<<”Data Found at :”<<C<<endl; int Temp=ARR[I];
else ARR[I]=ARR[POS];
cout<<”Data Not Found in the array”<<endl; ARR[POS]=Temp;
} } //for
}
Function to Sort the array ARR by Insertion Sort Function to Sort the array ARR by Bubble Sort
void InsertionSort(int ARR[], int L) void BubbleSort(int ARR[], int L)
{ {
for (int I=1;I<L;I++) for (int I=0;I<L-1;I++)
{ for (int J=0;J<L-I-1;J++)
int Temp=ARR[I], J=I-1; if (ARR[J]>ARR[J+1])
while (Temp<ARR[J] && J>=0) {
{ int Temp=ARR[J];
ARR[J+1]=ARR[J]); J--; ARR[J]=ARR[J+1];
} ARR[J+1]=Temp;
ARR[J+1]=Temp; }
} }
}
Function to Search for an element from ARR by Function to merge X and Y arrays of lengths N and M
Binary Search Assume both the arrays are in ascending order and
(Assuming Array is presorted in increasing order) merged array should also be in ascending order
void BinarySearch(int ARR[], int L, int val) void Merge(int X[],int Y[],int ARR[],int M,int N,int
{ &L)
int LB=0,UB=L-1,Mid,Found=0; { int I=0,J=0;
cout int k=0; L=M+N; //Initialisation of counters for X, Y,
while (LB<=UB && Found==0) and ARR
{ while (I<M && J<N)
Mid=(LB+UB)/2; { if (X[I]<Y[J])
if (ARR[Mid]==val) ARR[k++]=X[I++];
Found++; else
else ARR[k++]=Y[J++];
if (ARR[Mid]<val) }
LB=Mid+1; while(I<M)
else {
UB=Mid-1; ARR[k++]=X[I++];
} }
if (Found==1) while(j<N)
cout<<”Found at:”<<Mid<<endl; {
else ARR[k++]=Y[J++];
cout<<”Not Found!!”<<endl; }
} }
Function to Insert Data at Pos in the array ARR Function to multiply two-dimensional arrays A and B
void Insert(int ARR[], int &L, int Pos, int Data) of order NxL and LxM
{ void Multiply(int A[][20],int B[][20],int C[][20],int
if (L<Max) N,int L,int M)
{ {
for (int C=L;C>Pos;C--) for (int R=0;R<N;R++)
{ for (int C=0;C<M;C++)
ARR[C]=ARR[C-1]; {
} C[R][C]=0;
ARR[Pos]=Data; for (int T=0;T<L;T++)
L++;//After Insertion of an element increase L C[R][C]+=A[R][T]*B[T][C];
} }
else }
cout<<”Array is Full”<<endl; }
Function to find the sum of two dimensional arrays A Function to Delete Data from Pos in the array ARR
and B void Delete(int ARR[], int &L, int Pos)
void Addition(int A[][20],int B[][20],int C[][20],int {
N,int M) for (int C=Pos;C<L-1;C++)
{ {
for (int R=0;R<N;R++) ARR[C]=ARR[C+1];
for (int C=0;C<M;C++) }
C[R][C]=A[R][C]+B[R][C]; L--; //After Deletion of an element decrease L
} }
Function to find & display sum of rows & sum of cols. Function to find sum of diagonal elements of a square
of a 2 dim. array A matrix A
void SumRowCol(int A[][20],int M,int N) void Diagonal(int A[20][20],int N,int &Rdiag,int
{ for (int R=0;R<M;R++) &LDiag)
{ int SumR=0; {
for (int C=0;C<N;C++) Rdiag=0; Ldiag=0;
SumR+=A[R][C]; for (int I=0;I<N;I++)
cout<<”Row(“<<R<<”)=”<<SumR<<endl; {
} for (int J=0,J<N;J++)
for (int C=0;C<N;C++) {
{ int SumC=0; if (I==J) Rdiag+=A[I][I];
for (int R=0;R<M;R++) if (I+J==N-1) Ldiag+=A[N-I-1][I];
SumC+=A[R][C]; }
cout<<”Column(“<<C<<”)=”<<SumC<<endl; }
}} }
Function to create transpose of a 2-d array A Function to display content of a 2-d array A
void Transpose(int A[20][20],int B[20][20],int N,int void Display(int A[20][20], int N, int M)
M) {
{ for (int R=0;R<M;R++)
for (int R=0;R<M;R++) { for (int C=0;C<N;C++)
for (int C=0;C<N;C++) cout<<A[R][C]<<'\t';
B[R][C]=A[C][R]; cout<<endl;
} }}
Data Representation
//program to convert 2 digit decimal no. to binary. //program to convert 2 digit octal number to binary
void main() void main()
{int n; {int n;
int r[8]; cin>>n;
for(int i=0;i<8;i++) int n1=n%10;
r[i]=0; int n2=n/10;
cin>>n; int r[6];
i=0; for(int i=0;i<3;i++)
while(n) { r[i]= n1%2;
{ r[i]=n%2; n1=n1/2;
n=n/2; }
i++; for(;i<6;i++)
} { r[i]=n2%2;
for(i=7;i>=0;i--) n2=n2/2;
cout<<r[i]; }
} for(i=5;i>=0;i--)
cout<<r[i];}

Вам также может понравиться