#include<stdio.h>
#include<conio.h>
void sorting();
void selection_sort();
void bubble_sort();
void insertion_sort();
int arr[10],temp,i,c,j;
void main()
{
int ch;
char ch2;
clrscr();
do
{
printf("\n\t\tFor sorting press 1:");
scanf("%d",&ch);
switch(ch)
{
case 1:
sorting();
break;
default:
printf("\n\t\tYou have enter wrong choice\n");
}
printf("\n\t\tDo you want to continue:(y/n)\n");
scanf("%s",&ch2);
}
while(ch2=='y'||ch=='Y');
}
/*function for sorting*/
void sorting()
{
int ch;
char ch1;
do
{
printf("\t\tWhich sorting you want to do\n");
printf("\t\t1:Bubble sort\n");
printf("\t\t2:Insertion sort\n");
printf("\t\t3:Selection sort\n");
printf("\t\tEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nEnter limit not more then 10: ");
scanf("%d",&c);
if(c>10)
{printf("\n\t\tWrong Input");sorting();}
else
bubble_sort();
break;
case 2: printf("\nEnter limit not more then 10: ");
scanf("%d",&c);
if(c>10)
{printf("\n\t\tWrong Input");sorting();}
else
insertion_sort();
break;
case 3: printf("\nEnter limit not more then 10: ");
scanf("%d",&c);
if(c>10)
{printf("\n\t\tWrong Input");sorting();}
else
selection_sort();
default:
printf("\n\t\t\tYou have enter wrong choice\n");
}
printf("\n\t\t\tDo you want to continue:(y/n) ");
scanf("%s",&ch1);
}while(ch1=='y');
}
/*function for bubble sort*/
void bubble_sort()
{
printf("\n\t\tEnter element in array\n");
for(i=0;i<=c;i++)
{
scanf("%d ",&arr[i]);
}
printf("\n\t\tTthe unsorted array is:\n");
for(i=0;i<=c;i++)
{
printf("%d ,",arr[i]);
}
for(i=0;i<=c;i++)
{
for(j=0;j<=c-1;j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
printf("\n\t\t The sorted array is:\n");
for(i=0;i<=c;i++)
{
printf("%d ,",arr[i]);
}
}
/*function for insertion sort*/
void insertion_sort()
{
printf("\n\t\tEnter elements in array\n");
for(i=0;i<=c;i++)
{
scanf("%d",&arr[i]);
}
printf("\n\t\tThe unsorted array is:\n");
for(i=0;i<=c;i++)
{
printf("%d ,",arr[i]);
}
for(i=1;i<=c;i++)
{
temp=arr[i];
j=i-1;
while((temp<arr[j])&&(j>=0))
{
arr[j+1]=arr[j];
j=j-1;
}
arr[j+1]=temp;
}
printf("\n\t\tSorted array is:\n");
for(i=0;i<=c;i++)
{
printf("%d ,",arr[i]);
}
}
/*function for selection sort*/
void selection_sort()
{
printf("\n\t\tEnter elements in array\n");
for(i=0;i<=c;i++)
{
scanf("%d",&arr[i]);
}
printf("\n\t\tUnsorted array is\:n");
for(i=0;i<=c;i++)
{
printf("%d ,",arr[i]);
}
for(i=0;i<c-1;i++)
{
for(j=i+1;j<=c;j++)
{
if(arr[i]>arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
printf("\n\t\tSorted array is:\n");
for(i=0;i<=c;i++)
{
printf("%d ,",arr[i]);
}
}
#include<conio.h>
void sorting();
void selection_sort();
void bubble_sort();
void insertion_sort();
int arr[10],temp,i,c,j;
void main()
{
int ch;
char ch2;
clrscr();
do
{
printf("\n\t\tFor sorting press 1:");
scanf("%d",&ch);
switch(ch)
{
case 1:
sorting();
break;
default:
printf("\n\t\tYou have enter wrong choice\n");
}
printf("\n\t\tDo you want to continue:(y/n)\n");
scanf("%s",&ch2);
}
while(ch2=='y'||ch=='Y');
}
/*function for sorting*/
void sorting()
{
int ch;
char ch1;
do
{
printf("\t\tWhich sorting you want to do\n");
printf("\t\t1:Bubble sort\n");
printf("\t\t2:Insertion sort\n");
printf("\t\t3:Selection sort\n");
printf("\t\tEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nEnter limit not more then 10: ");
scanf("%d",&c);
if(c>10)
{printf("\n\t\tWrong Input");sorting();}
else
bubble_sort();
break;
case 2: printf("\nEnter limit not more then 10: ");
scanf("%d",&c);
if(c>10)
{printf("\n\t\tWrong Input");sorting();}
else
insertion_sort();
break;
case 3: printf("\nEnter limit not more then 10: ");
scanf("%d",&c);
if(c>10)
{printf("\n\t\tWrong Input");sorting();}
else
selection_sort();
default:
printf("\n\t\t\tYou have enter wrong choice\n");
}
printf("\n\t\t\tDo you want to continue:(y/n) ");
scanf("%s",&ch1);
}while(ch1=='y');
}
/*function for bubble sort*/
void bubble_sort()
{
printf("\n\t\tEnter element in array\n");
for(i=0;i<=c;i++)
{
scanf("%d ",&arr[i]);
}
printf("\n\t\tTthe unsorted array is:\n");
for(i=0;i<=c;i++)
{
printf("%d ,",arr[i]);
}
for(i=0;i<=c;i++)
{
for(j=0;j<=c-1;j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
printf("\n\t\t The sorted array is:\n");
for(i=0;i<=c;i++)
{
printf("%d ,",arr[i]);
}
}
/*function for insertion sort*/
void insertion_sort()
{
printf("\n\t\tEnter elements in array\n");
for(i=0;i<=c;i++)
{
scanf("%d",&arr[i]);
}
printf("\n\t\tThe unsorted array is:\n");
for(i=0;i<=c;i++)
{
printf("%d ,",arr[i]);
}
for(i=1;i<=c;i++)
{
temp=arr[i];
j=i-1;
while((temp<arr[j])&&(j>=0))
{
arr[j+1]=arr[j];
j=j-1;
}
arr[j+1]=temp;
}
printf("\n\t\tSorted array is:\n");
for(i=0;i<=c;i++)
{
printf("%d ,",arr[i]);
}
}
/*function for selection sort*/
void selection_sort()
{
printf("\n\t\tEnter elements in array\n");
for(i=0;i<=c;i++)
{
scanf("%d",&arr[i]);
}
printf("\n\t\tUnsorted array is\:n");
for(i=0;i<=c;i++)
{
printf("%d ,",arr[i]);
}
for(i=0;i<c-1;i++)
{
for(j=i+1;j<=c;j++)
{
if(arr[i]>arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
printf("\n\t\tSorted array is:\n");
for(i=0;i<=c;i++)
{
printf("%d ,",arr[i]);
}
}
No comments:
Post a Comment