Starting from:

$29

ALGORITHMS AND DATA STRUCTURES II PROGRAMMING ASSIGNMENT 1


1 Programming Assignment
An army battalion has n soldiers and the commander of the battalion would like to arrange a boxing tournament. There will be two categories, lightweight and heavyweight. The commander also wants to have a somewhat equal number of contestants in each category. Therefore, he needs to find the soldier with the median weight, then everyone below or equal to that weight will fall into the lightweight category and the remaining soldiers will be in the heavyweight category. Your job is to find an algorithm that will help him select the median weight soldier in the shortest possible time, because efficiency is very important in the army. We can assume that the weight of the soldiers are rounded up to the nearest integer.
Input: An array A of n non-negative integers. Output: The n 2 th integer. A Java template has been provided containing an empty function LinearSelect, which takes an integer array A and an integer k as argument, and returns the kth integer in A when sorted in an ascending order. Your task is to write the body of the LinearSelect function. You may assume that the input array A will always conform to the specification above (containing no negative values). Your code is not required to check for incorrectly formed input data. You must use the provided Java template as the basis of your submission, and put your implementation inside the LinearSelect function in the template. You may not change the name, return type or parameters of the LinearSelect function. The main function in the template contains code to help you test your implementation by entering test data or reading it from a file. You may modify the main function, because your submission will be tested using a different main function. Only the contents of the LinearSelect function and associated helper functions (if any) will be marked.
2 Examples
The table below shows the correct output of the LinearSelect function on various test inputs.
1
Input Array Median 3, 75, 12, 20 12 1, 2, 4, 5, 3 3 75 75 null -1

More products