Sunteți pe pagina 1din 39

Selection Sort

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

void main()
{
int a[5], i, j, temp;
clrscr();

printf("Enter 5 elements of Array: ");


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

for(i=0;i<5;i++)
{
for(j=i;j<4;j++)
{
if(a[i]>a[j+1])
{
temp=a[i];
a[i]=a[j+1];
a[j+1]=temp;
}
}
}

printf("Sorted Array is: ");

for(i=0;i<5;i++)
{
printf("\n%d",a[i]);
}

getch();
}
Output

Enter 5 elements of Array: 2 5 7 3 8


Sorted Array is:
2
3
5
7
8
Linear Search

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

void main()
{
int a[5],sno,i,f=0;
clrscr();

printf("Enter 5 element in Array: ");


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

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


scanf("%d",&sno);

for(i=0;i<5;i++)
{
if(sno==a[i])
{
printf("Number is found at %d position",i);
f=1;
}
}

if(f==0)
{
printf("Number is not found");
}

getch();
}
Output

Enter 5 element in Array: 1 5 6 8 3


Enter element to be searched: 5
Number is found at 1 position

Enter 5 element in Array: 1 5 6 8 3


Enter element to be searched: 2
Number is not found
Binary Search

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

void main()
{
int a[5]={1,4,6,8,9};
int item, loc, beg=1,end=5,mid;

printf("The numbers are: 1,4,6,8,9");


printf("\nEnter element you want to search: ");
scanf("%d",&item);

mid=(beg+end)/2;

while(beg<=end && a[mid]!=item)


{
if(item<a[mid])
{
end=mid-1;
}
else
{
beg=mid+1;
}
mid=(beg+end)/2;
}

if(a[mid]==item)
{
loc=mid;
printf("%d is found at position %d",item,loc);
}
else
{
loc=NULL;
printf("Element is not found");
}

getch();
}
Output

The numbers are: 1,4,6,8,9


Enter element you want to search: 4
4 is found at position 1

The numbers are: 1,4,6,8,9


Enter element you want to search: 2
Element is not found
Deletion in Array

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

int i, len;

void main()
{
int a[100], pos;
void del(int a[], int, int);
clrscr();

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


scanf("%d",&len);

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


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

printf(“On which position element do you want to delete: ”);


scanf("%d",&pos);
del(a, pos, len);

getch();
}

void del(int a[],int pos,int len)


{
int j,item;
item=a[pos];

for(j=pos;j<=len;j++)
{
a[j]=a[j+1];
}
len=len-1;

printf("%d is deleted\n",item);

printf(“New array is\n”);


for(i=0;i<=len-1;i++)
{
printf("%d\n",a[i]);
}
}
Output

Enter the length of array: 4


Enter the elements of array: 1 2 3 4
On which position element do you want to delete: 3
4 is deleted
New array is
1
2
3
Bubble Sort

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

void main()
{
int a[5], i, j, temp;
clrscr();

printf("Enter 5 elements of array: ");


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

for(i=0;i<5;i++)
{
for(j=0;j<4;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}

printf("Sorted array: ");


for(i=0;i<5;i++)
{
printf("\n%d",a[i]);
}

getch();
}
Output

Enter 5 elements of array: 1 8 5 2 7


Sorted array:
1
2
5
7
8
Insertion Sort

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

void insertionsort(int a[],int n)


{
int i,j,temp;

for(i=1;i<n;i++)
{
temp=a[i];
j=i-1;

while(temp<a[j] && j>=0)


{
a[j+1]=a[j];
j=j-1;
}

a[j+1]=temp;
}
}

void main()
{
int i,j,a[10],n;
clrscr();

printf("\n How many elements you want to insert in array:-");


scanf("%d",&n);

printf("\n Enter the elements of array:-");


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

insertionsort(a,n);

printf("\n The sorted array is:-");


for(i=0;i<n;i++)
printf("\n%d",a[i]);

getch();
}
Output

How many elements you want to insert in array:-5

Enter the elements of array:-


5
3
4
2
1

The sorted array is:-


1
2
3
4
5
Recursion

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

int fact(int n)
{
int x,y;
if(n==1)
return(n);
x=n-1;
y=fact(x);
return(n*y);
}

void main()
{
int n;

printf("Enter a Number: ");


scanf("%d",&n);

int z=fact(n);
printf("Factorial of a %d is %d",n,z);

getch();
}
Output

Enter a Number: 4
Factorial of a 4 is 24
Tower of Hanoi

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

void main()
{
void toh(int,char,char,char);
int n;

printf("This program shows the solution of tower of hanoi\n");


printf("Enter number of digits: ");
scanf("%d",&n);

toh(n,'A','B','C');

getch();
}

void toh(int n,char A,char B,char C)


{
if(n<0)
printf("Incorrect input");
else
if(n==1)
printf("\nMove disk from peg %c to peg %c",A,C);
else
{
toh(n-1,'A','C','B');
toh(1,'A','B','C');
toh(n-1,'B','A','C');
}
}
Output

This program shows the solution of tower of Hanoi


Enter number of digits: 3

Move disk from peg A to peg B


Move disk from peg A to peg C
Move disk from peg B to peg C
Move disk from peg A to peg C
Move disk from peg A to peg B
Move disk from peg A to peg C
Move disk from peg B to peg C
Queue

# include<stdio.h>
# include<conio.h>
# include<stdlib.h>
# define max 10
int que[max];
int front=0,rear=-1;

void insert()
{
int n;

if(rear>max-1)
{
printf("que is full");
}
else
{
printf("\nEnter number to be inserted ");
scanf("%d",&n);
rear++;
que[rear]=n;
}
}

void delet()
{
int temp;

if(front>rear)
{
printf("\nque is empty");
}
else
{
temp=que[front];
front++;
printf("\n%d is deleted",temp);
}
}

void display()
{
int i;

for(i=rear;i>=front;i--)
{
printf("\n%d",que[i]);
}
}

void main()
{
int ch;

while(1)
{
printf("\n\nEnter which operation do you want to perform \n1 To insert \n2
To delete \n3 To display \n4 To exit\n\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
insert();
break;
case 2:
delet();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("\n\nInvalid choice");
break;
}
}
}
Output

Enter which operation do you want to perform


1 To insert
2 To delete
3 To display
4 To exit

Enter number to be inserted 5

Enter which operation do you want to perform


1 To insert
2 To delete
3 To display
4 To exit

Enter which operation do you want to perform


1 To insert
2 To delete
3 To display
4 To exit

5 is deleted

Enter which operation do you want to perform


1 To insert
2 To delete
3 To display
4 To exit

Invalid choice

Enter which operation do you want to perform


1 To insert
2 To delete
3 To display
4 To exit
Stack

# include<stdio.h>
# include<conio.h>
# include<stdlib.h>
# define max 10
int stack[max];
int top=-1;

void push()
{
int n;

if(top>max-1)
{
printf("\nstack overflow occurs");
}
else
{
printf("\nEnter number to be inserted");
scanf("%d",&n);
top++;
stack[top]=n;
}
}

void pop()
{
int temp;

if(top==-1)
{
printf("\nstack is empty");
}
else
{
temp=stack[top];
printf("\n%d is deleted",temp);
top--;
}
}

void display()
{
int i;

for(i=top;i>=0;i--)
{
printf("\n\n%d",stack[i]);
}
}

void main()
{
int stack[max],top=-1,ch;

while(1)
{
printf("\n\nEnter which operation do you want to perform\n1 To push\n2 To
pop\n3 To display\n4 To exit\n\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("\n\nInvalid choice");
break;
}
}
}
Output

Enter which operation do you want to perform


1 To push
2 To pop
3 To display
4 To exit

Enter number to be inserted 5

Enter which operation do you want to perform


1 To push
2 To pop
3 To display
4 To exit

Enter which operation do you want to perform


1 To push
2 To pop
3 To display
4 To exit

5 is deleted

Enter which operation do you want to perform


1 To push
2 To pop
3 To display
4 To exit
Circular link list

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

struct clist
{
int data;

struct clist *next;


};

struct clist *end=NULL;

void insertatbeg()
{
struct clist *p;
int num;

p=(struct clist *)malloc(sizeof(struct clist));

printf("\n\nEnter number: ");


scanf("%d",&num);

p->data=num;
p->next=end->next;
end->next=p;
}

void insertatend()
{
struct clist *p;
int num;

p=(struct clist *)malloc(sizeof(struct clist));

printf("\n\nEnter number: ");


scanf("%d",&num);

p->data=num;
p->next=end->next;
end->next=p;
end=p;
}

void insertatpos()
{
int num,i=1,pos;
struct clist *p,*temp;

p=(struct clist *)malloc(sizeof(struct clist));

printf("\n\nEnter the position");


scanf("%d",pos);

printf("\n\nEnter number: ");


scanf("%d",&num);

temp=end->next;
while(i<pos-1)
{
temp=temp->next;
i++;
}

p->data=num;
p->next=temp->next;
temp->next=p;
}

void deletatbeg()
{
struct clist *temp,*prv;

temp=end->next;
prv=temp->next;
end->next=prv;
temp->next=NULL;

printf("\n\n%d is deleted",temp->data);
free(temp);
}

void deletatend()
{
struct clist *temp;
temp=end->next;
while(temp->next!=end)
{
temp=temp->next;
}

temp->next=end->next;

printf("\n\n%d is deleted",end->data);
end->next=NULL;
free(end);
end=temp;
}
void deletatpos()
{
struct clist *temp,*prv;
int pos,i=1,num;

printf("\n\nEnter position");
scanf("%d",&pos);

printf("\n\nEnter number: ");


scanf("%d",&num);

temp=end->next;

while(i<pos-1)
{
temp=temp->next;
i++;
}

prv=temp->next;
temp->next=prv->next;

printf("\n\n%d is deleted",prv->data);

prv->next=NULL;
free(prv);
}

void display()
{
struct clist *temp;

temp=end->next;

if(end!=NULL)
{
while(temp->next!=end)
{
printf("\n\n%d",temp->data);
temp=temp->next;
}
printf("\n%d",temp->data);
}
else
{
printf("\n\nList is empty");
}
}
void main()
{
int ch;
struct clist *p;
int num;

p=(struct clist *)malloc(sizeof(struct clist));

printf("\n\nEnter number");
scanf("%d",&num);

p->data=num;
p->next=p;
end=p;

while(1)
{
printf("\n\nEnter which operation do you want to perform\n1 To insert at
begining\n2 To insert at end\n3 To insert at given position\n4 To delete at
begining\n5 To delete at end\n6 To delete at given position\n7 To
display\n8 To exit\n\n");
scanf("%d",&ch);

switch(ch)
{
case 1:
insertatbeg();
break;
case 2:
insertatend();
break;
case 3:
insertatpos();
break;
case 4:
deletatbeg();
break;
case 5:
deletatend();
break;
case 6:
deletatpos();
break;
case 7:
display();
break;
case 8:
exit(0);
default:
printf("\n\nInvalid choice");
break;
}
}
}

Output
Enter Number: 2

Enter which operation do you want to perform


1 To insert at beginning
2 To insert at end
3 To insert at given position
4 To delete at beginning
5 To delete at end
6 To delete at given position
7 To display
8 To exit

Enter Number:

Simple link list


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

struct list
{
int data;
struct list *next;
};

struct list *start=NULL;

void insertatbeg()
{
struct list *p;
int num;

p=(struct list *)malloc(sizeof(struct list));

printf("enter number");
scanf("%d",&num);

p->data=num;
p->next=start;
start=p;
}

void insertatend()
{
struct list *p,*temp;
int num;

p=(struct list *)malloc(sizeof(struct list));

printf("enter number");
scanf("%d",&num);

temp=start;

while(temp->next!=NULL)
{
temp=temp->next;
}

p->data=num;
temp->next=p;
p->next=NULL;
}
void insertatpos()
{
int num,i=1,pos;
struct list *p,*temp;

p=(struct list *)malloc(sizeof(struct list));

printf("Enter the position");


scanf("%d",&pos);

printf("Enter number");
scanf("%d",&num);

temp=start;

while(i<pos-1)
{
temp=temp->next;
i++;
}

p->data=num;
p->next=temp->next;
temp->next=p;
}

void deletatbeg()
{
struct list *temp;
temp=start;
start=start->next;
temp->next=NULL;

printf("%d is deleted",temp->data);
free(temp);
}

void deletatend()
{
struct list *temp,*prv;
temp=start;

while(temp->next!=NULL)
{
prv=temp;
temp=temp->next;
}

prv->next=NULL;
printf("%d is deleted",temp->data);
free(temp);
}

void deletatpos()
{
struct list *temp,*prv;
int pos,i=1;

printf("Enter position");
scanf("%d",&pos);

temp=start;

while(i<pos-1)
{
temp=temp->next;
i++;
}

prv=temp->next;
temp->next=prv->next;

printf("%d is deleted",prv->data);
free(prv);
}

void display()
{
struct list *temp;
temp=start;

if(start!=NULL)
{
while(temp->next!=NULL)
{
printf("\n%d",temp->data);
temp=temp->next;
}
printf("\n%d",temp->data);
}
else
{
printf("List is empty");
}
}

void main()
{
int ch;
struct list *p;
int num;
p=(struct list *)malloc(sizeof(struct list));

printf("Enter number");
scanf("%d",&num);

p->data=num;
p->next=NULL;
start=p;

while(1)
{
printf("\nEnter which operation do you want to perform\n1 To insert at
begining\n2 To insert at end\n3 To insert at given position\n4 To delete at
begining\n5 To delete at end\n6 To delete at given position\n7 To
display\n8 To exit");
scanf("%d",&ch);

switch(ch)
{
case 1:
insertatbeg();
break;
case 2:
insertatend();
break;
case 3:
insertatpos();
break;
case 4:
deletatbeg();
break;
case 5:
deletatend();
break;
case 6:
deletatpos();
break;
case 7:
display();
break;
case 8:
exit(0);
default:
printf("\nInvalid choice");
break;
}
}
}

Output
Enter Number: 2

Enter which operation do you want to perform


1 To insert at beginning
2 To insert at end
3 To insert at given position
4 To delete at beginning
5 To delete at end
6 To delete at given position
7 To display
8 To exit

Enter Number:
Insertion in array

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

int i, len, pos, num;

void main()
{
int a[100]: void insert(int a[],int,int,int);
clrscr();

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


scanf("%d",&len);

printf("Enter integers: ");


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

printf("Enter integer to be inserted: ");


scanf("%d",&num);

printf("Enter position in the array for insertion: ");


scanf("%d",&pos);

--pos;
insert(a,len,pos,num);
}

void insert(int a[],int len,int pos,int num)


{
if(pos>len)
{
printf("Insertion outside the array: ");
}
else
{
for(i=len;i>=pos;i--)
{
a[i+1]=a[i];
}

a[pos]=num;
len++;

printf("New array is: \n");


for(i=0;i<len;i++)
{
printf("%d",a[i]);
}
}
}

Output

Enter the length of array: 6


Enter Integers:
2
4
7
9
11
12

Enter integer to be inserted: 5

Enter position in the array for insertion: 3

New array:
2
4
5
7
9
11
12

Quick Sort

#include<stdio.h>
#include<conio.h>
void quicksort(int a[],int l,int h)
{
int temp,low,high,key;
low=l;
high=h;
key=a[(low+high)/2];

while(low<=high)
{
while(a[low]<key)
low++;

while(a[high]>key)
high--;

if(low<=high)
{
temp=a[low];
a[low]=a[high];
a[high]=temp;
low++;
high--;
}
}

if(l<high)
quicksort(a,l,high);

if(low<h)
quicksort(a,low,h);
}

void main()
{
int i,j,a[10],n,l,h;
clrscr();

printf("\n How many elements you want to enter in array:-");


scanf("%d",&n);

printf("\n Enter the elements of array:-");


for(i=0;i<n;i++)
scanf("\n%d",&a[i]);
l=0;
h=n-1;

quicksort(a,l,h);

printf("\n The sorted array is:-");


for(i=0;i<n;i++)
printf("\n %d",a[i]);

getch();
}

Output

How many elements you want to enter in array:-5

Enter the elements of array:-


5
3
4
2
1

The sorted array is:-


1
2
3
4
5

S-ar putea să vă placă și