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

Sorting and Sorters

 

The traditional way to implement a sorting algorithm is to write a method that sorts an array of data. This chapter presents an alternate, object-oriented approach that is based on the notion of an abstract sorter  .

Think of a sorter as an abstract machine, the sole purpose of which is to sort arrays of data. A machine is an object. Therefore, it makes sense that we represent it as an instance of some class. The machine sorts data. Therefore, the class will have a method, say Sort, which sorts an array of data.

Program gif defines the Sorter interface. The interface consists of the single method Sort. This method takes as its argument an array of ComparableObjects and it sorts the objects therein.

   program34326
Program: Sorter interface.




next up previous contents index

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