Вы находитесь на странице: 1из 25

/* BINARY SEARCH*/

#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;
}

// Function to perform sorting


void RadixSort(int a[], int n)
{
int bucket[10][10], bucket_count[10];
int i, j, k, remainder, NOP=0, divisor=1, large, pass;

large = largest(a, n);


printf("The large element %d\n",large);
while(large > 0)
{
NOP++;
large/=10;
}

for(pass = 0; pass < NOP; pass++)


{
for(i = 0; i < 10; i++)
{
bucket_count[i] = 0;
}
for(i = 0; i < n; i++)
{
remainder = (a[i] / divisor) % 10;
bucket[remainder][bucket_count[remainder]] = a[i];
bucket_count[remainder] += 1;
}

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

//program starts here


int main()
{
int i, n, a[10];
printf("Enter the number of elements :: ");
scanf("%d",&n);
printf("Enter the elements :: ");
for(i = 0; i < n; i++)
{
scanf("%d",&a[i]);
}
RadixSort(a,n);
printf("The sorted elements are :: ");
for(i = 0; i < n; i++)
printf("%d ",a[i]);
printf("\n");
return 0;
}
//c Program code for Selection Sort

#include<stdio.h>
#include<conio.h>

void main()
{
int a[100],n,i,j,min,temp;
clrscr();

printf("\n Enter the Number of Elements: ");


scanf("%d",&n);

printf("\n Enter %d Elements: ",n);


for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}

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

printf("\n The Sorted array in ascending order: ");


for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
getch();
}
//Circular 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();

printf("circular queue operrations using array");


printf("\n____________________________________");
printf("\n1. Insert\n2.delete\n3. display\n4. quit\n");
do
{
printf("\nenter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: insertQ();
break;
case 2: deleteQ();
break;
case 3: displayQ();
break;
case 4: printf("exit point\n");
break;
default: printf("Enter a valid choice");
}
}while(ch!=4);
getch();
}
void insertQ()
{
int data;
if((front==0&&rear==MAX-1)||(rear+1==front))
{
printf("\ncircular queue is full");
}
else
{
printf("enter data to be inserted");
scanf("%d",&data);
if(rear==-1)
{
front=0;
rear=0;
}
else if(rear==MAX-1)
rear=0;
else
rear++;
queue[rear]= data;
}
}
void deleteQ()
{
if(front==-1)
{
printf("\n queue is empty");
}
else
{
if(front==rear)
{
front=-1;
rear=-1;
}
if(front==MAX-1)
front=0;
printf("\n deleted element from the queue is %d",queue[front]);
front++;
}
}
void displayQ()
{
int i,j;
if(front==-1&&rear==-1)
{
printf("\nqueue is empty");
}
else if(front>rear)
{

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

printf("queue operrations using array");


printf("\n____________________________");
printf("\n1. Insert\n2.delete\n3. display\n4. quit\n");
do
{
printf("\nenter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: insertQ();
break;
case 2: deleteQ();
break;
case 3: displayQ();
break;
case 4: printf("exit point\n");
break;
default: printf("Enter a valid choice");
}
}while(ch!=4);
getch();
}
void insertQ()
{
int data;
if(rear==MAX-1)
{
printf("\nLinear queue is full");
}
else
{
if(front==-1)
front=0;
printf("enter data");
scanf("%d",&data);
rear++;
queue[rear]= data;
}
}
void deleteQ()
{
if(front==-1||front>rear)
{
printf("\n queue is empty");
}
else
{
printf("\n deleted element from the queue is %d",queue[front]);
front++;
}
}
void displayQ()
{
int i;
if(front==-1||front>rear)
{
printf("\nqueue is empty");
}
else
{
printf("\n elements in the queue are\n");
for(i=front;i<=rear;i++)
{
printf("%d\t",queue[i]);
}
}
}
/* Queue operations using single linked list */
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node* link;
};
struct node* front = NULL,*rear = NULL;
void insert(void);
void del(void);
void display(void);
void main()
{
int ch;
clrscr();
while(1)
{
printf("\nqueue using single linked list operation\n\n");
printf("1.Insert\n");
printf("2.Delete\n");
printf("3.Display\n");
printf("4.Quit\n");

printf("enter your choice: ");


scanf("%d",&ch);

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

printf("enter your choice: ");


scanf("%d",&ch);
switch(ch)
{
case 1: append();
break;
case 2: addatbegin();
break;
case 3: addatafter();
break;
case 4: addatend();
break;
case 5: len = length();
printf("Number of nodes in the list are %d",len);
break;
case 6: display();
break;
case 7: delatbegin();
break;
case 8: delatend();
break;
case 9: delatafter();
break;
case 10: exit(1);
default: printf("Invalid Choice");
}
}
// getch();
}
void append()
{
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) //list is empty


{

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

printf("enter node data: \n");


scanf("%d",&temp->data);
temp->link = NULL;
p = root;
while(p->link!=NULL)
{
p = p->link;
}
p->link = temp;
}

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;

printf("enter location to delete: \n");


scanf("%d",&loc);
if(loc>length())
{
printf("invalid location");
}
else if(loc==1)
{
temp = root;
root = temp->link;
temp->link = NULL;
free(temp);
}
else
{
struct node *p=root,*q;
int i=1;
while(i<loc-1)
{
p = p->link;
i++;
}
q = p->link;
p->link = q->link;
q->link = NULL;
free(temp);
}
}
void delatbegin()
{
struct node *temp;
temp = root;
root = temp->link;
temp->link = NULL;
free(temp);
}
void delatend()
{
struct node *temp,*prev;
if(root==NULL)
{
printf("\nEmpty list");
}
else
{
temp = root;
prev = root;
while(temp->link!=NULL)
{
prev = temp;
temp = temp->link;
}
prev->link = NULL;
free(temp);
}
}
/*Stack operations using arrays*/
#include<stdio.h>
#include<conio.h>
int stack[100],choice,n,top,x,i;
void push();
void pop();
void display();
void main()
{
clrscr();
top=-1;
printf("enter the size of the stack");
scanf("%d",&n);
printf("Stack operations using arrays\n");
printf("______________________________\n");
printf("1. PUSH\n2. POP\n3. DISPLAY\n4. EXIT\n");
do
{
printf("\nenter the choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1: push();
break;
case 2: pop();
break;
case 3: display();
break;
case 4: printf("\n exit point");
break;
default: printf("please a valid choice\n");
}
}while(choice!=4);
getch();
}
void push()
{
if(top>=n-1)
{
printf("stack is overflow");
}
else
{
printf("enter a value to be pushed");
scanf("%d",&x);
top++;
stack[top]=x;
}
}
void pop()
{
if(top<=-1)
{
printf("stack is underflow");
}
else
{
printf("the popped element is %d",stack[top]);
top--;
}
}
void display()
{
if(top>=0)
{
printf("the elements in the stack are\n");
for(i=top;i>=0;i--)
printf("\n%d",stack[i]);
printf("\npress next choice");
}
else
{
printf("\n the stack is empty");
}
}
/* stack using single linked list */
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node* link;
};
struct node* top = NULL;
void push(void);
void pop(void);
void display(void);
void main()
{
int ch;
clrscr();
while(1)
{
printf("\nstack using single linked list operation\n\n");
printf("1.push\n");
printf("2.pop\n");
printf("3.Display\n");
printf("4.Quit\n");

printf("enter your choice: ");


scanf("%d",&ch);

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

Вам также может понравиться