Suppose you have to compute as well as . Describe how to compute these together using a single sorting of .


First we sort relation on . That is if two tuples have different values, then we use their values for attribute to sort them. If they have the same value, we use their values for attribute to sort them.

Then we perform the following:

  1. address of first tuple of
  2. Let be an empty set. At the end of this algorithm it will contain the result of .
  3. Let be an empty set. At the end of this algorithm it will contain the result of .
  4. tuple to which points
  5. .
  6. while ( null)
    1. tuple to which points
    2. .
    3. Set to point to the next tuple of .
    4. .
    5. while (not and null)
      1. tuple to which points.
      2. If ( )
        1. Set to point to the next tuple of .
      3. Else
    6. If ( OR null)
      1. .