Skip to main content

Simple Examples

  • Chapter
  • First Online:
  • 881 Accesses

Abstract

In this chapter, a number of fairly elementary algorithms are developed. They are, namely: linear search; finding the maximal element in an array; a version of binary search; a simple pattern matching algorithm; raising a number to a specific integer power; and finding the integer approximation of a logarithm to the base 2.

This is a preview of subscription content, log in via an institution.

Buying options

Chapter
USD   29.95
Price excludes VAT (USA)
  • Available as PDF
  • Read on any device
  • Instant download
  • Own it forever
eBook
USD   84.99
Price excludes VAT (USA)
  • Available as EPUB and PDF
  • Read on any device
  • Instant download
  • Own it forever
Softcover Book
USD   109.99
Price excludes VAT (USA)
  • Compact, lightweight edition
  • Dispatched in 3 to 5 business days
  • Free shipping worldwide - see info
Hardcover Book
USD   109.99
Price excludes VAT (USA)
  • Durable hardcover edition
  • Dispatched in 3 to 5 business days
  • Free shipping worldwide - see info

Tax calculation will be finalised at checkout

Purchases are for personal use only

Learn about institutional subscriptions

Notes

  1. 1.

    One can easily prove this assertion rigourously by referring to the definitions for existential and/or universal quantification in Sect. 2.1.2 in the context of the predicate formula version of app(A, x, A. len, A. len).

  2. 2.

    Recall that we noted at the start of this discussion, in Sect. 3.1.1, that app(A, x, 0, A. len) holds invariantly throughout—i.e. x was assumed to appear somewhere in A.

  3. 3.

    These variables are not mentioned in the pre- and postconditions, and are, in this sense, local to the inner workings of the code to be constructed “between” the pre- and postconditions, as it were. Morgan has a refinement rule that regards the introduction of local variables into the frame as a refinement.

  4. 4.

    Here, the part relating to the variant has been omitted, since it will be true under the substitution.

  5. 5.

    As suggested later, if N is closer to 2 + 1 than it is to 2, then ( + 1) would be a better approximation of log 2(N) than would be. However, this can only be established by explicitly computing 2 and 2 + 1—something which we wish to avoid in the algorithm to be derived.

Author information

Authors and Affiliations

Authors

Rights and permissions

Reprints and permissions

Copyright information

© 2012 Springer-Verlag Berlin Heidelberg

About this chapter

Cite this chapter

Kourie, D.G., Watson, B.W. (2012). Simple Examples. In: The Correctness-by-Construction Approach to Programming. Springer, Berlin, Heidelberg. https://doi.org/10.1007/978-3-642-27919-5_3

Download citation

  • DOI: https://doi.org/10.1007/978-3-642-27919-5_3

  • Published:

  • Publisher Name: Springer, Berlin, Heidelberg

  • Print ISBN: 978-3-642-27918-8

  • Online ISBN: 978-3-642-27919-5

  • eBook Packages: Computer ScienceComputer Science (R0)

Publish with us

Policies and ethics