Sunday, March 18, 2012

Bubble and Insertion sort

/* Program Name: Bubble and Insertion sort
Done By: Prof.Dhumane A.V. VIIT, Pune
*/

#include
#include
#include

void get_array(int*array,int no_of_elements)
{
int i;

for(i=0;i {
scanf("%d",&array[i]);
}

}
void display_sorted_array(int *array,int no_of_elements)
{
int i;
printf("\nSorted array is \n");
for(i=0;i {
printf("%d ",array[i]);
}
}
void bubble_sort()
{
int *array;
int no_of_elements,i,j,temp;

printf("\nHow many elements you want to sort ");
scanf("%d",&no_of_elements);

printf("\nEnter the array ");
get_array(array,no_of_elements);

for(i=0;i {
for(j=i+1;j {
if(array[j] {
temp=array[i];
array[i]=array[j];
array[j]=temp;
}
}
}
display_sorted_array(array,no_of_elements);

}
void insertion_sort()
{
int *array;
int no_of_elements,i,j,temp;

printf("\nHow many elements you want to sort ");
scanf("%d",&no_of_elements);

printf("\nEnter the array ");
get_array(array,no_of_elements);

for(i=1;i {
temp=array[i];

for(j=i-1;j>=0&&temp {
array[j+1]=array[j];
}
array[j+1]=temp;
}

display_sorted_array(array,no_of_elements);

}

void main()
{
int choice;

clrscr();

while(1)
{
printf("\n1.Bubble Sort");
printf("\n2.Insertion Sort");
printf("\n3.Exit");
printf("\nEnter your choice ");
scanf("%d",&choice);
switch(choice)
{
case 1: bubble_sort();
break;
case 2: insertion_sort();
break;
case 3:
exit(0);
}
}
}

No comments:

Post a Comment