Bubble Sort

Program to sort the list of names in alphabetic order using Bubble sort.

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define max 25
void sort(char *(a)[max],int n)
{
  int i,j;
  char temp[max];
  for(i=0;i<n;i++)
    for(j=n-1;j>=i+1;j--)
    if(strcasecmp(a[j],a[j-1])<0)
    {
      strcpy(temp,a[j-1]);
      strcpy(a[j-1],a[j]);
      strcpy(a[j],temp);
    }
}
void main()
{
  int i,n;
  char *(str)[max];
  printf("Enter no of names to enter:" );
  scanf("%d",&n );
  printf("Enter the names:\n" );
  for(i=0;i<n;i++)
  {
    str[i]=malloc(max);
    scanf("%s",str[i] );
  }
  sort(str,n);
  printf("Names after the sort \n" );
  for(i=0;i<n;i++)
  printf("%s\n",str[i] );
}

OUTPUT

Comments

Popular posts from this blog

Install VS-Code in Manjaro Linux

Running (pre-loaded)Virtual Machine on VirtualBox

A Day with Manjaro