Example 9.5


FORTRAN version

!
! =====> Program - P91.F90
!
      INTEGER LIST (1000)
      COMMON /COUNTS/NCOMP,NSWAP
!
!
      PRINT *, 'This is Program >> P95  - Selection sort'
!
!     Tell program where data for  READ *  is coming from
      OPEN(UNIT=5, FILE='P95.DAT')      ! UNIT=5 is the default input
!
      READ * ,N
      READ * ,(LIST(I),I=1,N)
      PRINT 17
  17  FORMAT(' BEFORE SORTING'/)
      PRINT 16,(LIST(I),I=1,N)
      PRINT *,' '
!
!     SORT WITH SUBROUTINES
!
      CALL SELECT(LIST,N)
!
      PRINT 14
  14  FORMAT(' AFTER SORTING'/)
      PRINT 16,(LIST(I),I=1,N)
  16  FORMAT(20I5)
      PRINT 27,NCOMP,NSWAP
  27  FORMAT(' NUMBER OF COMPARISONS=',I3/  &
             ' NUMBER OF EXCHANGES=',I3//)
      STOP
      END
!
      SUBROUTINE SELECT(LIST,N)
      INTEGER LIST(1),SMALL,I,J,K
      COMMON/COUNTS/NCOMP,NSWAP
      PRINT 18
  18  FORMAT(' SORTING'/)
      NCOMP=0
      NSWAP=0
L1:   DO I=1,N-1
         SMALL=LIST(I)
         K=I
L2:      DO J=I+1,N
            NCOMP=NCOMP+1
            IF(LIST(J)
DATA:
7
59  72  41  16  27  17  11
OUTPUT: 
[FTN90 Version 1.12 Copyright (c)SALFORD SOFTWARE LTD 1992  &  ]
[                   (c)THE NUMERICAL ALGORITHMS GROUP 1991,1992]
    NO ERRORS  [FTN90]
Program entered
 This is Program >> P95  - Selection sort
 BEFORE SORTING

   59   72   41   16   27   17   11
  
 SORTING

   11   72   41   16   27   17   59
   11   16   41   72   27   17   59
   11   16   17   72   27   41   59
   11   16   17   27   72   41   59
   11   16   17   27   41   72   59
   11   16   17   27   41   59   72
 AFTER SORTING

   11   16   17   27   41   59   72
 NUMBER OF COMPARISONS= 21
 NUMBER OF EXCHANGES=  6


Fortran-90 STOP


C version

#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. */



Come back to the previous page

Last modified: 19-02-96