Академический Документы
Профессиональный Документы
Культура Документы
#include<stdio.h>
#include<conio.h>
int main(void)
{
int n, i, a[50], key,f,l,m;
clrscr();
printf("\nEnter number of elements in the list:");
scanf("%d",&n);
printf("\nEnter array elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\nEnter searching element:");
scanf("%d",&key);
f=0;
l=n-1;
m=(f+l)/2;
while(f<=l)
{
if(a[m]==key)
{
printf("\n%d is found at the location %d",key,m+1);
break;
}
else if(a[m]<key)
f=m+1;
else
l=m-1;
m=(f+l)/2;
}
if(f>l)
printf("\n %d is not found in the list",key);
getch();
return 0;
}
//bubble sort
#include<stdio.h>
#include<conio.h>
int main()
{
int a[100],t,n,i,j;
clrscr();
printf("\nenter the size of the array");
scanf("%d",&n);
printf("enter the number of elements :");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<(n-1);i++)
{
for(j=0;j<(n-i-1);j++)
{
if (a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
printf("\nafter sorting array elements are:");
for(i=0;i<n;i++)
printf("\t%d",a[i]);
getch();
return 0;
}
/* Insertion Sort*/
#include<stdio.h>
#include<conio.h>
void insertionsort(int a[], int n);
int main(void)
{
int n,a[100],i;
clrscr();
printf("Enter array size:");
scanf("%d",&n);
printf("\nEnter array elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
insertionsort(a,n);
printf("\nElements after sorting are");
for(i=0;i<n;i++)
printf("\t%d",a[i]);
getch();
return 0;
}
void insertionsort(int a[], int n)
{
int i,j,key;
for(i=1;i<n;i++)
{
key=a[i];
j=i-1;
while(j>=0&&a[j]>key)
{
a[j+1]=a[j];
j=j-1;
}
a[j+1]=key;
}
}
/*Linear search*/
#include<stdio.h>
#include<conio.h>
int main(void)
{
int n, i, a[50], key,f=0;
clrscr();
printf("\nEnter number of elements in the list:");
scanf("%d",&n);
printf("\nEnter array elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\nEnter searching element:");
scanf("%d",&key);
for(i=0;i<n;i++)
{
if(a[i]==key)
{
f=1;
break;
}
}
if(f==1)
printf("\n%d is found at location %d",key,i+1);
else
printf("\n %d is not found in the list",key);
getch();
return 0;
}
/*Merge Sort*/
#include<stdio.h>
#include<conio.h>
void merge(int a[], int start, int mid, int end);
void mergesort(int a[],int start, int end);
int main(void)
{
int n,a[100],i;
clrscr();
printf("Enter array size:");
scanf("%d",&n);
printf("\nEnter array elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
mergesort(a,0,n-1);
printf("\nElements after sorting are");
for(i=0;i<n;i++)
printf("\t%d",a[i]);
getch();
return 0;
}
void mergesort(int a[],int start, int end)
{
if(start<end)
{
int mid=start+(end-start)/2; printf("\nmid=%d",mid);
mergesort(a,start,mid);
mergesort(a,mid+1,end);
merge(a,start,mid,end);
}
}
void merge(int a[], int start, int mid, int end)
{
int i,j,k;
int n1=mid-start+1;
int n2=end-mid;
int l[50],r[50];
for(i=0;i<n1;i++)
l[i]=a[start+i];
for(j=0;j<n2;j++)
r[j]=a[mid+1+j];
i=0;
j=0;
k=start;
while(i<n1&&j<n2)
{
if(l[i]<=r[j])
{
a[k]=l[i];
i++;
}
else
{
a[k]=r[j];
j++;
}
k++;
}
while(i<n1)
{
a[k]=l[i];
i++;
k++;
}
while(j<n2)
{
a[k]=r[j];
j++;
k++;
}
}
/* Quick Sort*/
#include<stdio.h>
#include<conio.h>
void quicksort(int x[],int l,int h);
int main(void)
{
int n,a[100],i;
clrscr();
printf("Enter array size:");
scanf("%d",&n);
printf("\nEnter array elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
quicksort(a,0,n-1);
printf("\nElements after sorting are");
for(i=0;i<n;i++)
printf("\t%d",a[i]);
getch();
return 0;
}
void quicksort(int x[],int l,int h)
{
int p,i,j,t;
if(l<h)
{
p=l;
i=l;
j=h;
while(i<j)
{
while(x[i]<=x[p]&&i<=h)
i++;
while(x[j]>x[p]&&j>=l)
j--;
if(i<j)
{
t=x[i];
x[i]=x[j];
x[j]=t;
}
/* for(i=0;i<n;i++)
{printf("\t%d",x[i]); }printf("\n"); */
}
t=x[j];
x[j]=x[p];
x[p]=t;
quicksort(x,l,j-1);
quicksort(x,j+1,h);
}
}
/*RADIX sort*/
#include<stdio.h>
// Function to find largest element
int largest(int a[], int n)
{
int large = a[0], i;
for(i = 1; i < n; i++)
{
if(large < a[i])
large = a[i];
}
return large;
}
i = 0;
for(k = 0; k < 10; k++)
{
for(j = 0; j < bucket_count[k]; j++)
{
a[i] = bucket[k][j];
i++;
}
}
divisor *= 10;
for(i = 0; i < n; i++)
printf("%d ",a[i]);
printf("\n");
}
}
#include<stdio.h>
#include<conio.h>
void main()
{
int a[100],n,i,j,min,temp;
clrscr();
for(i=0;i<n-1;i++)
{
min=i;
for(j=i+1;j<n;j++)
{
if(a[min]>a[j])
min=j;
}
if(min!=i)
{
temp=a[i];
a[i]=a[min];
a[min]=temp;
}
}
for(i=front;i<MAX;i++)
printf("\t%d",queue[i]);
for(j=0;j<=rear;j++)
printf("\t%d",queue[j]);
}
else
{
for(i=front;i<=rear;i++)
printf("\t%d",queue[i]);
}
printf("\n");
}
//Queue using arrays
#include<stdio.h>
#include<conio.h>
#define MAX 5
int queue[MAX];
void insertQ();
void deleteQ();
void displayQ();
int front=-1,rear=-1;
void main()
{
int ch;
clrscr();
switch(ch)
{
case 1 : insert();
break;
case 2 : del();
break;
case 3 : display();
break;
case 4 : exit(1);
default: printf("invalid input\n\n");
}
}
}
void insert(void)
{
struct node *temp;
temp=(struct node*)malloc(sizeof(struct node));
printf("enter the data");
scanf("%d",&temp->data);
temp->link = NULL;
if(front == NULL)
{
front = temp;
rear = temp;
}
else
{
rear->link = temp;
rear = temp;
}
}
void del(void)
{
if(front == NULL)
{
printf("\nQueue is empty\ndeletion is not possible\n\n");
}
else
{
struct node *temp;
temp = front;
front = front->link;
printf("\ndeleted element is %d \n",temp->data);
//temp->link=NULL;
free(temp);
}
}
void display(void)
{
if(front == NULL)
{
printf("\nQueue is empty\n\n");
}
else
{
struct node *temp;
temp = front;
while(temp)
{
printf("%d-->",temp->data);
temp = temp->link;
}
}
}
//Single linked list
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *link;
};
struct node *root = NULL;
int len;
void append(void);
void addatbegin(void);
void addatafter(void);
void addatend(void);
int length(void);
void display(void);
void delatafter(void);
void delatbegin(void);
void delatend(void);
void main()
{
int ch;
clrscr();
while(1)
{
printf("Single linked list operatons:\n\n");
printf("1.append\n");
printf("2. addatbegin\n");
printf("3. addatafter\n");
printf("4. addatend\n");
printf("5. Length\n");
printf("6. display\n");
printf("7. deletetbegin\n");
printf("8. deleteatend\n");
printf("9. deleteatafter\n");
printf("10. Quit\n");
root = temp;
}
else
{
struct node *p;
p = root;
while(p->link!=NULL)
{
p = p->link;
}
p->link = temp;
}
}
void addatend()
{
struct node *temp,*p;
temp = (struct node*)malloc(sizeof(struct node));
void addatafter()
{
struct node *temp,*p;
int loc,len,i=1;
printf("enter location: \n");
scanf("%d",&loc);
len=length();
if(loc>len)
{
printf("Invalid location\n");
printf("currently list is having %d nodes",len);
}
else
{
p = root;
while(i<loc)
{
p = p->link;
i++;
}
temp = (struct node*)malloc(sizeof(struct node));
printf("enter node data: \n");
scanf("%d",&temp->data);
temp->link = NULL;
temp->link = p->link; //right
p->link = temp; //left
}
}
void display()
{
struct node *temp;
temp = root;
if(temp == NULL)
{
printf("No nodes in the list\n");
}
else
{
while(temp!=NULL)
{
printf("%d--> ",temp->data);
temp = temp->link;
}
}
}
int length()
{
struct node *temp;
int count = 0;
temp = root;
while(temp!=NULL)
{
count++;
temp = temp->link;
}
return count;
}
void addatbegin()
{
struct node *temp;
temp = (struct node *)malloc(sizeof(struct node));
printf("enter node data: \n");
scanf("%d",&temp->data);
temp->link = NULL;
if(root == NULL)
{
root = temp;
}
else
{
temp->link = root; //right
root = temp; //left
}
}
void delatafter()
{
struct node *temp;
int loc;
switch(ch)
{
case 1 : push();
break;
case 2 : pop();
break;
case 3 : display();
break;
case 4 : exit(1);
default: printf("invalid input\n\n");
}
}
}
void push()
{
struct node* temp;
temp=(struct node*)malloc(sizeof(struct node));
printf("enter node data:");
scanf("%d",&temp->data);
temp->link=top;
top=temp;
}
void pop()
{
struct node* temp;
temp = top;
if(temp==NULL)
{
printf("no element\n");
}
else
{
printf("element we are deleting is %d\n",temp->data);
top=temp->link;
temp->link = NULL;
free(temp);
}
}
void display()
{
struct node* temp;
if(top==NULL)
{
printf("stack is empty:\n");
}
else
{
temp=top;
while(temp!=NULL)
{
printf("%d\n",temp->data);
temp=temp->link;
}
}
}