Sorting by Functions in Co-array Fortran
This program demonstrates custom sorting in Co-array Fortran. Here’s an explanation of the key parts:
We define custom types
string_type
andperson_type
to represent strings and people, respectively.We create arrays of these custom types:
fruits
for strings andpeople
for persons.The
sort_by_length
subroutine implements a simple bubble sort algorithm to sort thefruits
array based on the length of each string.The
sort_by_age
subroutine similarly sorts thepeople
array based on the age of each person.In the main program, we initialize the arrays, call the sorting subroutines, and then print the sorted results.
To run this program:
Save the code in a file named
sorting_by_functions.f90
.Compile the program using a Fortran compiler that supports Co-array Fortran. For example, with gfortran:
Run the compiled program:
The output should display the fruits sorted by length and the people sorted by age.
Note that Co-array Fortran is primarily used for parallel programming, but this example doesn’t utilize its parallel features. The sorting algorithms used here are simple and not optimized. In a real-world scenario, you might want to use more efficient sorting algorithms or leverage Co-array Fortran’s parallel capabilities for large datasets.