.

SIT221 Data Structures and Algorithms Practical Task 2.1

Practical Task 2.1

General Instructions

1. Your task is to enable sorting of generic data stored as part of the Vector<T> data structure, which you should have already developed in task 1.1. You do not need to write lots of code for this task, but rather get a solid understanding of interfaces required to support sorting operations in .NET Framework, and how methods implementing these interfaces can be linked to your own code. This knowledge is crucial for you as a programmer.

2. Import the Tester.cs file to the project to access the prepared Main method important for the purpose of debugging and testing the advanced version of the Vector<T> className.

3. Start with the extension of your Vector<T> classNameand add the following two overloaded methods:

 void Sort() Sorts the elements in the entire Vector<T>. It relies on the static Array.Sort method, which applies the Introspective Sort. To determine the order of the elements, Array.Sort calls the default comparer Comparer<T>.Default for type T.

 void Sort(IComparer<T> comparer) Sorts the elements in the entire Vector<T> using the specified comparer. This method relies on the Array.Sort method, which applies the Introspective Sort. If the comparer is null, the method calls Array.Sort with the default comparer Comparer<T>.Default for type T.

Note that in this task you do not need to implement any sorting algorithm at all. Indeed, you should delegate sorting of generic data elements to the default built‐in sorting method realised within the Array classNameof .NET Framework. Specifically, you must call the Array.Sort method internally and pass the private data array of the Vector<T> as an input. Note that there are several overloaded versions of this method and you should choose two of them: One that accepts no arguments and one that expects a comparer object along with the starting index and the number of elements to define the range of sorting. Read more about the standard methods of the Array classNamefollowing the link:

https://msdn.microsoft.com/en‐us/library/system.array_methods(v=vs.110).aspx

When the two above methods are configured properly, you should be able to sort integers and successfully run the corresponding tests A, B, C, and D in the Tester classNamewithout compilation and runtime errors. To enable the tests, remember to uncomment the corresponding code lines.

4. Now proceed with the next step, and first explore the Student classNamepresented in Tester.cs.

In order to make the Vector<T> classNamecapable to perform default sorting operations on a set of generic elements of type T, you must ensure a mechanism that compares two T type elements. This mechanism will provide a default way to determine precedence for a pair of elements. To achieve this default logic, the T type itself must know how to compare one element (usually referred to this) to another given element of the same type. From the code perspective, this is enforced by the special IComparable<T> interface that type T must implement. Implementing the IComparable<T> also requires T to define the compulsory CompareTo(T another) method which eventually compares this (current) element to another element and returns an integer value that is

 less than zero, when the current instance precedes the object specified by the CompareTo method in the sort order;

 zero, when this current instance occurs in the same position in the sort order as the object specified by the CompareTo method;

 greater than zero, when this current instance follows the object specified by the CompareTo method in the sort order.

Read the following article to get more insights about this interface

https://msdn.microsoft.com/en‐us/library/system.icomparable(v=vs.110).aspx

and make sure you understand all the remarks.

Hence, T refers to the Student as an actual className. You need to modify the Student classNameand implement IComparable<Student> interface along with its underlying CompareTo(Student another) method. Write the code so that the method arranges students in the ascending order of the ID attribute. Complete this part and pass Test E of the attached testing className. This test calls the Sort method of the Vector<T> className, which should currently delegate sorting to Array.Sort that implies IComparable<Student>. Again, uncomment the corresponding code lines to activate the test.

5. Finally, develop a so‐called comparator, a dedicated classNamewhose purpose is ordering a pair of elements of specific type. Each comparator classNameimplements the IComparer<T> interface

Note that Tester.cs already has a number of examples used to sort integers in ascending, descending, or in the order that places odd integers prior to even in a sequence of numbers. Explore the enclosed code as it should help you with development of similar comparator classes for the Student className. You will see that the IComparable<Student> and the IComparer<Student> interfaces are quite similar as they both require you to provide a method for comparison of two students. The only difference is that the CompareTo(Student another) method of the IComparable<Student> operates on this and another objects, while the Compare(Student A, Student B) is to compare A and B, two objects, given as arguments. The Compare method follows the same logic as the aforementioned CompareTo method and returns an integer value indicating whether A is less than, equal to, or greater than B.

Develop two comparators for the Student className: One, say AscendingIDComparer, to sort a vector of  students in ascending order of IDs, and another, say DescendingNameDescendingIdComparer, that sorts  students in descending order of names, breaking ties by descending order of IDs. Check the correctness  of sorting via tests F and G provided by the testing className. Make sure that students appear in the desired  order. Note that the both comparators are to be the argument for the Sort(IComparer<T> comparer)  method of the Vector<T> className.

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: Run a sequence of operations:

Create a new vector by calling 'Vector<int> vector = new Vector<int>(50);'

Add a sequence of numbers 2, 6, 8, 5, 5, 1, 8, 5, 3, 5, 7, 1, 4, 9

Sort the integers in the default order defined by the native CompareTo() method

Resulting order: [1,1,2,3,4,5,5,5,5,6,7,8,8,9]

:: SUCCESS

Test B: Run a sequence of operations:

Create a new vector by calling 'Vector<int> vector = new Vector<int>(50);'

Add a sequence of numbers 2, 6, 8, 5, 5, 1, 8, 5, 3, 5, 7, 1, 4, 9

Sort the integers in the order defined by the AscendingIntComparer className

Resulting order: [1,1,2,3,4,5,5,5,5,6,7,8,8,9]

:: SUCCESS

Test C: Run a sequence of operations:

Create a new vector by calling 'Vector<int> vector = new Vector<int>(50);'

Add a sequence of numbers 2, 6, 8, 5, 5, 1, 8, 5, 3, 5, 7, 1, 4, 9

Sort the integers in the order defined by the DescendingIntComparer className

Resulting order: [9,8,8,7,6,5,5,5,5,4,3,2,1,1]

:: SUCCESS

Test D: Run a sequence of operations:

Create a new vector by calling 'Vector<int> vector = new Vector<int>(50);'

Add a sequence of numbers 2, 6, 8, 5, 5, 1, 8, 5, 3, 5, 7, 1, 4, 9

Sort the integers in the order defined by the OddNumberFirstComparer className

Resulting order: [2,6,8,8,4,5,5,1,5,3,5,7,1,9]

:: SUCCESS

Test E: Run a sequence of operations:

Create a new vector of Student objects by calling 'Vector<Student> students = new Vector<Student>();'

Add student with record: 0[Vicky]

Add student with record: 1[Cindy]

Add student with record: 2[Tom]

Add student with record: 3[Simon]

Add student with record: 4[Richard]

Add student with record: 5[Vicky]

Add student with record: 6[Tom]

Add student with record: 7[Elicia]

Add student with record: 8[Richard]

Add student with record: 9[Cindy]

Add student with record: 10[Vicky]

Add student with record: 11[Guy]

Add student with record: 12[Richard]

Add student with record: 13[Michael]

Sort the students in the default order defined by the native CompareTo() method

Print the vector of students via students.ToString();

[0[Vicky],1[Cindy],2[Tom],3[Simon],4[Richard],5[Vicky],6[Tom],7[Elicia],8[Richard],9[Cindy],10[Vicky],11[Guy],12[Richard] ,13[Michael]]

:: SUCCESS

Test F: Run a sequence of operations:

Create a new vector of Student objects by calling 'Vector<Student> students = new Vector<Student>();'

Add student with record: 0[Vicky]

Add student with record: 1[Cindy]

Add student with record: 2[Tom]

Add student with record: 3[Simon]

Add student with record: 4[Richard]

Add student with record: 5[Vicky]

Add student with record: 6[Tom]

Add student with record: 7[Elicia]

Add student with record: 8[Richard]

Add student with record: 9[Cindy]

Add student with record: 10[Vicky]

Add student with record: 11[Guy]

Add student with record: 12[Richard]

Add student with record: 13[Michael]

Sort the students in the order defined by the AscendingIDComparer classNamePrint the vector of students via students.ToString(); [0[Vicky],1[Cindy],2[Tom],3[Simon],4[Richard],5[Vicky],6[Tom],7[Elicia],8[Richard],9[Cindy],10[Vicky],11[Guy],12[Richard] ,13[Michael]]

:: SUCCESS

Test G: Run a sequence of operations:

Create a new vector of Student objects by calling 'Vector<Student> students = new Vector<Student>();

Add student with record: 0[Vicky]

Add student with record: 1[Cindy]

Add student with record: 2[Tom]

Add student with record: 3[Simon]

Add student with record: 4[Richard]

Add student with record: 5[Vicky]

Add student with record: 6[Tom]

Add student with record: 7[Elicia]

Add student with record: 8[Richard]

Add student with record: 9[Cindy]

Add student with record: 10[Vicky]

Add student with record: 11[Guy]

Add student with record: 12[Richard]

Add student with record: 13[Michael]

Sort the students in the order defined by the DescendingNameDescendingIdComparer classNamePrint the vector of students via students.ToString(); [10[Vicky],5[Vicky],0[Vicky],6[Tom],2[Tom],3[Simon],12[Richard],8[Richard],4[Richard],13[Michael],11[Guy],7[Elicia],9[Cin dy],1[Cindy]]

:: SUCCESS

.