Sie sind auf Seite 1von 3

/*SE DIV 1 ROLL NO :204017 AISHWARYA NAMBIAR

/*SORTING USING FUNCTION


#include<stdio.h>
void bubble(int a[],int n)
{
int pass,i,temp,j;
for(pass=0;(pass<n-1);pass++)
{
printf("\narray after pass %d\n",pass);
for(j=0;j<n;j++)
printf("%d\t",a[j]);
for(i=0;i<n-pass-1;i++)
{
if(a[i]>a[i+1])
{
temp=a[i];
a[i]=a[i+1];
a[i+1]=temp;
}
}
}
}
void selection(int a[], int n)
{
int i,large,ind,j;
for(i=n-1;i>=0;i--)
{
printf("\narray after pass %d\n",n-i-1);
for(j=0;j<n;j++)
printf("%d\t",a[j]);
large=a[0];
ind=0;
for(j=0;j<=i;j++)
{
if(a[j]>large)
{
large=a[j];
ind=j;
}
}
a[ind]=a[i];
a[i]=large;
}
}
void insertion(int a[],int n)
{
int i,temp,j;
for(i=1;i<n;i++)
{
printf("\narray after pass %d\n",i-1);
for(j=0;j<n;j++)
printf("%d\t",a[j]);
temp=a[i];
for(j=i-1;j>=0&&temp<a[j];j--)
a[j+1]=a[j];
a[j+1]=temp;
}
}
void main()
{
int a[10],n,op,ch,i,j;
clrscr();
do
{
ch=0;
printf("enter size of array");
scanf("%d",&n);
printf("enter array elements");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("select a sorting method\n1.bubble sort\n2.selection sort\n3.insertion
sort");
scanf("%d",&op);
switch(op)
{
case 1:bubble(a,n);
break;
case 2:selection(a,n);
break;
case 3:insertion(a,n);
break;
}
printf("\nsorted array is\n");
for(j=0;j<n;j++)
printf("%d\t",a[j]);
printf("press 1 to repeat");
scanf("%d",&ch);
}while(ch==1);
getch();
}
/*
enter size of array3
enter array elements2
0
1
select a sorting method
1.bubble sort
2.selection sort
3.insertion sort3
array after pass 0
2 0 1
array after pass 1
0 2 1
sorted array is
0 1 2 press 1 to repeat0*/
OUTPUT
enter size of array4
enter array elements14
5
9
3
select a sorting method
1.bubble sort
2.selection sort
3.insertion sort1
array after pass 0
14 5 9 3
array after pass 1
5 9 3 14
array after pass 2
5 3 9 14
sorted array is
3 5 9 14 press 1 to repeat1
enter size of array5
enter array elements15
1
3
6
9
select a sorting method
1.bubble sort
2.selection sort
3.insertion sort2
array after pass 0
15 1 3 6 9
array after pass 1
9 1 3 6 15
array after pass 2
6 1 3 9 15
array after pass 3
3 1 6 9 15
array after pass 4
1 3 6 9 15
sorted array is
1 3 6 9 15 press 1 to repeat1
enter size of array4
enter array elements1 19 2 7
select a sorting method
1.bubble sort
2.selection sort
3.insertion sort3
array after pass 0
1 19 2 7
array after pass 1
1 19 2 7
array after pass 2
1 2 19 7
sorted array is
1 2 7 19 press 1 to repeat

Das könnte Ihnen auch gefallen