You are on page 1of 21

21BCE0427

Chethan N V

WELCOME

Linear Search

08-08-2022

AIM: To implement Linear Search using Array in C language

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;

TIME COMPLEXITY: O(n)

Page No. 3
21BCE0427
Chethan N V

OUTPUT:

Page No. 4
21BCE0427
Chethan N V

Binary Search

08-08-2022

AIM: To implement Binary Search using Array in C language.

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()
{

struct Array arr1;

Page No. 6
21BCE0427
Chethan N V

printf("Enter the length of array: ");

scanf("%d",&arr1.length);

printf("Enter the elements: ");


for(int i=0;i<arr1.length;i++)
scanf("%d",&arr1.A[i]);

printf("Enter the element to be searched: ");

int key;

scanf("%d",&key);
int index=BinarySearch(arr1,key);

if(index>-1)
printf("Search element: %d \nIndex=%d",key,index);
else

printf("Element not found");


return 0;

Time Complexity: O(n)

Page No. 7
21BCE0427
Chethan N V
Output:

Page No. 8
21BCE0427
Chethan N V

Bubble Sort

08-08-2022

AIM: To implement Bubble sorting technique using array in C language.

ALGORITHM:

void BubbleSort(int A[],int n)

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);

void sort(struct mix l[], int s);


int main()

{
int n;
printf("Enter the record numbers : ");

scanf("%d", &n);

struct mix data[n];


accept(data, n);

sort(data, n);
printf("\nSorted Array: ");
display(data, n);

return 0;
}

void accept(struct mix l[100], int s)


{
int i;

for (i = 0; i < s; i++)


{
Page No. 10
21BCE0427
Chethan N V

printf("\n\nEnter data for Record #%d", i + 1);

printf("\nEnter number : ");

scanf("%d", &l[i].number);
printf("\nEnter string : ");
scanf("%s", &l[i].string);

void display(struct mix l[100], int s)


{

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;

struct mix temp;


for (i = 0; i < s - 1; i++)
{

for (j = 0; j < (s - 1-i); j++)


{

if (l[j].number > l[j + 1].number)

Page No. 11
21BCE0427
Chethan N V

temp = l[j];
l[j] = l[j + 1];

l[j + 1] = temp;
}
}

Time Complexity: O(n2)

Page No. 12
21BCE0427
Chethan N V

Output:

Page No. 13
21BCE0427
Chethan N V

Insertion Sort

08-08-2022

AIM: To implement Insertion sorting technique using array in C language.

Algorithm:
void insertionSort(int A[],int n)

for(i=1;i<n;i++)

j=i-1;

x=A[i];

while(A[j]>x & j>-1)

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];

while(j>-1 && A[j]>x)


{
A[j+1]=A[j];

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("Sorted Array: ");


for(int i=0;i<n;i++)
printf("%f ",A[i]);

printf("\n");

return 0;

Time Complexity: O(n2)

Page No. 16
21BCE0427
Chethan N V

Output:

Page No. 17
21BCE0427
Chethan N V

Selection Sort

08-08-2022

AIM: To implement Selection sorting technique using array in C language.

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;

printf("Enter the size of an array: ");

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

You might also like