Skip to main content Accessibility help
×
Hostname: page-component-7bb8b95d7b-cx56b Total loading time: 0 Render date: 2024-09-27T03:41:09.005Z Has data issue: false hasContentIssue false

Program 2 - Sylvan Sorter

Published online by Cambridge University Press:  05 July 2014

Get access

Summary

This program uses the treesort algorithm of Section 11.2 except that it sorts a list rather than an array.

The program first reads in a sequence of positive integers terminated by the value zero (or a negative value) and builds it into a list which is printed in a tabular format. This unsorted list is then converted into a binary tree which is printed in the usual representation. The binary tree is then converted back into an ordered list and finally the sorted values are printed. The program repeats until it encounters an empty sequence (just a zero or negative number).

The two library packages Lists and Trees contain the key data structures Cell and Node and associated operations. Although we have not yet discussed packages, private types and compilation units in depth this is much better than putting everything inside the main subprogram which is the style which would have to be adopted using just the algorithmic facilities described in Part 2. The types List and Tree are private types. Their full type reveals that they are access types referring to Cell and Node. Thus the inner structure of the types Cell and Node are not used outside their defining packages. The small package Page declares a constant defining the page width and a subtype defining a line of text.

There are then a number of library subprograms for performing the main activities, namely Read_List, Print_List, Print_Tree, Convert_List_To_Tree and Convert_Tree_To_List. Finally, the main subprogram calls these within an outer loop.

Type
Chapter
Information
Publisher: Cambridge University Press
Print publication year: 2014

Access options

Get access to the full version of this content by using one of the access options below. (Log in options will check for institutional or personal access. Content may require purchase if you do not have access.)

Save book to Kindle

To save this book to your Kindle, first ensure coreplatform@cambridge.org is added to your Approved Personal Document E-mail List under your Personal Document Settings on the Manage Your Content and Devices page of your Amazon account. Then enter the ‘name’ part of your Kindle email address below. Find out more about saving to your Kindle.

Note you can select to save to either the @free.kindle.com or @kindle.com variations. ‘@free.kindle.com’ emails are free but can only be saved to your device when it is connected to wi-fi. ‘@kindle.com’ emails can be delivered even when you are not connected to wi-fi, but note that service fees apply.

Find out more about the Kindle Personal Document Service.

  • Sylvan Sorter
  • John Barnes
  • Book: Programming in Ada 2012
  • Online publication: 05 July 2014
  • Chapter DOI: https://doi.org/10.1017/CBO9781139696616.017
Available formats
×

Save book to Dropbox

To save content items to your account, please confirm that you agree to abide by our usage policies. If this is the first time you use this feature, you will be asked to authorise Cambridge Core to connect with your account. Find out more about saving content to Dropbox.

  • Sylvan Sorter
  • John Barnes
  • Book: Programming in Ada 2012
  • Online publication: 05 July 2014
  • Chapter DOI: https://doi.org/10.1017/CBO9781139696616.017
Available formats
×

Save book to Google Drive

To save content items to your account, please confirm that you agree to abide by our usage policies. If this is the first time you use this feature, you will be asked to authorise Cambridge Core to connect with your account. Find out more about saving content to Google Drive.

  • Sylvan Sorter
  • John Barnes
  • Book: Programming in Ada 2012
  • Online publication: 05 July 2014
  • Chapter DOI: https://doi.org/10.1017/CBO9781139696616.017
Available formats
×