#include <stdio.h>
/* Program C91.C - Selection Sort */

typedef short listarray[1000];

listarray list;
short ncomp, nswap, n, i;

void swap(k, l)
short *k, *l;
{
  short temp;
  temp = *k;
  *k = *l;
  *l = temp;
  return(0);
}

void sort1(list, n)
short *list;
short n;
{
  short i, j, k;

  printf("Sorting\n\n");
  ncomp = 0;
  nswap = 0;
  for (i = 0; i <= n - 2; i++) {
    for (j = i + 1; j < n; j++) {
      ncomp++;
      if (list[i] > list[j]) {
        swap(&list[i], &list[j]);
        nswap++;
      }
    }
    for (k = 0; k < n; k++)
      printf("%5d", list[k]);
    putchar('\n');
  }
}

int main(void)

{
  short FORLIM;

  printf("C91.C > demonstration of Selection Sort\n");
  printf("Enter No. of Elements : ");
  scanf("%hd", &n);
  FORLIM = n;
  for (i = 1; i <= FORLIM; i++){
    printf("Element %d : ",i);
    scanf("%hd", &list[i - 1]);
  }
  printf("Press ""Enter"" to continue...\n");
  scanf("");
  printf("\n");
  printf("Before Sorting\n\n");
  FORLIM = n;
  for (i = 1; i <= FORLIM; i++)
    printf("%5d", list[i - 1]);
  printf("\n\n");

  /*  Sort with subroutines  */

  sort1(list, n);

  printf("\nAfter Sorting\n\n");
  FORLIM = n;
  for (i = 1; i <= FORLIM; i++)
    printf("%5d", list[i - 1]);
  printf("\n\nNumber of comparisons=%3d\n", ncomp);
  printf("Number of exchanges=%3d\n\n\n", nswap);
  return(0);
}

/* End. */
#include <stdio.h>
/* Program C91.C - Selection Sort */

typedef short listarray[1000];

listarray list;
short ncomp, nswap, n, i;

void swap(k, l)
short *k, *l;
{
  short temp;

  temp = *k;
  *k = *l;
  *l = temp;
  return(0);
}

void sort1(list, n)
short *list;
short n;
{
  short i, j, k;

  printf("Sorting\n\n");
  ncomp = 0;
  nswap = 0;
  for (i = 0; i <= n - 2; i++) {
    for (j = i + 1; j < n; j++) {
      ncomp++;
      if (list[i] > list[j]) {
        swap(&list[i], &list[j]);
        nswap++;
      }
    }
    for (k = 0; k < n; k++)
      printf("%5d", list[k]);
    putchar('\n');
  }
}

int main(void)
{
  short FORLIM;

  printf("C91.C > demonstration of Selection Sort\n");
  printf("Enter No. of Elements : ");
  scanf("%hd", &n);
  FORLIM = n;
  for (i = 1; i <= FORLIM; i++){
    printf("Element %d : ",i);
    scanf("%hd", &list[i - 1]);
  }
  printf("Press ""Enter"" to continue...\n");
  scanf("");
  printf("\n");
  printf("Before Sorting\n\n");
  FORLIM = n;
  for (i = 1; i <= FORLIM; i++)
    printf("%5d", list[i - 1]);
  printf("\n\n");

  /*  Sort with subroutines  */

  sort1(list, n);

  printf("\nAfter Sorting\n\n");
  FORLIM = n;
  for (i = 1; i <= FORLIM; i++)
    printf("%5d", list[i - 1]);
  printf("\n\nNumber of comparisons=%3d\n", ncomp);
  printf("Number of exchanges=%3d\n\n\n", nswap);
  return(0);
}

/* End. */