Logo Data Structures and Algorithms with Object-Oriented Design Patterns in Java
next up previous contents index

Basics

Consider an arbitrary sequence tex2html_wrap_inline68501 comprised of of tex2html_wrap_inline58076 elements drawn from a some universal set U. The goal of sorting is to rearrange the elements of S to produce a new sequence, say S', in which the elements of S appear in order.

But what does it mean for the elements of S' to be in order? We shall assume that there is a relation, <, defined over the universe U. The relation < must be a total order, which is defined as follows:

Definition  A total order  is a relation, say <, defined on the elements of some universal set U with the following properties:
  1. For all pairs of elements tex2html_wrap_inline68525, exactly one of the following is true: i<j, i=j, or j<i.

    (All elements are commensurate ).

  2. For all triples tex2html_wrap_inline68533, tex2html_wrap_inline60906.

    (The relation < is transitive ).

In order to sort the elements of the sequence S, we determine the permutation tex2html_wrap_inline68541 of the elements of S such that

displaymath68499

In practice, we are not interested in the permutation P, per se. Instead, our objective is to compute the sorted sequence tex2html_wrap_inline68547 in which tex2html_wrap_inline68549 for tex2html_wrap_inline68551.

Sometimes the sequence to be sorted, S, contains duplicates. That is, there exist values i and j, tex2html_wrap_inline68559, such that tex2html_wrap_inline68561. In general when a sequence that contains duplicates is sorted, there is no guarantee that the duplicated elements retain their relative positions. That is, tex2html_wrap_inline68563 could appear either before or after tex2html_wrap_inline68565 in the sorted sequence S'. If duplicates retain their relative positions in the sorted sequence the sort is said to be stable . In order for tex2html_wrap_inline68563 and tex2html_wrap_inline68565 to retain their relative order in the sorted sequence, we require that tex2html_wrap_inline68573 precedes tex2html_wrap_inline68575 in S'. Therefore, the sort is stable if tex2html_wrap_inline68579.


next up previous contents index

Bruno Copyright © 1998 by Bruno R. Preiss, P.Eng. All rights reserved.