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

Abstract Sorters

Program gif defines the AbstractSorter class. The AbstractSorter class implements the Sorter interface defined in Program gif. The AbstractSorter comprises the two fields, array and n, the concrete methods Swap and Sort(ComparableObject[]), and the no-arg abstract method Sort(). Since the no-arg Sort method is an abstract method, an implementation must be given in a derived class.

   program34348
Program: AbstractSorter class.

The Sort(ComparableObject[]) method does not sort the data itself. It is the no-arg Sort method, which is provided by a derived class, that does the actual sorting. The Sort(ComparableObject[]) method merely sets-up things by initializing the fields of AbstractSorter as follows: The array field refers to the array of objects to be sorted and the length of that array is assigned to the n field.

The Swap method is used to implement most of the sorting algorithms presented in this chapter. The swap method takes two integers arguments. It exchanges the contents of the array at the positions specified by the arguments. The exchange is done as a sequence of three assignments. Therefore, the Swap method runs in constant time.


next up previous contents index

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