1 / 19

Sorting Data

Sorting Data. Home. HOME. Introduction. Arrays. Subroutine Subprograms. Programming Exercise. Resources. Quiz. Learning Objectives. Learning objectives in this module Develop problem solution skills using computers and numerical methods Review of methods for sorting of tabular data

kibo-hicks
Télécharger la présentation

Sorting Data

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Sorting Data

  2. Home HOME Introduction Arrays Subroutine Subprograms Programming Exercise Resources Quiz

  3. Learning Objectives • Learning objectives in this module • 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

  4. Introduction 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 Sort-button, and the job is done. 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

  5. Introduction 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. Straight insertionis an N2 routine, and should be used only for small N, say < 20. The technique is exactly the one used by experienced card players to sort their cards: • Pick out the second card and put it in order with respect to 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. As a curiosity, in 1999 IBM set a world record in sorting of data. In 17 minutes their parallel processing computer RS6000 SP was able to sort one billion bytes of data (1012 bytes). See http://www.rs6000.ibm.com/resource/pressreleases/1999/Jul/spsort_record.html See the Procedure Click Here

  6. Procedure • 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 first number (1) that is out of sequence relative to the 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. More...

  7. Arrays • Many scientific computations use vectors and matrices. The data type Fortran uses for representing such objects is the array. • A one-dimensionalarray corresponds to a vector, while a two-dimensional array corresponds to a matrix. One-dimensional arrays Two-dimensional arrays

  8. One-dimensional arrays • 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) • 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…

  9. One-dimensional arrays • 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: • INTEGER I, SQ(10) • DO 10 I = 1, 10 • SQ(I) = I**2 • 100 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]

  10. Two-dimensional arrays • Matrices are very important in linear algebra. Matrices are usually represented by two-dimensional arrays. For example, the declaration REAL A(3,5) 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: Click for more (1,1) (1,2) (1,3) (1,4) (1,5) (2,1) (2,2) (2,3) (2,4) (2,5) Click on number to view it’s index (3,1) (3,2) (3,3) (3,4) (3,5)

  11. Subroutine Subprograms • 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

  12. Subroutine Subprograms: Structure Program name Declarations Statements call subroutinename (list-of-arguments) Stop end Main program subroutine name (list-of-arguments) declarations statements return end Subprogram If the subroutine name wassort, you should refer to it bycall sort (list-of-arguments)

  13. Program Exercise • 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: Resources

  14. Resources Introduction to Fortran Fortran Template here The whole exercise in a printable format here Web sites • Numerical Recipes In Fortran • Fortran Tutorial • Professional Programmer's Guide to Fortran77 • Programming in Fortran77

  15. Quiz • 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 • Hit object to start quiz • (Depending on your connection, this make take a few seconds...)

  16. General information About the author

  17. FAQ • 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

  18. References W. H. Preuss, et al., “Numerical Recipes in Fortran”, 2nd edition Cambridge University Press, 1992 • References to textbook: Sorting: page 321 (Chapter 8) • The Textbook can also be accessed online: Numerical Recipes in Fortran

  19. Summary Subsequent to this module you should... • 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

More Related