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

Basics

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

    (All elements are commensurate ).

  2. For all triples tex2html_wrap_inline69721, tex2html_wrap_inline61946.

    (The relation < is transitive ).

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

displaymath69687

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

Sometimes the sequence to be sorted, S, contains duplicates. I.e., tex2html_wrap_inline69743 such that tex2html_wrap_inline69745. In general when a sequence that contains duplicates is sorted, there is no guarantee that the duplicated elements retain their relative positions. I.e., tex2html_wrap_inline69747 could appear either before or after tex2html_wrap_inline69749 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_inline69747 and tex2html_wrap_inline69749 to retain their relative order in the sorted sequence, we require that tex2html_wrap_inline69757 precedes tex2html_wrap_inline69759 in S'. Therefore, the sort is stable if tex2html_wrap_inline69763.


next up previous contents index

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