Professional Documents
Culture Documents
Chethan N V
WELCOME
Linear Search
08-08-2022
ALGORITHM:
int c=-1
for(i=0;i<length;I++)
If(key==A[I])
C=I;
return c;
If(c<0)
Print(“Search Successful”);
else
Print(Index or c);
Page No. 1
21BCE0427
Chethan N V
PROGRAM:
#include<stdio.h>
#include<string.h>
struct Array
{
char A[100][100];
int length;
};
int LS(struct Array arr,char key[15])
{
int i;
for(i=0;i<arr.length;i++)
if(strcmp(key,arr.A[i])==0)
return i;
return -1;
}
int main()
{
struct Array arr1;
printf("Enter the length of array: ");
scanf("%d",&arr1.length);
printf("Enter the elements: ");
for(int i=0;i<arr1.length;i++)
scanf("%s",arr1.A[i]);
printf("Enter the element to be searched: ");
char key[100];
scanf("%s",&key);
Page No. 2
21BCE0427
Chethan N V
int index=LS(arr1,key);
if(index>-1)
printf("Search Element: %s\nIndex: %d",key,index);
else
printf("Element not found");
return 0;
Page No. 3
21BCE0427
Chethan N V
OUTPUT:
Page No. 4
21BCE0427
Chethan N V
Binary Search
08-08-2022
ALGORITHM:
Bin Search(l,h,key)
while(l<=h)
mid=(l+h)/2
If(key==A[mid])
return mid;
Else if(key<A[mid])
h=mid-1
Else
l=mid+1
return -1
Page No. 5
21BCE0427
Chethan N V
Program:
#include<stdio.h>
struct Array
{
int A[10];
int length;
};
int BinarySearch(struct Array arr,int key)
{
int l,mid,h;
l=0;
h=arr.length-1;
while(l<=h)
{
mid=(l+h)/2;
if(key==arr.A[mid])
return mid;
else if(key<arr.A[mid])
h=mid-1;
else
l=mid+1;
}
return -1;
}
int main()
{
Page No. 6
21BCE0427
Chethan N V
scanf("%d",&arr1.length);
int key;
scanf("%d",&key);
int index=BinarySearch(arr1,key);
if(index>-1)
printf("Search element: %d \nIndex=%d",key,index);
else
Page No. 7
21BCE0427
Chethan N V
Output:
Page No. 8
21BCE0427
Chethan N V
Bubble Sort
08-08-2022
ALGORITHM:
int flag;
for(i=0;i<n-1;i++)
flag=0;
for(j=0;j<n-i-1;j++)
if(A[i]>A[j+1]
flag=1
swap(A[j],A[j+1])
if(flag==0)
break;
}
Page No. 9
21BCE0427
Chethan N V
Program:
#include <stdio.h>
struct mix
{
int number;
char string[100];
};
void accept(struct mix l[], int s);
void display(struct mix l[], int s);
{
int n;
printf("Enter the record numbers : ");
scanf("%d", &n);
sort(data, n);
printf("\nSorted Array: ");
display(data, n);
return 0;
}
scanf("%d", &l[i].number);
printf("\nEnter string : ");
scanf("%s", &l[i].string);
int i;
printf("\n\nNumber\tString\n");
for (i = 0; i < s; i++)
{
printf("%d\t%s\n", l[i].number, l[i].string);
}
}
void sort(struct mix l[100], int s)
{
int i, j;
Page No. 11
21BCE0427
Chethan N V
temp = l[j];
l[j] = l[j + 1];
l[j + 1] = temp;
}
}
Page No. 12
21BCE0427
Chethan N V
Output:
Page No. 13
21BCE0427
Chethan N V
Insertion Sort
08-08-2022
Algorithm:
void insertionSort(int A[],int n)
for(i=1;i<n;i++)
j=i-1;
x=A[i];
A[j+1]=A[j];
j--;
A[j+1]=x
Page No. 14
21BCE0427
Chethan N V
Program:
#include <stdio.h>
#include<stdlib.h>
void Insertion(float A[],int n)
{
int i,j;
float x;
for(i=1;i<n;i++)
{
j=i-1;
x=A[i];
j--;
A[j+1]=x;
}
}
int main()
{
int n;
printf("Enter the size of an array: ");
scanf("%d",&n);
float A[n];
printf("Enter the elements: ");
Page No. 15
21BCE0427
Chethan N V
for(int i=0;i<n;i++)
scanf("%f",&A[i]);
Insertion(A,n);
printf("\n");
return 0;
Page No. 16
21BCE0427
Chethan N V
Output:
Page No. 17
21BCE0427
Chethan N V
Selection Sort
08-08-2022
Algorithm:
void selectionSort(int A[],int n)
int I;
for(i=0;i<n-1;i++)
for(j=k=i;j<n;j++)
if(A[j]<A[k])
k=j;
swap(A[I],A[k])
Page No. 18
21BCE0427
Chethan N V
Program:
#include <stdio.h>
#include<stdlib.h>
void swap(int *x,int *y)
{
int temp=*x;
*x=*y;
*y=temp;
}
void SelectionSort(int A[],int n)
{
int i,j,k;
for(i=0;i<n-1;i++)
{
for(j=k=i;j<n;j++)
{
if(A[j]<A[k])
k=j;
}
swap(&A[i],&A[k]);
}
}
int main()
{
Page No. 19
21BCE0427
Chethan N V
int n;
scanf("%d",&n);
int A[n];
printf("Enter the elements: ");
for(int i=0;i<n;i++)
{
scanf("%d",&A[i]);
}
SelectionSort(A,n);
printf("Sorted Array: ");
for(int i=0;i<n;i++)
printf("%d ",A[i]);
printf("\n");
return 0;
}
Time Complexity: O(n2)
Page No. 20
21BCE0427
Chethan N V
Output:
Page No. 21