You are on page 1of 4

# DATASTRUCTURES PROGRAMS

## 1) PROGRAM TO ENTER THE ARRAY ELEMENTS AT GIVEN POSITIONS WITHOUT OVERLAPPING

THE ARRAY ELEMENTS
#include<stdio.h>
#include<conio.h>
void insert(int * arr,int p,int v);
const MAX = 10;
static int ne;
void main()
{
int arr1[10],p1,v1;
int i;
clrscr();
while(ne<MAX) //Accepting the array elements
{
printf("\n\n Enter the number of position at which you want to insert the element:");
scanf("%d",& p1);
printf("\n Enter the element value:");
scanf("%d",& v1);
insert(&(arr1[0]),p1,v1);
}
printf("\n\n Array is full");
printf("\n\n Elements of an array:\n\n");
for(i=0;i<10;i++)
{
printf("\n Element %d : %d",i,arr1[i]);
}
getch();
}
void insert(int*arr,int p,int v)
{
int i; //moving elements between p and ne to one position ahead
for( i=ne;i>=p;i--)
{
arr[i+1]=arr[i];
}
arr[p]=v; //inserting the given value at the given postion in an array
ne++;
}

2) PROGRAM TO SORT THE ARRAY ELEMENTS BY BUBBLE SORT OF ARRAY WITH FIXED
LENGTH
#include<stdio.h>
#include<conio.h>
static int p=0,c=0,in=0;
void main()
{
int arr[5],i,j;
clrscr();

## printf("\n\n\n Enter the five numbers to store in the array:");

for(i=0;i<5;i++)
{
printf("\n ");
scanf("%d",&arr[i]);
}
for(i=4;i>0;i--)
{
p++;
for(j=0;j<i;j++)
{
c++;
if(arr[j]>arr[j+1])
{
in++;
int temp;
temp=arr[j+1];
arr[j+1]=arr[j];
arr[j]=temp;
}
}
}
printf("\n\n =============================================================\n");
printf("\n\n Sorted elements of an array:");
for(i=0;i<5;i++)
{
printf("\n %d",arr[i]);
}
printf("\n\n\n BUBBLE SORT INFORMATION ");
printf("\n\n Total number of passes : %d",p);
printf("\n\n Total number of comparisons : %d",c);
printf("\n\n Total number of interchanges : %d",in);
getch();
}

3) PROGRAM TO SORT THE ARRAY ELEMENT BY BUBBLE SORT FOR VARIABLE LENGTH
ARRAY
#include<stdio.h>
#include<conio.h>
static int p=0,c=0,in=0;
void main()
{
int *ptr,n,i,j;
clrscr();
printf("\n\n\n Enter the size of an array to be sorted:");
scanf("%d",&n);
ptr=new int[n];
printf("\n\n Array of required size is created");
printf("\n\n ================================================");
printf("\n\n Enter the elements of an array:");
for(i=0;i<n;i++)
{
printf("\n ");
scanf("%d",&ptr[i]);
}
for(i=(n-1);i>0;i--)
{
p++;
for(j=0;j<i;j++)
{
c++;
if(ptr[j]>ptr[j+1])
{
int temp;
temp=ptr[j+1];
ptr[j+1]=ptr[j];
ptr[j]=temp;
in++;
}
}
}
printf("\n\n ================================================");
printf("\n\n Sorted elements of an array:");
for(i=0;i<n;i++)
{
printf("\n %d",ptr[i]);
}
printf("\n\n\n
printf("\n\n\n BUBBLE SORT INFORMATION ");
printf("\n\n Total number of passes : %d",p);
printf("\n\n Total number of comparisons : %d",c);
printf("\n\n Total number of interchanges : %d",in);
getch();
}

## 4) PROGRAM TO SORT THE ARRAY ELEMENT BY INSERTION SORT

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,arr[5],m;
clrscr();

## printf("\n\n Enter the five elements to store in an array : ");

for(i=0;i<5;i++)
{
printf("\n ");
scanf("%d",&arr[i]);
}
for(i=1;i<=4;i++)
{
for(j=0;j<i;j++)
{
if (arr[i]<arr[j])
{
int temp;
temp=arr[i];
for(m=i;m>=j;m--)
{
a rr[m]=arr[m-1];
}
arr[j]=temp;
}
}
}
printf("\n\n Array elements in the srorted order : ");
for(i=0;i<5;i++)
{
printf("\n %d",arr[i]);
}
getch();
}