/*--------------------------------------------------------------------------------------
Program : Implement sorting methods using functions
–
1. Bubble sort
2. Selection sort
3. Insertion sort
4. Shell sort.
Created
By : JABIR DAUD PATHAN
Branch :
COMPUTER ENGINEERING
-----------------------------------------------------------------------------------------*/
/*--------------------<< Header File
>>---------------------*/
#include<stdio.h>
#include<conio.h>
/*-----------<<
Global Declaration of Functions
>>-----------*/
void
accept(void);
void
bubblesort(void);
void
insertionsort(void);
void
selectionsort(void);
void
shellsort(void);
void
display(void);
int
i,j,arr[30],n,temp,ch,k,d; //Global Declaration of variables
/*----<<
function defination for accept array elements >>------*/
void
accept()
{
printf("\n\nHow many elements you want
to enter in array==>");
scanf("%d",&n);
printf("\n\nEnter the
elements==>\n");
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);//accept
array elements from user
}
printf("\naccepted elements
are==>\n");
for(i=0;i<n;i++)
{
printf("\narr[%d]=%d",i,arr[i]);
//display accepted elements
}
}
/*-------<<
function defination for bubble sort >>----------*/
void
bubblesort()
{
printf("\n\nNo of passes required
are==>%d",n-1);
for(i=1;i<n;i++) //used to count no. of
passes
{
for(j=0;j<n-i;j++) //used for comparison
purpose
{
if(arr[j]>arr[j+1]) //condition to
find out smallest number
{
temp=arr[j]; // [we
arr[j]=arr[j+1]; // swapping
arr[j+1]=temp; // values]
}
}
printf("\n\npasses=%d",i);
for(k=0;k<n;k++)
{
printf("\t%d",arr[k]);
//display passes
}
}
}
/*---------<<
function defination for insertion sort >>--------*/
void
insertionsort()
{
for(i=1;i<n;i++) //used to count no. of
passes
{
temp=arr[i]; //assign value of arr[i] to
temp variable
for(j=i-1;j>=0&&arr[j]>temp;j--)
//used for comparison
purpose
{
arr[j+1]=arr[j]; //[swapping
arr[j]=temp; // values]
}
printf("\n\npasses=%d",i);
for(k=0;k<n;k++)
{
printf("\t%d",arr[k]);
//display passes
}
}
}
/*--------<<
function defination for selection sort >>-------*/
void
selectionsort()
{
for(i=0;i<n-1;i++) //used to count no. of
passes
{
k=i;
//assumed i th element is smallest
for(j=i+1;j<n;j++) //used for traverse
the column elements
{
if(arr[j]<arr[k]) //condition to find
out smallest number
{
k=j;
}
}
if(k!=i)
{
temp=arr[i]; // [we
arr[i]=arr[k]; // swapping
arr[k]=temp; //
values]
}
printf("\n\npasses=%d",i+1);
for(k=0;k<n;k++)
{
printf("\t%d",arr[k]);
//display passes
}
}
}
/*-------<<
function defination for shell sort >>--------*/
void
shellsort()
{
for(d=n/2;d>0;d=d/2) //to count no. of
groups(used to divide array)
{
for(i=d;i<n;i++) //used to traverse
right part of array
{
temp=arr[i];
for(j=i;j>=d;j=j-d)//used to traverse
left part of array
{
if(temp<arr[j-d])
{
arr[j]=arr[j-d]; //[swapping
arr[j-d]=temp; // values]
}
else
break;
}
}
printf("\n\ngroups=%d",d);
for(k=0;k<n;k++)
{
printf("\t%d",arr[k]);
//display passes
}
}
}
/*-----<<
function defination for display array elements >>------*/
void
display()
{
printf("\n\nsorted Elements Are==>\n
");
for(i=0;i<n;i++)
{
printf("\narr[%d]=%d",i,arr[i]);
}
}
/*-----------<< Main Function Starts >>------------*/
void
main()
{
char ans; //variable declaration
do
{
clrscr();
printf("\n\t\t\t-------<< MENU
>>-------");
printf("\n\n\t\t\t1.Bubble Sort");
printf("\n\t\t\t2.Insertion Sort");
printf("\n\t\t\t3.Selection Sort");
printf("\n\t\t\t4.Shell Sort");
printf("\n\t\t\t5.exit");
printf("\n\n\t\t\t--------------------------");
printf("\n\t\t\tEnter your
choice==>");
scanf("%d",&ch);//accept choice
from user
switch(ch)
{
case 1 :
accept(); //function call
bubblesort();//function call
display();
break;
case 2 :
accept();
insertionsort();
display();
break;
case 3 :
accept();
selectionsort();
display();
break;
case 4 :
accept();
shellsort();
display();
break;
case 5 :
exit(0);
break;
default:
printf("\n\tInvalid choice");
break;
}
printf("\n\n\tDo you want to
continue(Y/N)?==>");
ans=getch();
}while(ans=='Y'||ans=='y');
getch();
}
/*-----------<< End Of Main Function >>-----------*/
/*-----------------<< OUTPUT
SCREEN >>-------------------*/
-------<< MENU
>>-------
1.Bubble Sort
2.Insertion Sort
3.Selection Sort
4.Shell Sort
5.exit
--------------------------
Enter your
choice==>1
How
many elements you want to enter in array==>6
Enter
the elements==>
5
9
6
2
8
1
accepted
elements are==>
arr[0]=5
arr[1]=9
arr[2]=6
arr[3]=2
arr[4]=8
arr[5]=1
No
of passes required are==>5
passes=1 5
6 2 8
1 9
passes=2 5
2 6 1
8 9
passes=3 2
5 1 6
8 9
passes=4 2
1 5 6
8 9
passes=5 1
2 5 6
8 9
sorted
Elements Are==>
arr[0]=1
arr[1]=2
arr[2]=5
arr[3]=6
arr[4]=8
arr[5]=9
Do you want to continue(Y/N)?==>Y
-------<< MENU
>>-------
1.Bubble Sort
2.Insertion Sort
3.Selection Sort
4.Shell Sort
5.exit
--------------------------
Enter your
choice==>2
How
many elements you want to enter in array==>7
Enter
the elements==>
20
10
8
6
4
2
1
accepted
elements are==>
arr[0]=20
arr[1]=10
arr[2]=8
arr[3]=6
arr[4]=4
arr[5]=2
arr[6]=1
passes=1 10
20 8 6
4 2 1
passes=2 8
10 20 6
4 2 1
passes=3 6
8 10
20 4 2
1
passes=4 4
6 8 10
20 2 1
passes=5 2
4 6 8
10 20 1
passes=6 1
2 4 6
8 10 20
sorted
Elements Are==>
arr[0]=1
arr[1]=2
arr[2]=4
arr[3]=6
arr[4]=8
arr[5]=10
arr[6]=20
Do you want to continue(Y/N)?==>Y
-------<< MENU
>>-------
1.Bubble Sort
2.Insertion Sort
3.Selection Sort
4.Shell Sort
5.exit
--------------------------
Enter your
choice==>3
How
many elements you want to enter in array==>6
Enter
the elements==>
5
9
1
11
2
4
accepted
elements are==>
arr[0]=5
arr[1]=9
arr[2]=1
arr[3]=11
arr[4]=2
arr[5]=4
passes=1 1
9 5 11
2 4
passes=2 1
2 5 11
9 4
passes=3 1
2 4 11
9 5
passes=4 1
2 4 5
9 11
passes=5 1
2 4 5
9 11
sorted
Elements Are==>
arr[0]=1
arr[1]=2
arr[2]=4
arr[3]=5
arr[4]=9
arr[5]=11
Do you want to continue(Y/N)?==>Y
-------<< MENU
>>-------
1.Bubble Sort
2.Insertion Sort
3.Selection Sort
4.Shell Sort
5.exit
--------------------------
Enter your
choice==>4
How
many elements you want to enter in array==>8
Enter
the elements==>
5
1
9
8
2
4
6
9
accepted
elements are==>
arr[0]=5
arr[1]=1
arr[2]=9
arr[3]=8
arr[4]=2
arr[5]=4
arr[6]=6
arr[7]=9
groups=4 2
1 6 8
5 4 9
9
groups=2 2
1 5 4
6 8 9
9
groups=1 1
2 4 5
6 8 9
9
sorted
Elements Are==>
arr[0]=1
arr[1]=2
arr[2]=4
arr[3]=5
arr[4]=6
arr[5]=8
arr[6]=9
arr[7]=9
Do you want to continue(Y/N)?==>Y
-------<< MENU
>>-------
1.Bubble Sort
2.Insertion Sort
3.Selection Sort
4.Shell Sort
5.exit
--------------------------
Enter your choice==>5
No comments:
Post a Comment