+1(978)310-4246 credencewriters@gmail.com
  

Practical – Files and Sorting
Aims
• To implement and test Bubble Sort, Insertion Sort and Selection Sort.
• To parse and sort the data in a file.
Before the Practical
• Read this practical sheet fully before starting.
• Download Sorts.java or Sorts.py from Blackboard. Use this as a starting point
to writing the sorting methods.
• Download SortsTestHarness.java or SortsTestHarness.py to test your code.
• Download the RandomNames7000.csv.
Note:
Java Students: Please adhere to the Java Coding Standard document located
under Links and Resources on Blackboard.
Python Students: Please adhere to the PEP8 Coding Standard.
Activities
1. Bubble Sort Implementation
Time to write some code:
• In Sorts.java/.py, implement the bubbleSort() method using the pseudocode
from the lecture slides as a guide.
• Don’t forget to include a check to stop bubbleSort() if it does not do any swaps
during a pass (i.e., The array has finished being sorted).
• Note: The method in the Sorts.java/py file works on an int[] array.
• Test your code using the SortsTestHarness.java/py
java SortsTestHarness n xy …
python3 SortsTestHarness.py n xy …
where:
n is the number of integers to sort
x is one of:
b – bubble sort
i – insertion sort
s – selection sort
y is one of:
a – 1..n ascending
d – 1..n descending
r – 1..n in random order
n – 1..n nearly sorted (10% moved)
Hint: You can run multiple test cases by adding additional
Command Line Arguments.
2. Selection Sort Implementation
Do the same for Selection Sort as you did for Bubble Sort.
• Selection Sort differs from Bubble Sort in that it only swaps once per pass.
Instead, what it does is search for the smallest value, update the index of the
smallest value until the end of the pass. Only then, does it swap the smallest
value with the first value.
– Remember that in the second pass, the first value has already been sorted.
Therefore, don’t include the first value in the second pass
– The same is true for subsequent passes (i.e., The third pass should ignore
the first two values.).
• Test your code using the SortsTestHarness.java/py.
3. Insertion Sort Implementation
Do the same for Insertion Sort as you did for Bubble Sort and Selection Sort.
• Test your code using the SortsTestHarness.java/py.
4. Exploring Sorting Runtimes
SortsTestHarness.java/py lets you easily test your sorts code for various types of
data. (Refer to Activity 1)
• Create a table of runtime results, using at least four array sizes and at least three
of the ascending/descending/random/nearly sorted options across each of the
implemented sorting algorithms.
• Write a paragraph discussing the results in terms of time complexity and other
characteristics of the sorting algorithms.
• Hint: Identify the average/best/worst cases and investigate the scalability of the
sorting algorithms.
5. Sorting a File
Now you can try sorting a file of random student numbers and names:
• Parse the file to read in the Student IDs into an array.
• Sort the data using each of the sorting algorithms and output the sorted array
into a .csv file.
• Challenge: You can read in the Student ID and Student Name into an object.
(Not required, but highly beneficial for subsequent practicals.)
Marking Guide
Your submission will be marked as follows:
• [2] Bubble Sort is implemented properly and tests correctly.
• [2] Selection Sort is implemented properly and tests correctly.
• [2] Insertion Sort is implemented properly and tests correctly.
• [2] Sorts performance investigation.
• [2] File reading and sorting.

Purchase answer to see full
attachment

  
error: Content is protected !!