Headlines
Loading...
Data Structure Program -Bubble Sort, Exchange Sort

Data Structure Program -Bubble Sort, Exchange Sort

/* Bubble Sort, Exchange Sort */

#include
#include

#define MAXNOS 100 /* Macro definition for maximum numbers to be input */
#define TRUE 1 /* Macro definition for TRUE */
#define FALSE 0 /* Macro defintion for FALSE */

void bubble(int [], int); /* sorting function prototype declaration */

void main(void)
{
int a[MAXNOS]; /* array to hold the input numbers */
int m; /* stores the total count of input numbers */
int i; /* for loop variable */
input : /* label name for transfer of control through goto */
clrscr();
printf("Bubble Sort\n");
printf("------ ----\n\n");
printf("Enter how many numbers will be input for sorting - ");
flushall();
scanf("%d", &m);
if (m < 1 || m > MAXNOS) /* check for valid input */
{
printf("Error - input value is out of range...");
getch();
goto input;
}
printf("Enter the numbers one by one -\n");
for (i=0; i {
printf("%2d - ", (i+1));
flushall();
scanf("%d", &a[i]);
}
bubble(a, m); /* function is invoked for sorting the numbers */
printf("\n\nSorted numbers are - \n");
for (i=0; i printf("%d\t", a[i]); /* sorted numbers are displayed on the screen */
getch();
}

void bubble(int x[], int n) /* function definition begins here */
{
int i, j, hold, switched;
switched = TRUE;
for (i=1; i {
switched = FALSE; /* set the switched status to false for a new pass */
for (j=0; j<(n-i); j++) /* start comparing loop */
{
if (x[j] > x[j+1] ) /* compare and exchange, if required */
{
hold = x[j];
x[j] = x[j+1];
x[j+1] = hold;
switched = TRUE; /* set the switched status to true */
} /* end of if statement */
} /* end of comparisons loop */
} /* end of pass loop */
} /* end of bubble function */

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