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

Untitled document (1)

The document contains various C programming code snippets demonstrating different search and sorting algorithms, including linear search, binary search, selection sort, insertion sort, bubble sort, quicksort, and merge sort. Additionally, it includes implementations of circular, singly, and doubly linked lists with functions to insert elements at different positions. Each code snippet is self-contained and provides basic functionality for the respective algorithm or data structure.

Uploaded by

Matrix Gaming
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Untitled document (1)

The document contains various C programming code snippets demonstrating different search and sorting algorithms, including linear search, binary search, selection sort, insertion sort, bubble sort, quicksort, and merge sort. Additionally, it includes implementations of circular, singly, and doubly linked lists with functions to insert elements at different positions. Each code snippet is self-contained and provides basic functionality for the respective algorithm or data structure.

Uploaded by

Matrix Gaming
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Linear search

#include <stdio.h>
int main()
{
int a[20],b,c=0,d,i;
printf("enter the no. of elements");
scanf("%d",&b);
for(i=0;i<b;i++)
{
scanf("%d",&a[i]);
}

{
scanf("%d",&d);
}
for(i=0;i<b;i++)
{
c=0;
if(a[i]==d)
{
c=1;
break;
}

}
if(c==1)
printf("found");
else
printf(" not found");
}

Binary search
#include <stdio.h>
int main()
{
int arr[30],a,b,i,flag,z=0;
printf("enter the no. of elements");
scanf("%d",&b);
for(i=0;i<b;i++)
{
scanf("%d",&arr[i]);
}
scanf("%d",&a);
flag=0;
if(a==arr[b/2])
{
flag=1;
}
else if(a<arr[b/2])
{
for(i=0;i<arr[(b/2)-1];i++)
{
flag=0;
if(arr[i]==a)
{
flag=1;
break;
}
}
}
else if(a>arr[b/2])
{
for(i=0;i<arr[(b/2)];i++)
{
flag=0;
if(arr[i+(b/2)]==a)
{
flag=1;
break;
}

}
}
if(flag==1)
{
printf("found");
}
else
printf("not found");
}

Selection

#include<stdio.h>
int main()
{
int a,count,ex,i,j;
scanf("%d",&count);
int arr[10]={10,5,2,3,1,9,4};
for(i=0;i<count;i++)
{
for(j=i;j<count;j++)
{
if(arr[j]<arr[i])
{
ex=arr[j];
arr[j]=arr[i];
arr[i]=ex;
}
}
}
for(i=0;i<count;i++)
{
printf("%d",arr[i]);
}
}

Insertion

#include<stdio.h>
int main()
{
int a,count,inc=1,ex,i,k,j;
scanf("%d",&count);
int arr[10]={10,3,7,1,9,2};
for(i=0;i<count-1;i++)
{
for(j=0;j<=inc;j++)
{
for(k=j;k<=inc;k++)
{
if(arr[k]<arr[j])
{
ex=arr[k];
arr[k]=arr[j];
arr[j]=ex;
}
}
}
inc++;
}
for(i=0;i<count;i++)
{
printf("%d",arr[i]);
}
}

Insertion 2

#include<stdio.h>
int main()
{
int a[10],i,n,j,key,temp;
printf("enter how many numbers to enter");
scanf("%d",&n);
printf("enter numbers");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=1;i<n;i++)
{
key=a[i];
for(j=i-1;j>=0;j--)
{
if(a[j]>key)
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
for(i=0;i<n;i++)
{
printf("%d",a[i]);
}
}

Bubble

#include<stdio.h>
int main()
{
int a,count,ex,i,j;
scanf("%d",&count);
int arr[10]={10,5,2,3,1,9,4};
for(i=0;i<count;i++)
{
for(j=0;j<count-1;j++)
{
if(arr[j]>arr[j+1])
{
ex=arr[j];
arr[j]=arr[j+1];
arr[j+1]=ex;
}
}
}
for(i=0;i<count;i++)
{
printf("%d",arr[i]);
}
}

Quick

#include<stdio.h>
#include<conio.h>
void main()
{ void quicksort(int x[],int
left,int right); int a[10],n,i;
printf("enter n");
scanf("%d",&n);
printf("enter %d elements",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
quicksort(a,0,n-1);
for(i=0;i<n;i++)
printf("%d",a[i]);
}
void quicksort(int a[],int left,int right)
{ int l=left,r=right,t; int pivot=left;
while(left<right)
{
while(a[pivot]<=a[right]&&left<right) right--;
if(left!=right)
{
t=a[pivot];
a[pivot]=a[right];
a[right]=t;
pivot=right;
}
while(a[pivot]>=a[left]&&left<right)
left++;
if(left!=right) {
t=a[pivot];
a[pivot]=a[left];
a[left]=t;
pivot=left;
}
}
left=l;right=r;
if(l<pivot)
quicksort(a,left,pivot-1);
if(r>pivot)
quicksort(a,pivot+1,right);
}

Merge sort

#include <stdio.h>
#define max 10
int a[11] = { 10, 14, 19, 26, 27, 31, 33, 35, 42, 44, 0 };
int b[10];
void merging(int low, int mid, int high) {
int l1, l2, i;

for(l1 = low, l2 = mid + 1, i = low; l1 <= mid && l2 <= high; i++) {
if(a[l1] <= a[l2])
b[i] = a[l1++];
else
b[i] = a[l2++];
}

while(l1 <= mid)


b[i++] = a[l1++];

while(l2 <= high)


b[i++] = a[l2++];
for(i = low; i <= high; i++)
a[i] = b[i];
}
void sort(int low, int high) {
int mid;

if(low < high) {


mid = (low + high) / 2;
sort(low, mid);
sort(mid+1, high);
merging(low, mid, high);
} else {
return;
}
}
int main() {
int i;
sort(0, max);
for(i = 0; i <= max; i++)
printf("%d ", a[i]);
}

Circular

#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *link;
};
struct node *inbeg(struct node *tail,int data)
{
struct node *temp=malloc(sizeof(struct node));
temp->data=data;
temp->link=tail->link;
tail->link=temp;
return tail;
}
struct node *inend(struct node *tail,int data)
{
struct node *temp=malloc(sizeof(struct node));
temp->data=data;
temp->link=tail->link;
tail->link=temp;
tail=temp;
return tail;
}
struct node *inpo(struct node *tail,int a,int data)
{
int co;
struct node *temp=malloc(sizeof(struct node));
struct node *pr;
struct node *prr;
pr=tail->link;
while(co!=a)
{
prr=pr;
pr=pr->link;
co++;
}
temp->data=data;
temp->link=prr->link;
prr->link=temp;
return tail;
}

int main()
{

int a;
struct node *tail=malloc(sizeof(struct node));
tail->data=50;
tail->link=tail;

tail=inbeg(tail,40);

tail=inend(tail,60);

scanf("%d",&a);
tail=inpo(tail,a,55);

struct node *pr;


pr=tail->link;

do
{
printf("%d",pr->data);
pr=pr->link;
}while(pr!=tail->link);
}

Single

#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *link;
};
struct node *inbeg(struct node *head,int data)
{
struct node *temp=malloc(sizeof(struct node));
temp->data=data;
temp->link=head;
head=temp;
return head;
};
struct node *inend(struct node *head,int data)
{
struct node *temp=malloc(sizeof(struct node));
temp->data=data;
temp->link=NULL;
struct node *pr;
struct node *prr;
pr=head;
while(pr!=NULL)
{
prr=pr;
pr=pr->link;
}
prr->link=temp;
return head;
}
struct node *inpo(struct node *head,int a,int data)
{
int co;
struct node *temp=malloc(sizeof(struct node));
temp->data=data;
struct node *pr;
struct node *prr;
pr=head;
while(co!=a)
{
prr=pr;
pr=pr->link;
co++;
}
temp->link=prr->link;
prr->link=temp;
return head;
}
int main()
{
int a;
struct node *head=malloc(sizeof(struct node));
head->data=50;
head->link=NULL;

head=inbeg(head,60);
head=inend(head,70);

scanf("%d",&a);
head=inpo(head,a,55);

struct node *pr;


pr=head;
while(pr!=NULL)
{
printf("%d ",pr->data);
pr=pr->link;
}
}

Double

#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *nlink;
struct node *plink;
};
struct node *inbeg(struct node *head,int data)
{
struct node *temp=malloc(sizeof(struct node));
temp->data=data;
temp->nlink=head;
temp->plink=NULL;
head=temp;
return head;
};
struct node *inend(struct node *head,int data)
{
struct node *temp=malloc(sizeof(struct node));
temp->data=data;
temp->nlink=NULL;
struct node *pr;
struct node *prr;
pr=head;
while(pr!=NULL)
{
prr=pr;
pr=pr->nlink;
}
prr->nlink=temp;
temp->plink=prr;
return head;
};
struct node *inpo(struct node *head,int a,int data)
{
int co;
struct node *temp=malloc(sizeof(struct node));
temp->data=data;
struct node *pr;
struct node *prr;
pr=head;
while(co!=a)
{
prr=pr;
pr=pr->nlink;
co++;
}
temp->nlink=prr->nlink;
prr->nlink=temp;
temp->plink=prr;
return head;
}
int main()
{
int a;
struct node *head=malloc(sizeof(struct node));
head->data=50;
head->nlink=NULL;
head->plink=NULL;

scanf("%d",&a);

head=inbeg(head,60);
head=inend(head,70);
head=inpo(head,a,55);
struct node *pr;
pr=head;
while(pr!=NULL)
{
printf("%d ",pr->data);
pr=pr->nlink;
}

You might also like