Computers in Engineering WWW Site - Example 15.2

# Example 15.2

Untitled

#### FORTRAN Version

```!
PROGRAM P92
!
IMPLICIT NONE
INTEGER :: LIST(1000)
INTEGER :: COUNTS,NCOMP,NSWAP
!
INTERFACE
SUBROUTINE BSORT1(LIST,N,COUNTS,NCOMP,NSWAP)
IMPLICIT NONE
INTEGER ,INTENT(IN OUT) :: LIST(:),NCOMP,NSWAP,COUNTS
INTEGER ,INTENT(IN) :: N
END SUBROUTINE BSORT1
END INTERFACE
INTERFACE
SUBROUTINE SWAP(LIST,K,L)
IMPLICIT NONE
INTEGER ,INTENT(IN OUT) :: LIST(:),K,L
END SUBROUTINE SWAP
END INTERFACE
!
PRINT *, 'This is Program >> P92  - Bubble sort'
!
!     Tell program where data for  READ *  is coming from
OPEN(UNIT=5, FILE='P92.DAT')
! UNIT=5 is the default input
!
PRINT 17
17  FORMAT(' BEFORE SORTING'/)
PRINT 16,(LIST(I),I=1,N)
PRINT * ,' '
!
!     SORT WITH SUBROUTINES
!
CALL BSORT1(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 PROGRAM P92
!
SUBROUTINE BSORT1(LIST,N,COUNTS,NCOMP,NSWAP)
IMPLICIT NONE
INTEGER ,INTENT(IN OUT) :: LIST(:),NCOMP,NSWAP,COUNTS
INTEGER ,INTENT(IN) :: N
INTEGER :: I,K,KK
PRINT 23
23  FORMAT(' SORTING'/)
NCOMP=0
NSWAP=0
L1:   DO
K=0
L2:      DO I=1,N-1
NCOMP=NCOMP+1
IF(LIST(I) > LIST(I+1))THEN
CALL SWAP(LIST,I,I+1)
NSWAP=NSWAP+1
K=1        ! A swap done
ENDIF
END DO L2
PRINT 16,(LIST(KK),KK=1,N)
16  FORMAT(20I5)
IF(K == 0) EXIT  ! No swaps done
END DO L1
RETURN
END SUBROUTINE BSORT1
!
SUBROUTINE SWAP(LIST,K,L)
IMPLICIT NONE
INTEGER ,INTENT(IN OUT) :: LIST(:),K,L
INTEGER :: K
M=LIST(K)
LIST(K)=LIST(L)
LIST(L)=M
RETURN
END SUBROUTINE SWAP
```

#### C Version

```/*
C92.C -> Example of Bubble Sort.

*/

#include <stdio.h>

typedef short listarray[1000];  /* Variable type declaration */

listarray list;            /* Global Variable declaration */
short ncomp, nswap, n, i;

void swap(k, l)    /* Swap function declaration */
short *k, *l;
{
short temp;

temp = *k;
*k = *l;
*l = temp;
} /*  End of swap function  */

void bsort1(list, n)    /* bsort1 subroutine declaration */
short *list;
short n;
{
short i, k, kk;

printf("Sorting\n\n");
ncomp = 0;
nswap = 0;
do
{
k = 0;
for (i = 1; i < n; i++)
{
ncomp++;
if (list[i - 1] > list[i])
{
swap(&list[i - 1], &list[i]);  /*  Call of swap function  */
nswap++;
k = 1;
}
} /*  End of for{} loop  */

for (kk = 0; kk < n; kk++) printf("%5d", list[kk]);
putchar('\n');
} while (k != 0);     /* End of while{} loop */
} /*  End of bsort1 subroutine  */

main()
{
/*  Declaration Statements  */
short FORLIM;

clrscr();   /* Clear screen */
printf("C92.C -> Bubble sort program\n");

/*  Assignment Statements  */
printf("Enter Number of Elements : ");
scanf("%hd", &n);
FORLIM = n;

printf("Enter elements (press ""enter"" between each entry)\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  */

bsort1(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 C92  */
/*
```
INPUT :
```7
59
72
41
16
27
17
11

```
OUTPUT :
```C92.C -> Bubble sort program
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
59   41   16   27   17   11   72
41   16   27   17   11   59   72
16   27   17   11   41   59   72
16   17   11   27   41   59   72
16   11   17   27   41   59   72
11   16   17   27   41   59   72
11   16   17   27   41   59   72

After Sorting
11   16   17   27   41   59   72

Number of comparisons= 42
Number of exchanges= 18
*/
```

#### Pascal Version

```{\$G256}
{\$P512}
{D+}
PROGRAM p92 (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 bsort1 (VAR list : listarray; n : INTEGER);
VAR
i, k, kk : INTEGER;
BEGIN
writeln ('Sorting');
writeln;
ncomp := 0;
nswap := 0;
REPEAT
K := 0;
FOR i := 1 TO n-1 DO
BEGIN
ncomp := ncomp + 1;
IF ( list[i] > list[i+1] ) THEN
BEGIN
swap (list[i], list[i+1]);
nswap := nswap + 1;
k := 1
END
END;
FOR kk := 1 TO n DO
write ( list[kk] : 5);
writeln;
UNTIL k = 0
END;

BEGIN
FOR i := 1 TO n DO
writeln (^l);
writeln ('Before Sorting');
writeln;
FOR i := 1 TO n DO
write ( list[i] : 5);
writeln;
writeln;

{  Sort with subroutines  }

bsort1 (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
```