Sorting Data

Download Report

Transcript Sorting Data

Sorting Data
Home
Learning Objectives
Introduction
Introduction
Arrays
Subroutine
subprograms
Program Exercise
Arrays
HOME
Subroutine Subprograms
Resources
Quiz
Programming Exercise
Resources
Quiz
Info
FAQ
References
Summary
Learning Objectives
Learning Objectives
Introduction
Arrays
Subroutine
subprograms
Learning objectives in this module
1.
Program Exercise
Resources
Quiz
2.
3.
Develop problem solution skills using computers and
numerical methods
Review of methods for sorting of tabular data
Develop programming skills using FORTRAN
New FORTRAN elements in this module
sorting
arrays
subroutines
Info
FAQ
References
Summary
Introduction
Learning Objectives
Introduction
Arrays
Subroutine
subprograms
Program Exercise
Resources
Sorting of tabular data may seem trivial, and perhaps not worth
spending time learning. After all, most of our needs may be filled
by the capabilities of Excel or some other spreadsheet program.
Just by marking the data to be sorted, and pressing the Sortbutton, and the job is done.
Quiz
However, frequently we have measured data from the laboratory
or the field, or data-sets that have been generated numerically,
that we need to input to a computer program that requires that
the numbers are sorted in ascending or descending order.
Although we in most cases may easily import the data into Excel
and perform sorting of the data before returning it to the input file
of the computer program, it will in many cases be convenient to
be able to call a subroutine that checks if an input data set is
properly sorted, and if not, do a sorting procedure.
Next
Info
FAQ
References
Summary
Introduction
Learning Objectives
Introduction
Arrays
Subroutine
subprograms
Several methods for sorting exist, as you may see in the reference
above. For our purpose, we will use the Straight insertion method
to illustrate sorting of data.
Program Exercise
Resources
Quiz
Straight insertion is an N2 routine, and should be used only for
small N, say < 20.
As a curiosity, in 1999 IBM set a world
record in sorting of data. In 17 minutes
their parallel processing computer RS6000
The technique is exactly the
oneable
used
cardofplayers
SP was
to by
sortexperienced
one billion bytes
to sort their cards:
data (1012 bytes). See
http://www.rs6000.ibm.com/resource/pres
 Pick out the second
card and put it in order with respect to
sreleases/1999/Jul/spsort_record.html
the first; then pick out the third card and insert it into the
sequence among the first two; and so on until the last card
has been picked out and inserted.
See the Procedure
Click Here
Info
FAQ
References
Summary
Procedure
Learning Objectives
Introduction
Arrays
Subroutine
subprograms
Program Exercise
Resources
Quiz
 Straight insertion is a simple method which uses simple picking and
insertion for sorting one number at a time.
 Given an initial sequence of unsorted data, the first run finds the
More... (1) that is out of sequence relative to the first number
first number
(7), and puts it into sequence.
 Then, the next number out of sequence (4) is located and put into
it’s right place. In the third run, the third number out of sequence
(6) is put in it’s right place, and so on. For this particular table, it
takes 6 runs to sort the entire sequence of numbers.
Initial order
After 1st run
After 2nd run
After 3rd run
After 4th run
After 5th run
After 6th run
7
1
1
1
1
1
1
1
7
4
4
3
3
2
8
8
7
6
4
4
3
4
4
8
7
6
5
4
Info
6
6
6
8
7
6
5
FAQ
3
3
3
3
8
7
6
5
5
5
5
5
8
7
2
2
2
2
2
2
8
References
Summary
Arrays
Learning Objectives
Introduction
Arrays
Subroutine
subprograms
 Many scientific computations use vectors and matrices. The
data type Fortran uses for representing such objects is the
array.
Program Exercise
Resources
Quiz
 A one-dimensional array corresponds to a vector, while a
two-dimensional array corresponds to a matrix.
One-dimensional arrays
Two-dimensional arrays
Info
FAQ
References
Summary
One-dimensional arrays
Learning Objectives
Introduction
Arrays
Subroutine
subprograms
Program Exercise
 The simplest array is the one-dimensional array, which is just
a linear sequence of elements stored consecutively in
memory. For example, the declaration:
REAL A(5)
Resources
Quiz
declares A as a real array of length 5 (fx A=[0, 4, 6, 3, 9])
 By convention, Fortran arrays are indexed from 1 and up.
Thus the first number in the array is denoted by A(1) and the
last by A(5).
”I’m beginning to get a hold of it, but I could use some extra
information and maybe an example!”
More…
Info
FAQ
References
Summary
One-dimensional arrays
Learning Objectives
Introduction
Arrays
Subroutine
subprograms
Program Exercise
 Each element of an array can be thought of as a separate
variable. You reference the I'th element of array a by A(I). Here
is a code segment that stores the 10 first square numbers in
the array SQ:
Resources
Quiz
100
INTEGER I, SQ(10)
DO 10 I = 1, 10
SQ(I) = I**2
CONTINUE
 This program would take the vector
SQ = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
and return the following vector
SQ = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
Info
FAQ
References
Summary
Two-dimensional arrays
Learning Objectives
Introduction
Arrays
Subroutine
subprograms
 Matrices are very important in linear algebra. Matrices are
usually represented by two-dimensional arrays. For
example, the declaration
REAL A(3,5)
Program Exercise
Resources
Quiz
defines A as a two-dimensional array of 3*5=15 real
numbers. It is useful to think of the first index as the row
index, and the second as the column index. Hence we get
the graphical picture:
3
(1,1)
7
(1,2)
23
(1,3)
1
(1,4)
6
(1,5)
5
(2,1)
5
(2,2)
17
(2,3)
8
(2,4)
54
(2,5)
5
(3,2)
76
(3,3)
32
(3,4)
49
(3,5)
2
(3,1)
Info
FAQ
Click for more
Click on number to
view it’s index
References
Summary
Subroutine Subprograms
Learning Objectives
Introduction
Arrays
Subroutine
subprograms
Program Exercise
Resources
Quiz
 Subroutine Subprograms will be further discussed in the next
module, we will here just have a quick overview of how it works.
 Subroutine subprograms is quite similar to Function
Subprograms; they are located after the main program, and
have syntax much the same as the main program.
 As opposed to function subprograms, subroutines are referred to
by a call statement followed by the subroutine name and a list
of arguments
See the Program
Structure
Info
FAQ
References
Summary
Subroutine Subprograms: Structure
Learning Objectives
Introduction
Arrays
Subroutine
subprograms
Program Exercise
Program name
Declarations
Statements
call subroutinename (list-of-arguments)
Resources
Stop
Quiz
end
Main program
subroutine name (list-of-arguments)
declarations
statements
Subprogram
return
end
If the subroutine name was
sort, you should refer to it by
call sort (list-of-arguments)
Info
FAQ
References
Summary
Program Exercise
Learning Objectives
Introduction
Arrays
Subroutine
subprograms
Program Exercise
Resources
Quiz
 Make a MAIN PROGRAM that reads tabular data (N pairs of
values of XT and YT) from an input file IN.DAT, and a FORTRAN
subroutine, SORT(N,XT,YT), that may be called with the data as
input arguments, which sorts the values in ascending order
before returning the results to the calling program.
 Print the initial data table as well as the sorted table to an
output file OUT.DAT
 Test the subroutine on the following data:
x
f(x)
So
kro
0,2
0,008
0,33
0,03
1,0
1,0
0,2
0
0,4
0,064
0,82
0,7
0,6
0,216
0,53
0,2
0
0
0,8
0,512
Resources
Info
FAQ
References
Summary
Resources
Learning Objectives
Introduction
Introduction to Fortran
Arrays
Subroutine
subprograms
Fortran Template
here
Program Exercise
Resources
The whole exercise in a printable format
here
Quiz
Web sites
 Numerical Recipes In Fortran
 Fortran Tutorial
 Professional Programmer's Guide to Fortran77
 Programming in Fortran77
Info
FAQ
References
Summary
Quiz
Learning Objectives
Introduction
Arrays
Subroutine
subprograms
Program Exercise
This section includes a quiz on the topics covered by
this module.
The quiz is meant as a control to see if you have
learned some of the most important features
Resources
Quiz
Hit object to start quiz
(Depending on your connection, this make take a few seconds...)
Info
FAQ
References
Summary
General information
Learning Objectives
Title:
Sorting Data
Introduction
Teacher(s):
Professor Jon Kleppe
Arrays
Assistant(s):
Per Jørgen Dahl Svendsen
Abstract:
Provide a good background for solving problems within
petroleum related topics using numerical methods
4 keywords:
Sorting, Subroutines, Fortran, Arrays
Subroutine
subprograms
Program Exercise
Resources
Quiz
Topic discipline:
Level:
2
Prerequisites:
None
Learning goals:
Develop problem solution skills using computers and
numerical methods
Size in megabytes:
0.5 MB
Software requirements:
MS Power Point 2002 or later, Flash Player 6.0
Estimated time to complete:
Copyright information:
The author has copyright to the module and use of the
content must be in agreement with the responsible author
or in agreement with http://www.learningjournals.net.
About the author
Info
FAQ
References
Summary
FAQ
Learning Objectives
Introduction
Arrays
Subroutine
subprograms
Program Exercise
Resources
Quiz
No questions have been posted yet. However, when questions are
asked they will be posted here.
Remember, if something is unclear to you, it is a good chance that
there are more people that have the same question
For more general questions and definitions try these
Dataleksikon
Webopedia
Schlumberger Oilfield Glossary
Info
FAQ
References
Summary
References
Learning Objectives
Introduction
Arrays
Subroutine
subprograms
W. H. Preuss, et al., “Numerical Recipes in Fortran”, 2nd
edition
Cambridge University Press, 1992
Program Exercise
Resources
Quiz

References to textbook:
Sorting: page 321 (Chapter 8)

The Textbook can also be accessed online:
Numerical Recipes in Fortran
Info
FAQ
References
Summary
Summary
Learning Objectives
Introduction
Arrays
Subsequent to this module you should...
Subroutine
subprograms

Program Exercise

Resources

Quiz

be able to construct subroutines
write and handle DO loops
have a feel for the output format
know the conditional statements and use the IF structure
Info
FAQ
References
Summary