Headlines
Loading...
Data structure Program -Selection Sort, Push-down Sort

Data structure Program -Selection Sort, Push-down Sort

/* Straight Selection Sort, Push-down Sort */

#include
#include

#define MAXNOS 100 /* Macro definition for maximum numbers to be input */

void selection(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("Straight Selection Sort OR Push-down 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]);
}
selection(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 selection(int *x, int n) /* function definition begins here */
{
int i, j, v_large, p_large; /* stores value and position of largest number */
for (i=(n-1); i>0; i--) /* pass thorugh the array from the end backwards */
{
v_large = x[0]; /* stores the value of the first number */
p_large = 0; /* stores the position of the first number */
for (j=1; j<=i; j++) /* loop for locating the largest number */
{
if (x[j] > v_large)
{
v_large = x[j];
p_large = j;
} /* end of if statement */
} /* end of loop for resetting the large value */
x[p_large] = x[i]; /* shift the last value to the position of largest */
x[i] = v_large; /* shift largest value to end of unsorted array */
} /* end of for statement with loop varable = i */
} /* end of selection function */

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