Headlines
Loading...
C Program for Bubble sort or exchange sort for strings

C Program for Bubble sort or exchange sort for strings

/* Bubble sort or exchange sort - for strings */

#include <stdio.h>

#include <conio.h>

 

#define MAX 5

 

void main()

{

 int nos[MAX],temp,i,j,k;

 int tot_pass, tot_comp, tot_exch;

 char sorted = 'Y';

 clrscr();

 printf("Enter the numbers to be sorted - \n");

 for (i=0;i<MAX;i++)

 {

  printf("Element no %d - ",i+1);

  flushall();

  scanf("%d",&nos[i]);

 }

 tot_pass = 0;

 tot_comp = 0;

 tot_exch = 0;

 for (i=1;i<MAX;i++)

 {

  sorted = 'Y';

  tot_pass = tot_pass + 1;

  for (j=0;j<(MAX-i);j++)

  {

   tot_comp = tot_comp + 1;

   if (nos[j]>nos[j+1])

   {

    temp = nos[j];

    nos[j] = nos[j+1];

    nos[j+1] = temp;

    sorted = 'N';

    tot_exch = tot_exch + 1;

   }

  }

 printf("\nPass No = %d\n",i);

 for (k=0;k<MAX;k++)

 {

  printf("%d  ",nos[k]);

 }

 getch();

  if (sorted == 'Y')

   break;

 }

 printf("\n\nTotal Passes = %d",tot_pass);

 printf("\n\nTotal Comparisons = %d",tot_comp);

 printf("\n\nTotal Exchanges = %d",tot_exch);

 printf("\n\nThe sorted numbers are :- \n");

 for (i=0;i<MAX;i++)

 {

  printf("%d  ",nos[i]);

 }

 getch();

}


*** PLEASE checkout the Best deals from for top sites like Amazon, Flipkart etc ***