Powered By Blogger

Wednesday 27 April 2011

Different operations like Insertion,Deletion,Display,Linear Search,Binary Search,Bubble Sort,Selection Sort, Insertion Sort, Swapping performed on a user defined length restricted linear data structure(ARRAY) in C


#include<stdio.h>
void insert(int *);
void del(int *);
void display(int *);
void lsearch(int *);
void bsearch(int *);
void bsort(int *);
void ssort(int *);
void isort(int *);
void Swap(int *);
void insrt(int *,int,int);
int fmin(int *,int,int);
// GLOBAL VARIABLES USED TO KEEP TRACK OF THE ARRAY
int count=0,length=0,empty=0;

//Start of Main();
int main(void)
{
int *array;
int choice,i=0;
int elem=3;


do
{
system("cls");
printf("\n Enter the length of the array to be created :: ");
scanf("%d",&length);
array=(int*)malloc(sizeof(int)*length);
printf("\n Enter the total no of elements you want to insert :: ");
scanf("%d",&count);
if(count>length)
{
printf("\n ERROR!!!   The no of elements to be inserted can't be greater than array size");
getch();
}
}while(count>length);
 printf("\n Please enter the elements of the array :: "); for(i=0;i<count;i++)
{
scanf(" %d",&array[i]);
}
empty=(length-count);

printf("\n\n\n The Given array is ::");
printf("\n\n |");
for(i=0;i<count;i++)
{
printf("%d|",array[i]);
}
getch();

do
{
system("cls");
printf("\n\n\n ******** OPERATION ON ARRAY ********\n\n");
printf("\n 1. insertion at a specific place");
printf("\n 2. Deletion from a specific place");
printf("\n 3. Display the modified array");
printf("\n 4. Search (linear) for an element in the array");
printf("\n 5. Search (binary) for an element in the array");
printf("\n 6. Bubble sort the array in desending order");
printf("\n 7. Selection sort the array in accsending order");
printf("\n 8. Insertion sort the array in accsending order");
printf("\n 11.Swap two walues of the array");
printf("\n 20. Exit");
printf("\n\n\n Enter your choice :: ");
scanf("%d",&choice);

switch(choice)
{
case 1:
insert(array);
break;

case 2:
del(array);
break;
case 3:
display(array);
break;
case 4:
lsearch(array);
break;
case 5:
bsearch(array);
break;
case 6:
bsort(array);
break;

  case 7: ssort(array);
break;
case 8:
isort(array);
break;
case 11:
Swap(array);
break;
case 20:
printf("\n");
exit(0);

default:
printf("\n Enter a valid choice :: ");
scanf("%d",&choice);
break;
}
}while(1);

return 0;
}
//////////////////////////////////////////////////////////////////////////////////////////////////

void Swap(int *arr)
{
int x=0,y=0,swap=0,i=0;
printf("\n******** SWAPING ********\n");
printf("\n The array before swaping ::");
printf("\n\n |");
for(i=0;i<count;i++)
{
printf("%d|",arr[i]);
}
printf("\n\n Swap position :: ");
scanf("%d",&x);
printf("\n With the position :: ");
scanf("%d",&y);
swap=arr[x-1];
arr[x-1]=arr[y-1];
arr[y-1]=swap;
printf("\n\n The array after swaping ::");
printf("\n\n |");
for(i=0;i<count;i++)
{
printf("%d|",arr[i]);
}
getch();
}

//////////////////////////////////////////////////////////////////////////////////////////////////


void insert(int *arr)
{
int pos=0,i=0,value=0;


printf("\n******** INSERTION ********\n");
printf("\n The place where the element is to be inserted:: ");
scanf("%d",&pos);
if(empty==0)
{
printf("\n The given array is full element cant be inserted\n\n");
getch();
return 0;
}
printf("\n The array before insertion ::");
printf("\n\n |");
for(i=0;i<count;i++)
{
printf("%d|",arr[i]);
}
printf("\n\n Total no of vaccant places :: %d",empty);

for(i=(count-1);i>=(pos-1);i--)
{
arr[i+1]=arr[i];
}
printf("\n\n enter the element to be inserted at %d place :: ",pos);
scanf("%d",&value);
arr[i+1]=value;
count=count+1;
empty=length-count;
printf("\n The array after insertion ::");
printf("\n\n |");
for(i=0;i<count;i++)
{
printf("%d|",arr[i]);
}
printf("\n\n Total no of vaccant places :: %d",empty);
printf("\n\n");
getch();
}

/////////////////////////////////////////////////////////////////////////////////////////////////////
void del(int *arr)
{
int pos=0,i=0;


printf("\n******** DELETION ********\n");

if(empty==length)
{
printf("\n The given array is empty. Element cant be deleted\n\n");
getch();
return 0;
}
printf("\n The array before deletion ::");
printf("\n\n |");
for(i=0;i<count;i++)
{
printf("%d|",arr[i]);
}
printf("\n\n Total no of vaccant places :: %d",empty);

printf("\n The element is to be deleted:: ");
scanf("%d",&pos);

for(i=(pos-1);i<(count-1);i++)
{
arr[i]=arr[i+1];
}
count=count-1;
empty=length-count;
printf("\n The array after deletion ::");
printf("\n\n |");
for(i=0;i<count;i++)
{
printf("%d|",arr[i]);
}
printf("\n\n Total no of vaccant places :: %d",empty);
printf("\n\n");
getch();
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////
void display(int *arr)
{
int i=0;
printf("\n******** DISPLAY ********\n");
printf("\n The modified array is ::");
printf("\n\n |");
for(i=0;i<count;i++)
{
printf("%d|",arr[i]);
}
printf("\n\n Total no of vaccant places :: %d",empty);
printf("\n\n");
getch();
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////

void lsearch(int *arr)
{
int search=0,flag=0,pos=0,i=0;
printf("\n******** LINEAR SEARCH ********\n");
printf("\n The number to be searched :: ");
scanf("%d",&search);
for(i=0;i<count;i++)
{
if(arr[i]==search)
{
flag=flag+1;
pos=(i+1);
}
}

printf("\n The exsisting array is ::");
printf("\n\n |");
for(i=0;i<count;i++)
{
printf("%d|",arr[i]);
}

if(flag==0)
{
printf("\n\n The number is not present in the array\n");
}
if(flag>0)
{
printf("\n\n The total number of times %d occured in the array is :: %d\n",search,flag);
printf("\n Last occurence of %d is at place :: %d\n",search,pos);
}
getch();
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////


void bsearch(int *arr)
{
int key=0,flag=0,i=0,lb=0,ub=count,mid=0;
printf("\n******** BINARY SEARCH ********\n");
printf("\n The number to be searched :: ");
scanf("%d",&key);
while(lb<=ub)
{
mid=((lb+ub)/2);
if(key==arr[mid])
{
flag=1;
break;
}

if(key<arr[mid])
{
ub=(mid-1);
}
if(key>arr[mid])
{
lb=(mid+1);
}
}
printf("\n The exsisting array is ::");
printf("\n\n |");
for(i=0;i<count;i++)
{
printf("%d|",arr[i]);
}

if(flag==0)
{
printf("\n\n The number is not present in the array\n");
}
if(flag>0)
{
printf("\n\n The number %d is present in the %d position of the array.\n",key,(mid+1));
}
getch();
}

/////////////////////////////////////////////////////////////////////////////////////////////

void bsort(int *arr)
{
int i=0,j=1,swap=0;

printf("\n******** BUBBLE SORT ********\n");
printf("\n The exsisting array is ::");
printf("\n\n |");
for(i=0;i<count;i++)
{
printf("%d|",arr[i]);
}

for(i=0;i<count;i++)
{
for(j=0;j<count;j++)
{
if(arr[i]>arr[j])
{
swap=arr[i];
arr[i]=arr[j];
arr[j]=swap;
}
}
}

printf("\n\n The array after bubble sorting::");
printf("\n\n |");
for(i=0;i<count;i++)
{
printf("%d|",arr[i]);
}
getch();
}

///////////////////////////////////////////////////////////////////////////////////////////////

void ssort(int *arr)
{
int i=0,lb=0,ub=count,swap=0,min=0;

printf("\n******** SELECTION SORT ********\n");
printf("\n The exsisting array is ::");
printf("\n\n |");
for(i=0;i<count;i++)
{
printf("%d|",arr[i]);
}

for(lb=0;lb<ub;lb++)
{
min=fmin(arr,lb,ub);
swap=arr[lb];
arr[lb]=arr[min];
arr[min]=swap;
}

printf("\n\n The array after selection sort ::");
printf("\n\n |");
for(i=0;i<count;i++)
{
printf("%d|",arr[i]);
}
getch();
}

int fmin(int *arr1,int lb1,int ub1)
{
int min=lb1;
for(;lb1<ub1;lb1++)
{
if(arr1[min]>arr1[lb1])
{
min=lb1;
}
}
return min;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////


void isort(int *arr)
{
int k,n=count,key,i,x;

printf("\n******** INSERTION SORT ********\n");
printf("\n The exsisting array is ::");
printf("\n\n |");
for(x=0;x<count;x++)
{
printf("%d|",arr[x]);
}


for (k=1;k<n;++k)
{
key=arr[k];
i=k-1;
while((i>=0)&&(key<arr[i]))
{
arr[i+1]=arr[i];
--i;
}
arr[i+1]=key;
}

printf("\n\n The array after insertion sort is ::");
printf("\n\n |");
for(x=0;x<count;x++)
{
printf("%d|",arr[x]);
}
getch();
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////


No comments:

Post a Comment