Design and development of significantly sized software using top-down design and bottom-up implementation. Dynamically allocated data, object-oriented programming, architecture of memory, basics of language translation, and basics of algorithm analysis. Development of simple graphical user interfaces. Prerequisite: CS 110 (grade of C or better).

Lab 5 - Searching and Sorting - (50 points)



Due date: 4/8/2016 at 11:59pm
Create a directory called lab5.

Linear and Binary Search



Create a class called SearchComparison. The point of this part of the lab is to compare linear search and binary search. For this lab, follow these steps:

  • Declare and initialize a variable (arraysize) with a final modifier to be the size of the array. Start with 10.
  • Declare an array of integers of size arraysize.
  • Write a function that takes in an array of integers and fills it with random numbers between 0 and arraysize. Call this function on the array of integers to initialize it with random numbers.
  • Write a print function that takes in an array of any size and prints its contents.
  • Write a linear search function that takes in an array of integers and returns the index where the target was found or a -1 if the target was not found.
  • Write a binary search function in your driver class that takes in an array of integers and returns the index where the target was found or a -1 if the target was not found.
  • Ask the user to search for a target based on the key you will be searching on.
  • Use the following code to time how long it takes the linear search function to execute and note the entry in a spreadsheet.
  •  long startTime = System.nanoTime();
     // ... the code being measured ...
     long estimatedTime = System.nanoTime() - startTime;
    

  • Use the same code to time how long it takes the binary search function to execute and note the time required for it in the same spreadsheet.
  • Vary arraysize from 10 to 100, 1000, 10K, 100K, 1M and note the time required to search for a target using linear search and binary search.
  • Perform the search operation 10 times for each arraysize (10-10M) and note the time required for both linear search and binary search in separate columns of the spreadsheet.

Sorting algorithms



Create a class called SortComparison. This class will contain the following.

  • Read the input file as you did for Lab 4 and fill in an array of type Vehicle. Note: Please make sure this works before working on the rest of the lab.
  • Add functionality to the Vehicle class such that it implements the Comparable interface. Hint: You must write a compareTo() method in the Vehicle class.
  • Write a function each that takes in the array of Vehicle objects and sorts them using insertion sort, selection sort, merge sort, and quick sort and prints the sorted array on the screen. Sort the array based on manufacturer name.
  • Create a small menu that has the following options:

    1. Read the file
    2. Insertion Sort
    3. Selection Sort
    4. Merge Sort
    5. Quick Sort
    6. Quit
  • The program should work as follows: Let the user read in the file (by picking option 1 in the menu) and fill in the array with vehicle objects. Then come back to the menu and pick one of the sorting algorithms, which will print the result of the sorting. It will look the same every time because the result of sorting regardless of the algorithms but it will test your sorting algorithms.

Submitting the assignment



  • Submit all the java files, the spreadsheet, and a README to submit your assignment.
  • You can resubmit the assignment as many times as you want. It will upload a new copy of the files to the SVN repository. We will only grade the one that is closest to 11:59pm on the due date.