Tuesday, 20 August 2019

Sort the set of strings in ascending order using Bubble sort and descending order by using Selection sort.

Sort the set of strings in ascending order using Bubble sort and descending order by using Selection sort.

#include<stdio.h>
#include<string.h>

void bubblesort();
void selectionsort();

int main(){
   int ch,z;
   setbuf(stdout, NULL);

   do
    {
    printf("\n1.Ascending Order using Bubble sort \n2.Descending Order using Selection Sort");
    printf("\nEnter choice: ");
    scanf("%d", &ch);
    switch(ch)
    {
    case 1:
    bubblesort();
    break;
    case 2:
    selectionsort();
    break;
    default:
    printf("\nInvalid choice.");
    break;
    }
    printf("\nDo you wish to continue? \nType 1 for Yes 0 for No\n");
    scanf("\n%d",&z);

    }
    while(z==1);
    return 0;
}

void bubblesort()
{
char str[25][25];
int i,j,k, count;
printf("How many strings u are going to enter?: ");
scanf("%d",&count);

printf("Enter Strings one by one: ");
for(i=0;i<count;i++)
   scanf("%s",&str[i]);

   char temp[25];
   for(i=0;i<count;i++)
   {
      for(j=i+1;j<count;j++)
      {
         if(strcmp(str[i],str[j])>0)  //str[i]>str[j]
         {
            strcpy(temp,str[i]);
            strcpy(str[i],str[j]);
            strcpy(str[j],temp);
         }
      }
      printf("\nPASS  %d",(i+1));
      for(k=0;k<count;k++)
               printf("\n%s",str[k]);
   }
}

void selectionsort()
{
char str[25][25];
int i,j,k, count;
printf("How many strings u are going to enter?: ");
scanf("%d",&count);

printf("Enter Strings one by one: ");
for(i=0;i<count;i++)
scanf("%s",&str[i]);

char temp[25];
for (j=0;j<count-1; j++ )
{
      int max = j;
      for (k=j+1; k <count; k++ )
        if ( strcmp(str[k],str[max]) > 0 )   //str[k]>str[max]
        max = k;

      strcpy(temp,str[max]);
      strcpy(str[max],str[j]);
      strcpy(str[j],temp);

      printf("\nPASS  ");
      for(i=0;i<count;i++)
      printf("\n%s",str[i]);
}
}

Output:
1.Ascending Order using Bubble sort
2.Descending Order using Selection Sort
Enter choice: 1
How many strings u are going to enter?: 4
Enter Strings one by one: a
s
d
f

PASS  1
a
s
d
f
PASS  2
a
d
s
f
PASS  3
a
d
f
s
PASS  4
a
d
f
s
Do you wish to continue?
Type 1 for Yes 0 for No
1

1.Ascending Order using Bubble sort
2.Descending Order using Selection Sort
Enter choice: 2
How many strings u are going to enter?: 4
Enter Strings one by one: a
s
d
f

PASS 
s
a
d
f
PASS 
s
f
d
a
PASS 
s
f
d
a
Do you wish to continue?

Type 1 for Yes 0 for No



Stack and Queue as ADT in C++

Stack as ADT The functions associated with stack are: empty()  – Returns whether the stack is empty size()  – Returns the size o...