---
title: "Sort array ascending in FORTRAN"
subject: "Computer Programming"
chapter: 11
key: "semester_1/computer_programming:fortran-sort-ascending"
source: https://www.bctengineer.com/q/semester_1/computer_programming/fortran-sort-ascending
site: BCT Engineering
---

# Sort array ascending in FORTRAN

Computer Programming · Chapter 11: Programming Language: FORTRAN

- Asked in: 2073 Chaitra, 5 marks; 2069 Asar, 6 marks (asked 2 times)
- Video: [006: FORTRAN PROGRAM TO SORT ELEMENTS OF 1-DIMENSIONAL ARREY](https://www.youtube.com/watch?v=01GmQnAu6RA) — adventure in mathematics
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_1/computer_programming/fortran-sort-ascending
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-11

## Question

Write a program in FORTRAN to read an array containing N elements, sort this data in ascending order and display the result. (Read ten integer numbers, store them in array, arrange them in ascending order and display the sorted list.)

## Short answer

Read ten integers into array, sort them using bubble sort (two nested loops comparing adjacent elements and swapping if left larger). Print sorted list. Example: input 42 7 19 3 88 25 1 56 12 30 yields 1 3 7 12 19 25 30 42 56 88.

## Answer

**Method.** Ten integers are read into the array `A` with an implied DO. Bubble sort is used: in each pass adjacent elements are compared and exchanged if the left one is larger. After pass I the largest I values are in their final places at the end, so 9 passes are enough for 10 elements. The sorted list is printed with an implied DO. (For N elements read N first and use `DO 20 I = 1, N-1`, `DO 10 J = 1, N-I`.)

```fortran
      PROGRAM SORTAS
      INTEGER A(10), I, J, T
      PRINT *, 'ENTER TEN INTEGERS'
      READ *, (A(I), I = 1, 10)
      DO 20 I = 1, 9
         DO 10 J = 1, 10 - I
            IF (A(J) .GT. A(J+1)) THEN
               T = A(J)
               A(J) = A(J+1)
               A(J+1) = T
            END IF
   10    CONTINUE
   20 CONTINUE
      PRINT *, 'SORTED LIST IN ASCENDING ORDER:'
      PRINT *, (A(I), I = 1, 10)
      STOP
      END
```

**Sample:** input `42 7 19 3 88 25 1 56 12 30` gives `1 3 7 12 19 25 30 42 56 88`.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. When is a swap made in the bubble sort for ascending order?
   A. If A(J) .LT. A(J+1)
   B. If A(J) .GT. A(J+1)
   C. If A(J) .EQ. A(J+1)
   D. Always
   Answer: B. Adjacent elements are exchanged if the left one is larger, so large values move to the end. (Key point: Swap condition)

2. Why are 9 passes enough for 10 elements?
   A. Only 9 numbers can be read
   B. After each pass the largest remaining value is in its final place, so the last element is automatically correct after 9 passes
   C. The array has index 0
   D. Each pass sorts two elements
   Answer: B. After pass I the largest I values are at the end, so after 9 passes only one element remains, which is already in place. (Key point: Number of passes)

3. Why does the inner loop run J = 1, 10 - I?
   A. The last I elements are already in place, so they need not be compared again
   B. To avoid division by zero
   C. Because the array has 10 - I elements
   D. To print fewer values
   Answer: A. After pass I the largest I values are fixed at the end, so comparisons are needed only up to 10 - I. (Key point: Inner loop limit)

4. What is the variable T used for?
   A. To count passes
   B. To hold A(J) temporarily while two elements are exchanged
   C. To store the largest value
   D. To store the sorted array
   Answer: B. Without T, copying A(J+1) into A(J) would lose the old A(J). (Key point: Temporary variable)

## Related in Computer Programming

- [Display records sorted by name from file](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afile-sorted-records-by-name) — Computer Programming, chapter 10
- [Sort array using a function](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Aarray-sort-function) — Computer Programming, chapter 7
- [Greatest and smallest of ten numbers](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afortran-greatest-smallest-ten) — Computer Programming, chapter 11
- [Employee text file; sort into binary file](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afile-employee-sorted-binary) — Computer Programming, chapter 10
- [Sort employee structures by salary](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Astruct-employee-sort-salary) — Computer Programming, chapter 8, asked 2×
- [Largest of n numbers in FORTRAN](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afortran-largest-n-numbers) — Computer Programming, chapter 11

## Related in other subjects

- [8085 ALP: square of ten numbers](https://www.bctengineer.com/question.md?key=semester_4%2Fmicroprocessor%3Aalp-8085-square-numbers) — Microprocessor, chapter 2
- [Protected specifier; student/test/result program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aprotected-three-class-program) — Object Oriented Programming, chapter 6
- [Array class template with max, min, sort](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aclass-template-array-max-min-sort) — Object Oriented Programming, chapter 9
- [C program: sum of two 8-bit BCD numbers](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3A8051-bcd-sum-c) — Embedded System, chapter 9
