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

Basics

Consider an arbitrary sequence tex2html_wrap_inline68405 comprised of of tex2html_wrap_inline57996 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_inline68429, exactly one of the following is true: i<j, i=j, or j<i.

    (All elements are commensurate ).

  2. For all triples tex2html_wrap_inline68437, tex2html_wrap_inline60826.

    (The relation < is transitive ).

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

displaymath68403

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

Sometimes the sequence to be sorted, S, contains duplicates. That is, there exist values i and j, tex2html_wrap_inline68463, such that tex2html_wrap_inline68465. 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_inline68467 could appear either before or after tex2html_wrap_inline68469 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_inline68467 and tex2html_wrap_inline68469 to retain their relative order in the sorted sequence, we require that tex2html_wrap_inline68477 precedes tex2html_wrap_inline68479 in S'. Therefore, the sort is stable if tex2html_wrap_inline68483.


next up previous contents index

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