.

SIT221 Data Structures and Algorithms Practical Task 3.1

Practical Task 3.1

The purpose of this task is to study implementation of classical sorting algorithms such as Bubble Sort, Insertion Sort, and Selection Sort.

1. Explore the program code attached to this task. Create a new Microsoft Visual Studio project and import the Vector.cs file, or alternatively, extend the project inherited from Task 2.1 by copying the missing code from the enclosed template for the Vector<T> className. Import the Tester.cs file to the project to access the prepared Main method important for the purpose of debugging and testing the required algorithmic solutions.

2. Your first step in this task is to figure out how particular sorting algorithms represented via unique classes can be linked to the Vector<T> classNameyou should have completed through Tasks 1.1 and 1.2. The Vector<T> classNamemust have ability to flexibly switch between possible algorithms at any time when a user wants to change the sorting technique in use. In fact, a simple solution to implement this linkage is an interface that every classNameproviding sorting functionality must implement. Such interface, named ISorter, is already prepared for you as part of the ISorter.cs file. Study the method’s signature that the interface ensures. This method is generic and has the following purpose:

 Sort<K>( K[] sequence, IComparer<K> comparer )

Sorts the elements in the entire one‐dimensional array of generic type K using the specified comparer IComparer<K>. When the comparer is not specified, i.e. is null, the default comparer Comparer<K>.Default is applied.

Note that it imposes a constraint on the type K such that K must implement the IComparable<K> interface. This is done to guarantee that the actual data type substituting K in practice implements a default comparer, therefore the Sort<K> can sort elements of that type according to the default ordering rule determined by the IComparable<K>.

3. Now, switch to the attached template of the Vector<T> className. The given classNamehas a public property, named Sorter, implementing the aforementioned ISorter interface. The purpose of the property is to refer to a particular instance of the sorting algorithm that is currently in use by the Vector<T> className. Note that it realizes so‐called classNameAggregation as a particular form of classNamerelationship in terms of object‐oriented programming design. This makes the chosen sorting algorithm encapsulated as a className, e.g. BubbleSort or InsertionSort, so that it becomes “a part of” the Vector<T> className. (To review possible classNamerelationships, explore Chapter 4 of the SIT232 Workbook, page 99). One may read and write to this property; that is, to get/set a reference to the object serving as a sorting algorithm. Obviously, by changing the value of this property, one may also switch the sorting approach in use.

By default, this Sorter property of the Vector<T> classNamerefers to the built‐in internal classNameDefaultSorter, which implements the Sort<K> method prescribed by the imposed ISorter interface. The DefaultSorter classNamedelegates sorting to the Array.Sort method, which you should be familiar with based on Task 2.1. Because of this, by default, the both Sort() and Sort(IComparer<T> comparer) methods guarantee exactly the same behaviour as supposed in Task 2.1.

4. Implement three sorting algorithms: Bubble Sort, Insertion Sort, and Selection Sort. For this purpose, create three new classes, i.e. BubbleSort, InsertionSort, and SelectionSort, respectively. Ensure that the new classes implement the ISorter interface along with its prescribed method Sort<K>. Each classNamemust have a default constructor. You may add any extra private methods and attributes if necessary. You should rely on the code of DefaultSorter as an example of how your classes are to be implemented. Therefore, explore the code of the method and how it deals with the default comparer, i.e. the Comparer<K>.Default.

5. As you progress with the implementation of the algorithms, you should start using the Tester classNamein order to test them for potential logical issues and runtime errors. This (testing) part of the task is as important as coding. You may wish to extend it with extra test cases to be sure that your solutions are checked against other potential mistakes. To enable the tests, remember to uncomment the corresponding code lines. Remember that you may change the sorting approach for an instance of the Vector classNameby referring its Sorter property to a new object. For example,

For example, vector.Sorter = new BubbleSort();

should enable the Bubble Sort algorithm encoded via the BubbleSort className. Check whether you test the right algorithm.

Expected Printout :

This section displays the printout produced by the attached Tester className, specifically by its Main method. It is based on our solution. The printout is provided here to help with testing your code for potential logical errors. It demonstrates the correct logic rather than an expected printout in terms of text and alignment.

Test A: Sort integer numbers applying Default Sort with AscendingIntComparer:

Intital data: [333,236,312,780,100,722,511,966,213,724,122,120,263,175,752,958,596,299,995,772]

Resulting order: [100,120,122,175,213,236,263,299,312,333,511,596,722,724,752,772,780,958,966,995]

:: SUCCESS

Test B: Sort integer numbers applying Default Sort with DescendingIntComparer:

Intital data: [333,236,312,780,100,722,511,966,213,724,122,120,263,175,752,958,596,299,995,772]

Resulting order: [995,966,958,780,772,752,724,722,596,511,333,312,299,263,236,213,175,122,120,100]

:: SUCCESS

Test C: Sort integer numbers applying Default Sort with OddNumberFirstComparer:

Intital data: [333,236,312,780,100,722,511,966,213,724,122,120,263,175,752,958,596,299,995,772]

Resulting order: [724,596,958,752,120,122,966,772,722,100,780,312,236,213,995,263,175,299,511,333]

:: SUCCESS

Test D: Sort integer numbers applying BubbleSort with AscendingIntComparer:

Intital data: [333,236,312,780,100,722,511,966,213,724,122,120,263,175,752,958,596,299,995,772]

Resulting order: [100,120,122,175,213,236,263,299,312,333,511,596,722,724,752,772,780,958,966,995]

:: SUCCESS

Test E: Sort integer numbers applying BubbleSort with DescendingIntComparer:

Intital data: [333,236,312,780,100,722,511,966,213,724,122,120,263,175,752,958,596,299,995,772]

Resulting order: [995,966,958,780,772,752,724,722,596,511,333,312,299,263,236,213,175,122,120,100]

:: SUCCESS

Test F: Sort integer numbers applying BubbleSort with OddNumberFirstComparer:

Intital data: [333,236,312,780,100,722,511,966,213,724,122,120,263,175,752,958,596,299,995,772]

Resulting order: [236,312,780,100,722,966,724,122,120,752,958,596,772,333,511,213,263,175,299,995]

:: SUCCESS

Test G: Sort integer numbers applying SelectionSort with AscendingIntComparer:

Intital data: [333,236,312,780,100,722,511,966,213,724,122,120,263,175,752,958,596,299,995,772]

Resulting order: [100,120,122,175,213,236,263,299,312,333,511,596,722,724,752,772,780,958,966,995]

:: SUCCESS

Test H: Sort integer numbers applying SelectionSort with DescendingIntComparer:

Intital data: [333,236,312,780,100,722,511,966,213,724,122,120,263,175,752,958,596,299,995,772]

Resulting order: [995,966,958,780,772,752,724,722,596,511,333,312,299,263,236,213,175,122,120,100]

:: SUCCESS

Test I: Sort integer numbers applying SelectionSort with OddNumberFirstComparer:

Intital data: [333,236,312,780,100,722,511,966,213,724,122,120,263,175,752,958,596,299,995,772]

Resulting order: [236,312,780,100,722,966,724,122,120,752,958,596,772,175,511,333,213,299,995,263]

:: SUCCESS

Test J: Sort integer numbers applying InsertionSort with AscendingIntComparer:

Intital data: [333,236,312,780,100,722,511,966,213,724,122,120,263,175,752,958,596,299,995,772]

Resulting order: [100,120,122,175,213,236,263,299,312,333,511,596,722,724,752,772,780,958,966,995]

:: SUCCESS

Test K: Sort integer numbers applying InsertionSort with DescendingIntComparer:

Intital data: [333,236,312,780,100,722,511,966,213,724,122,120,263,175,752,958,596,299,995,772]

Resulting order: [995,966,958,780,772,752,724,722,596,511,333,312,299,263,236,213,175,122,120,100]

:: SUCCESS

Test L: Sort integer numbers applying InsertionSort with OddNumberFirstComparer:

Intital data: [333,236,312,780,100,722,511,966,213,724,122,120,263,175,752,958,596,299,995,772]

Resulting order: [236,312,780,100,722,966,724,122,120,752,958,596,772,333,511,213,263,175,299,995]

:: SUCCESS

.