EXP-1
EXP-1
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int *p,n,i;
clrscr();
printf("Enter the size of Array:");
scanf("%d",&n);
p=(int *)malloc(n);
printf("Enter %d elements \n",n);
for(i=0;i<n;i++)
scanf("%d",(p+i));
printf("The Elements in reverse \n");
for(i=n-1;i>=0;i--)
printf("%d ",*(p+i));
}
Sample Output:-
Step 1: Start
Step 2: Read n, a[i], key values as integers
Step 3: Search the list
While (a[i] != key && i <= n)
i=i+1
Repeat step 3
Step 4: Successful search
if(i = n + 1) then print “Element not found in the list”
else
print “Element found in the list”
Return (i)
Step 6: Stop
Program:
#include<stdio.h>
#include<conio.h>
void LSearch(int a[20], int n);
void main()
{
int i, a[20], n;
clrscr();
printf(“Enter the size of an array \n”);
scanf(“%d”, &n);
printf(“Enter the array elements”);
for(i = 0; i < n; i++)
{
scanf(“%d”, &a[i]);
}
LSearch(a,n);
}
void LSearch(int a[20],int n)
{
int i,key,flag0;
printf(“Enter the key elements”);
scanf(“%d”, &key);
for(i = 0; i < n; i++)
{
if(a[i] == key)
{
flag = 1;
break;
}
}
if(flag == 1)
printf(“The key elements is found at location %d”, i + 1);
else
printf(“The key element is not found in the array”);
getch();
}
Output:-
Recursive Binary Search Algorithm:
return -1
Program:
#include <stdio.h>
#include<conio.h>
int binarySearch(int arr[10],int item,int beg,int end)
{
int mid,flag=0;
while (beg<=end)
{
mid=(beg+end)/2;
if (arr[mid]==item)
{
flag=1;
break;
}
else if (arr[mid]<item)
beg=mid+1;
else if(arr[mid]>item)
end=mid-1;
}
return flag;
}
void main()
{
int arr[10],n,item;
int i,ans;
clrscr();
printf("Enter thr Size of Array :");
scanf("%d",&n);
printf("Enter %d Elements \n",n);
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
printf("Enter the number you want search:" );
scanf("%d",&item);
ans=binarySearch(arr,item,0,n-1);
if (ans==1)
printf("Element is found");
else
printf("Element is not found");
}
Output:-
WEEK - 2
Program:-
#include <stdio.h>
#include<stdlib.h>
#include <conio.h>
void bsort(); //function declaration
int *p,n;
void main()
{
int i;
clrscr();
printf("\n Enter the number of elements in the array : ");
scanf("%d", &n);
p=(int *) malloc(n);
printf("\n Enter the elements: ");
for(i=0;i<n;i++)
scanf("%d",(p+i));
bsort(); //function calling
}
void bsort() // Function Defination
{
int i,j,temp;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i;j++)
{
if( *(p+j)>*(p+j+1))
{
temp = *(p+j);
*(p+j) = *(p+j+1);
*(p+j+1) = temp;
}
}
}
printf("\n The array sorted in ascending order is :\n");
for(i=0;i<n;i++)
printf("%d \t", *(p+i));
getch();
}
Output:-
Program:-
#include <stdio.h>
#include <conio.h>
#define size 5
void insertion_sort(int arr[], int n);
void main()
{
int arr[size], i, n;
printf("\n Enter the number of elements in the array: ");
scanf("%d", &n);
printf("\n Enter the elements of the array: ");
for(i=0;i<n;i++)
{
scanf("%d", &arr[i]);
}
insertion_sort(arr, n);
printf("\n The sorted array is: \n");
for(i=0;i<n;i++)
printf(" %d\t", arr[i]);
getch();
}
void insertion_sort(int arr[], int n)
{
int i, j, temp;
for(i=1;i<n;i++)
{
temp = arr[i];
j = i-1;
while((temp < arr[j]) && (j>=0))
{
arr[j+1] = arr[j];
j--;
}
arr[j+1] = temp;
}
}
Output:-
Selection Sort Algorithm:
selectionSort(array, size)
repeat (size - 1) times
set the first unsorted element as the minimum
for each of the unsorted elements
if element < currentMinimum
set element as new minimum
swap minimum with first unsorted position
Program:
#include <stdio.h>
#include <conio.h>
void ssort(int a[10],int n); //Function Declaration
void main()
{
int i, n, arr[10];
clrscr();
printf("\n Enter the number of elements in the array : ");
scanf("%d", &n);
printf("\n Enter the elements: ");
for(i=0;i<n;i++)
{
scanf("%d", &arr[i]);
}
ssort(arr,n);//Function Calling
}
Output: