Data Structures (Arrays) : Mid ( (Beg+end) /2) If (Item A (Mid) ) Return Mid Else (Item A (Mid) ) End Mid-1
Data Structures (Arrays) : Mid ( (Beg+end) /2) If (Item A (Mid) ) Return Mid Else (Item A (Mid) ) End Mid-1
Array Types:
Arrays can be of different types:
i) One Dimensional Array – This array has got only one row of
elements.
ii) Multi Dimensional Arrays – This type of array must have two or
more rows.
Binary Search – Binary search makes the searching faster by applying a
technique known as ‘Divide and Conquer’. The array is divided into two
halves. Now the element will be present either in first half or in the
second half of the array. If it is seen to be in first half then first
half of the array is searched and the second half is discarded. If it is
seen to be in second half then second half of the array is searched and
the first half is discarded. This method is repeated till the element is
found. The pre condition for this search is that array should be in
sorted order.
int bin_srch(int a[],int N,int item)
{
int beg=0,end=N-1;
while(beg<=end)
{
mid=int((beg+end)/2);
if(item==a[mid])
return mid;
else
if(item<a[mid])
end=mid-1;
1| Page
else
beg=mid+1;
}
return -1;
}
Selection Sort
This sort starts from the first element and searches the entire array
until it finds the minimal value. The sort places the minimum value at
first place and writes the first place value at the place from where it
had picked up the minimum value. After this it selects the second element
and searches the second smallest element. This process is repeated until
the entire array is sorted.
#include<stdio.h>
#include<iostream.h>
#include<conio.h>
void selsort(int num[],int s)
{
clrscr();
int i,j,k,t=0;
2| Page
for(i=0;i<s;i++)
{
for(j=i;j<s;j++)
{
if(num[i]>num[j])
{
t=num[i];
num[i]=num[j];
num[j]=t;
}
}
}
}
void disp(int ar[],int s)
{
clrscr();
cout<<"\n\n\n\t";
for(int i=0;i<s;i++)
cout<<" "<<ar[i];
}
void main()
{
int a[]={22,67,8,90,1};
cout<<"before Sorting";
disp(a,5);
getch();
selsort(a,5);
cout<<"After Sorting";
disp(a,5);
getch();
}
Merge Sort
#include<stdio.h>
#include<iostream.h>
#include<conio.h>
void disp(int ar[],int s)
{
clrscr();
cout<<"\n\n\n\t";
for(int i=0;i<s;i++)
cout<<""<<ar[i];
}
void merges(int a[],int b[],int c[],int m,int n)
{
int actr=0,bctr=n-1,cctr=0;
while(actr<m&&bctr>=0)
{
if(a[actr]<=b[bctr])
c[cctr++]=a[actr++];
else
c[cctr++]=b[bctr--];
}
if(actr<m)
{
while(actr<m)
c[cctr++]=a[actr++];
}
else
{
3| Page
while(bctr>=0)
c[cctr++]=b[bctr--];
}
}
void main()
{
int a[50],b[50],c[50],MN=0,m,n;
cout<<"Enter size of first array";
cin>>m;
#include<iostream.h>
void main()
{
int num[]={44,33,55,22,11};
int i=0,j=0,t=0;
for(i=0;i<4;i++)
{
if (num[i+1] < num[0])
{
t=num[i+1];
for(j=i+1;j>=0;j--)
{
num[j]=num[j-1];
}
num[0]=t;
}
}
for(i=0;i<5;i++)
cout<<" "<<num[i];
}
Example – card Playing , sorting 13 cards in order of 1,2,3,4,…..
Insertion – Adding a new element in the array is called Insertion.if the
array is unordered, the new element is inserted at the end of the array,
(ii) if the array is sorted then new element is added at appropriate
position without altering the order and to achieve this, rest of the
elements are shifted. If the array is already full, then insertion of an
element into its result into OVERFLOW.
Deletion – The element to be deleted is first searched , if search is
successful, the element is removed and rest of the elements are shifted
so as to keep the order of array undisturbed.
void array::insarray()
{
cout<<"Enter element to add";
int ele;
cin>>ele;
if(N==50)
{ cout<<"Overflow...."; return }
int pos=0;
if(ele<ar[0])
pos=0;
else
{
for(int i=0;i<N;i++)
{
if(ar[i]<=ele && ele < ar[i+1])
{
pos=i+1;break; } } if(i==N-1) pos=N;}
for(int i=N;i>pos;i--)
{
ar[i]=ar[i-1];
}
ar[pos]=ele;
N=N+1;
}
void array::accept()
{
cout<<"Enter N";
cin>>N;
for(int i=0;i<N;i++)
{
cout<<"enter element"<<i+1;
cin>>ar[i];
5| Page
}
}
void array::disp()
{
clrscr();
for(int i=0;i<N;i++)
{
cout<<"\nelement "<<ar[i];
} getch(); }
void array::selsort()
{
int t;
for(int i=0;i<N;i++)
{ for(int j=i;j<N;j++)
{ if(ar[i]>=ar[j])
{
t=ar[i];
ar[i]=ar[j];
ar[j]=t;
} } }}
void array::bubsort()
{
int t;
for(int i=0;i<N;i++)
{ for(int j=0;j<(N-1)-i;j++)
{ if(ar[j]>ar[j+1])
{
t=ar[j];
ar[j]=ar[j+1];
ar[j+1]=t;
} } }}
void main()
{
array a1; a1.accept();a1.disp();
//a1.selsort();
a1.bubsort();
a1.disp();
a1.insarray();
a1.disp(); a1.delarray();
a1.disp();
getch();
}
Address Calculation
Row Major Implementation
A[I][J]= B+w(C(I-Lr)+(J-Lc))
Column Major
A[I][J]= B+w((I-Lr)+R(J-Lc))
6| Page