0% found this document useful (0 votes)
2 views

EXP-1

The document outlines programming exercises for reversing an array and implementing searching techniques such as linear and binary search in C. It includes code examples for each algorithm along with sample outputs. Additionally, it covers sorting techniques including bubble sort, insertion sort, and selection sort with corresponding programs and outputs.

Uploaded by

hodcsdm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

EXP-1

The document outlines programming exercises for reversing an array and implementing searching techniques such as linear and binary search in C. It includes code examples for each algorithm along with sample outputs. Additionally, it covers sorting techniques including bubble sort, insertion sort, and selection sort with corresponding programs and outputs.

Uploaded by

hodcsdm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

WEEK - 1

I. Write a program to reverse an array.


II. C Programs to implement the Searching Techniques – Linear & Binary Search

Aim: Write a program to reverse an array.

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:-

Enter the size of Array:5


Enter 5 elements
12
3
45
67
8
The Elements in reverse
8 67 45 3 12
Aim: C Programs to implement the Searching Techniques – Linear & Binary Search

Linear Search Algorithm (Non Recursive):

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:

binarySearch(arr, item, beg, end)


if beg<=end
midIndex = (beg + end) / 2
if item == arr[midIndex]
return midIndex
else if item < arr[midIndex]
return binarySearch(arr, item, midIndex + 1, end)
else
return binarySearch(arr, item, beg, midIndex - 1)

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

C Programs to implement Sorting Techniques – Bubble, Selection and Insertion Sort

Bubble Sort Algorithm:

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:-

Insertion Sort Algorithm:-

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
}

void ssort(int a[10],int n) //Function Defination


{
int i,j,temp;
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i] > a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
printf("\n The array sorted in ascending order is :\n");
for(i=0;i<n;i++)
printf("%d\t", a[i]);
getch();
}

Output:

You might also like