This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
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 8Sorted Array is:23578
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 3Enter element to be searched: 5Number is found at 1 position
Enter 5 element in Array: 1 5 6 8 3Enter element to be searched: 2Number 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,9Enter element you want to search: 44 is found at position 1
The numbers are: 1,4,6,8,9Enter element you want to search: 2Element 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: 4Enter the elements of array: 1 2 3 4On which position element do you want to delete: 34 is deletedNew array is123
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 7Sorted array:12578
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:-53421
The sorted array is:-12345
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: 4Factorial 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");
elseif(n==1)printf("\nMove disk from peg %c to peg %c",A,C);
This program shows the solution of tower of HanoiEnter number of digits: 3
Move disk from peg A to peg BMove disk from peg A to peg CMove disk from peg B to peg CMove disk from peg A to peg CMove disk from peg A to peg BMove disk from peg A to peg CMove disk from peg B to peg C
Queue
# include<stdio.h># include<conio.h># include<stdlib.h># define max 10int 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
1
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
3
5
Enter which operation do you want to perform 1 To insert 2 To delete 3 To display 4 To exit
2
5 is deleted
Enter which operation do you want to perform 1 To insert 2 To delete 3 To display 4 To exit
5
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 10int 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 perform1 To push2 To pop3 To display4 To exit
1
Enter number to be inserted 5
Enter which operation do you want to perform1 To push2 To pop3 To display4 To exit
3
5
Enter which operation do you want to perform1 To push2 To pop3 To display4 To exit
2
5 is deleted
Enter which operation do you want to perform1 To push2 To pop3 To display4 To exit
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 perform1 To insert at beginning2 To insert at end3 To insert at given position4 To delete at beginning5 To delete at end6 To delete at given position7 To display8 To exit
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 perform1 To insert at beginning2 To insert at end3 To insert at given position4 To delete at beginning5 To delete at end6 To delete at given position7 To display8 To exit
1
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);