Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting Algorithms - Methods

I have to implement three different sorting algorithms using an Object-Oriented Approach and I have been thinking of the best method to combat this.

Basically, I think it should look like this:

-> Sort (Class, interface, Polymorphic)

Inherit:

-> Bubble Sort

-> Insertion Sort

-> QuickSort

If each of the sorts have the same interface then it would be easier to access the different methods of sorting, thus, meaning that when I come to add other sorting algorithms, I can easily implement them into the current design and class structure.

My questions are:

  • Is this a good approach to use?

  • Is there scope for me to use Templates? I.e. if I wanted to use Bubble, it would call the bubble sort, if I wanted to use Insertion it would call Insertion?

like image 432
Phorce Avatar asked Dec 27 '22 15:12

Phorce


1 Answers

Sort should be an interface or an abstract class, whereas bubble / insertion / quick-sort should be implementations / concrete classes.

It's also worth learning about the following:

Strategy Pattern:

Strategy Pattern Diagramhttp://en.wikipedia.org/wiki/Strategy_pattern

and

State Pattern:

State Pattern Diagram

http://en.wikipedia.org/wiki/State_pattern

As for templates, I don't think it's worth it in your case.

like image 152
sampson-chen Avatar answered Jan 16 '23 22:01

sampson-chen