!
PROGRAM P91
!
IMPLICIT NONE
INTEGER :: LIST(1000),COUNTS,NCOMP,NSWAP
INTEGER :: N,I
!
INTERFACE
SUBROUTINE SORT1(LIST,N,COUNTS,NCOMP,NSWAP)
IMPLICIT NONE
INTEGER ,INTENT(IN OUT) :: LIST(:),N,COUNTS,NCOMP,NSWAP
SUBROUTINE SWAP(LIST,K,L)
IMPLICIT NONE
INTEGER ,INTENT(IN OUT) :: LIST(:),K,L
END SUBROUTINE SWAP
END SUBROUTINE SORT1
END INTERFACE
!
!
PRINT *, 'This is Program >> P91 - Interchange sort'
!
! Tell program where data for READ * is coming from
OPEN(UNIT=5, FILE='P91.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 SORT1(LIST,N,COUNTS,NCOMP,NSWAP)
!
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 PROGRAM P91
!
SUBROUTINE SORT1(LIST,N,COUNTS,NCOMP,NSWAP)
IMPLICIT NONE
INTEGER ,INTENT(IN OUT) :: LIST(:),N,COUNTS,NCOMP,NSWAP
INTEGER :: I,J,K
PRINT 18
18 FORMAT(' SORTING'/)
NCOMP=0
NSWAP=0
L1: DO I=1,N-1
L2: DO J=I+1,N
NCOMP=NCOMP+1
IF(LIST(I).GT.LIST(J)) THEN
CALL SWAP(LIST,I,J)
NSWAP=NSWAP+1
ENDIF
END DO L2
PRINT 16,(LIST(K),K=1,N)
16 FORMAT(20I5)
END DO L1
RETURN
END SUBROUTINE SORT1
!
SUBROUTINE SWAP(LIST,K,L)
IMPLICIT NONE
INTEGER ,INTENT(IN OUT) :: LIST(:),K,L
INTEGER :: M
M=LIST(K)
LIST(K)=LIST(L)
LIST(L)=M
RETURN
END SUBROUTINE SWAP
/*
Program C91.C - Interchange sort
*/
#include <stdio.h>
typedef int listarray[1000]; /* Variable Type Declaration */
listarray list; /* Declaration Statements */
int ncomp, nswap, n, i;
void swap(k, l) /* Swap function declaration */
int *k, *l;
{
int temp;
temp = *k;
*k = *l;
*l = temp;
return(0);
} /* End of swap function */
void sort1(list, n) /* Sort1 function declaration */
int *list;
int n;
{
int 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]); /* Call of swap function */
nswap++;
}
} /* End of inner for{} loop */
for (k = 0; k < n; k++) printf("%5d", list[k]);
putchar('\n');
} /* End of outer for{} loop */
} /* End of sort1 function */
main()
{
int FORLIM;
printf("C91.C -> demonstration of interchange sort\n");
printf("Enter number of Elements : ");
scanf("%d", &n);
FORLIM = n;
printf("Enter elements (press ""enter"" between each entry)\n");
for (i = 1; i <= FORLIM; i++)
{
printf("Element %d : ",i);
scanf("%d", &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); /* Call of subroutine */
/* Print results */
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 of main Program C91 */
/*
INPUT :
7 59 72 41 16 27 17 11OUTPUT :
C91.C -> demonstration of interchange sort Enter number of Elements : 7 Enter elements (press enter between each entry) Element 1 : 59 Element 2 : 72 Element 3 : 41 Element 4 : 16 Element 5 : 27 Element 6 : 17 Element 7 : 11 Press Enter to continue... Before Sorting 59 72 41 16 27 17 11 Sorting 11 72 59 41 27 17 16 11 16 72 59 41 27 17 11 16 17 72 59 41 27 11 16 17 27 72 59 41 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= 18 */
{$G256}
{$P512}
{$D+}
PROGRAM p91 (input, output);
TYPE
listarray = ARRAY[1..1000] OF INTEGER;
VAR
list : listarray;
ncomp, nswap, n, i : INTEGER;
PROCEDURE swap (VAR k, l : INTEGER);
VAR
temp : INTEGER;
BEGIN
temp := k;
k := l;
l := temp
END;
PROCEDURE sort1 (VAR list : listarray; n : INTEGER);
VAR
i, j, k : INTEGER;
BEGIN
writeln ('Sorting');
writeln;
ncomp := 0;
nswap := 0;
FOR i := 1 TO n-1 DO
BEGIN
FOR j := i+1 TO n DO
BEGIN
ncomp := ncomp + 1;
IF (list[i] > list[j]) THEN
BEGIN
swap (list[i], list[j]);
nswap := nswap + 1
END
END;
FOR k := 1 TO n DO
write ( list[k] : 5);
writeln
END
END;
BEGIN
readln (n);
FOR i := 1 TO n DO
read (list[i]);
readln;
writeln (^l);
writeln ('Before Sorting');
writeln;
FOR i := 1 TO n DO
write ( list[i] : 5);
writeln;
writeln;
{ Sort with subroutines }
sort1 (list, n);
writeln;
writeln ('After Sorting');
writeln;
FOR i := 1 TO n DO
write ( list[i] : 5);
writeln;
writeln;
writeln ('Number of comparisons=', ncomp : 3);
writeln ('Number of exchanges=', nswap : 3);
writeln;
writeln;
END.
DATA :7 59 72 41 16 27 17 11
Last modified: 08/07/97