Assume that you are given an unsorted array that contains 100 items, and you have been asked to write/propose an efficient searching algorithm. Which algorithm would you choose and why? What are the possible trad-offs of the selected algorithm if the array size is increased (e.g., 9000 items)?
Sequential Search
If you want to find the position in an unsorted array of n integers that stores a particular value, we cannot really do better than simply looking through the array from the beginning and move toward the end until you find what you are looking for. This algorithm is called sequential search.
Here is a simple implementation for sequential search.
JAVA CODE :
// Return the position of an element in array A with value K. // If K is not in A, return A.length. static int sequential(int[] A, int K) { for (int i=0; i<A.length; i++) // For each element if (A[i] == K) // if we found it return i; // return this position return A.length; // Otherwise, return the array length }
C++ CODE :
// Find the position in A that holds value K, if any does int sequential(int A[], int size, int K) { for (int i=1; i<size; i++) // For each element if (A[i] == K) // if we found it return i; // return this position return size; // Otherwise, return the array length }
In the case of sequential search, it is easy to see that if the value is in position i of the array, then sequential search will look at i values to find it. If the value is not in the array at all, then we must look at n values if the array holds n values. This would be called the worst case for sequential search.
Since the amount of work is proportional to n, we say that the worst case for sequential search has linear cost. For this reason, the sequential search algorithm is sometimes called linear search.
It will work great with both type of data i.e. 100 items as well as 9000 items.
Assume that you are given an unsorted array that contains 100 items, and you have been...
Type your ideas/definitions/examples into the message field to the following discussion prompt: Discussion Topic: Assume that you are given an unsorted array that contains 100 items, and you have been asked to write/propose an efficient searching algorithm. Which algorithm would you choose and why? What are the possible trad-offs of the selected algorithm if the array size is increased (e.g., 9000 items)?
Consider the function FINDDUPLICATES(A[0. 1) that returns a list of all duplicate items in the unsorted integer array A of size n. For example, given the array |3, 2, 4, 3], the function should return the value 3. For the array 11 2,3 5,5,5,66,81, the function should return an array (or list) 5,6 In this task, you will develop two alternative solutions for the FINDDUPLICATES(Ao..n 1 func- tion. In your implementations, you may call any of the algorithms introduced in...
Problem 3 (20 points): An array A of size (n) contains floating-point items. 1. Implement a Divide & Conquer algorithm to return the number of items with values less than a given value (x). (5 points) 2. Test your algorithm by generating an array A of size n = 1024 random floating-point numbers with each number R between 0 and 1, i.e. 0.0 <R< 1.0. Run the algorithm to find the percentage of the numbers in the array with values...
Please use MATLAB You are given an array, weights, that contains the weights of some cargo items in pounds. You want to load a truck with items from the list, up to its capacity. The truck has a maximum capacity of 1,000 pounds. For this problem, you want to find the maximum possible weight that can be loaded onto the truck. The greedy algorithm is not guaranteed to achieve this optimal result. For example, if the weights were (25 452...
In a Knapsack problem, given n items {I1, I2, · · · , In} with weight {w1, w2, · · · , wn} and value {v1,v2, ···, vn}, the goal is to select a combination of items such that the total value V is maximized and the total weight is less or equal to a given capacity W . i-1 In this question, we will consider two different ways to represent a solution to the Knapsack problem using . an...
Given the prototype: int countPassing(double arr[], int num); Assume that array arr contains num elements with a value. You may assume that the array has been declared with at least num elements. Write the the function definition including the function header for countPassing() so that the number of values greater than or equal to 69.5 in arr[] is returned.
Please finish all the questions,Thanks Following is Appendix assume the definition of Java class Heap given in the Appendix For this question, a. (2 marks Consider the following heap 30 12 20 19 6 10 18 Given the array representation of a heap discussed in class, what is the array that corre sponds to this heap? b. (5 marks) Successively insert into the heap of part (a.) 22, 35 and 11, in that order. Use the standard heap insertion algorithm....
solution is required in pseudo code please. 2 Knapsack Problem În al Knapsack problem. given n items(11-12. . . . . 1"} with weight {w1·W2. . . . . ux) and value (n 2, .., nJ, the goal is to select a combination of items such that the total value V is maximized and the total weight is less or equal to a given capacity In this question, we will consider two different ways to represent a solution to the...
(20 points) You are given an array A of distinct integers of size n. The sequence A[1], A[2], ..., A[n] is unimodal if for some index k between 1 and n the values increase up to position k and then decrease the reminder of the way until position n. (example 1, 4, 5, 7, 9, 10, 13, 14, 8, 6, 4, 3, 2 where the values increase until 14 and then decrease until 1). (a) Propose a recursive algorithm to...
2 Knapsack Problem In a Knapsack problem, given n items {11, I2, -.., In} with weight {wi, w2, -.., wn) and value fvi, v2, ..., vn], the goal is to select a combination of items such that the total value V is maximized and the total weight is less or equal to a given capacity W. Tt i=1 In this question, we will consider two different ways to represent a solution to the Knapsack problem using an array with size...