C programming (you don't need to write program)
Problem 1 [Linear Search with Early Stop]
Below you will find a linear search function with early stop. A linear search is just a naive search - you go through each of the elements of a list one by one. Early stop works only on sorted list. Early stop means, instead of going through whole list, we will stop when your number to search can no longer be possibly found in the rest of the list. For instance, if my array a is sorted in ascending order, my linear search for a number x can stop when we reach an element a[i] > x.
Notice if no match is found, the convention for search functions is to return -1.
#include <stdio.h> |
You will notice that I am tracking the number of comparisons (between x and array element) used. Let's assume we will adopt that as the unit time cost of the linear search function.
We now have an array of size n, with elements in range of [0,max], in ascending order and max >= n-1. Using your knowledge in probability, what is the estimated average time cost for using the linear search function above to search for x, where x is in range of [0,max]? Show your derivations and assumptions.
Please write your derivations and assumptions and final formula below: |
More formal proof: Assume that the input has uniform probability and the the size of the array is n. Let X be a random variable denoting the number of comparisons. Then p(X= x) will denote the probability that we do x comparisons. Since the distribution is uniform, p(X = x) = 1/n. Once we formally defined our probability distribution now we can calculate the average number of comparisons in the linear search. What we do is compute the expected value
So average probability is (n+1)/2
Informally:
First assume input is uniformly distributed. More precisely it is (n+1)/2. When you search for a particular element x in an array of size n, that element may be located at the position either 1, or 2, …… or n. When we search we check each element in the array and compare it with x, and so when we reach kth element of the array we have already done k comparisons.
If it is at 1 then you find it in 1 comparison, if it is at 2, you find it in 2 comparisons, ……, if it is at n then you do n comparison in order to find it. In order to average it you sum the total number of comparisons 1+2+⋯+n = (n+1)n/2 and divide it by n (size of the array) resulting in (n+1)/2
C programming (you don't need to write program) Problem 1 [Linear Search with Early Stop] Below...
I need some with this program, please look at the changes I need closely. Its running correctly, but I need to do a few adjustments here is the list of changes I need: 1. All of the integers on a single line, sorted in ascending order. 2. The median value of the sorted array on a single line 3. The average of the sorted array on a single line Here is the program: #include<stdio.h> #include<stdlib.h> /* This places the Adds...
The purpose of this assignment is to familiarize you with sort algorithms. Problem Description is as follows: 8. Search Benchmarks Write a program that has at least 20 250 integers stored in an array in ascending order. It should call a function that uses the linear search algorithm to locate one of the values. The function should keep a count of the number of comparisons it makes until it finds the value. The program then should call a function that...
The purpose of this assignment is to familiarize you with sort algorithms. Problem Description is as follows: 8. Search Benchmarks Write a program that has at least 20 250 integers stored in an array in ascending order. It should call a function that uses the linear search algorithm to locate one of the values. The function should keep a count of the number of comparisons it makes until it finds the value. The program then should call a function that...
C++ Time the sequential search and the binary search methods several times each for randomly generated values, then record the results in a table. Do not time individual searches, but groups of them. For example, time 100 searches together or 1,000 searches together. Compare the running times of these two search methods that are obtained during the experiment. Regarding the efficiency of both search methods, what conclusion can be reached from this experiment? Both the table and your conclusions should...
How can I convert the following C code to MIPS Assembly? +++++++++++++++++++++++++++++++++ MIPS main program ++++++++++++++++++++++++++++++++ .data # Defines variable section of an assembly routine. array: .word x, x, x, x, x, x, x, x, x, x # Define a variable named array as a word (integer) array # with 10 unsorted integer numbers of your own. # After your program has run, the integers in this array # should be sorted. .text # Defines the start of the code...
You will be reading in 3 files in the program. One will contain a list of 1000 words in unsorted order. The second file will contain 1000 words in sorted order. The final file will contain 20 words to be searched for. The main program has been written for you. You will be implementing three functions: bool readWords(string array[], int size, string fileName); int linearSearch(string wordToFind, const string words[], int size); int binarySearch(string wordToFind, const string words[], int size); The...
5. (2 points) In this problem, consider the following variant of linear search, called search: static int search(int[] a, int x) { for (int i = 0; i <= a.length / 2; i++) { if (a[i] == x) return i; if (a[a. length - i - 1] == x) return a.length - i - 1; return -1; (a) (1 point) Which of the following describes the worst-case input for search? A. An array of size N in which x is...
C Programming write two functions, similar to what you see in the sample program. The first will ask the user to enter some information (I have included the type in parentheses) First Name (char[]) Last Name (char[]) Age (int) Height in Inches (double) Weight in Pounds (double) You will use pass-by-reference to modify the values of the arguments passed in from the main(). Remember that arrays require no special notation, as they are passed by reference automatically, but the other...
JAVA Objectives: 1. Apply linear search algorithm 2. Apply select sort algorithm 3. Apply array iteration skill Problem description: Write the following eight methods and write a main function to test these methods // return the index of the first occurrence of key in arr // if key is not found in arra, return -1 public static int linearSearch(int arr[], int key) // sort the arr from least to largest by using select sort algorithm public stati void selectSort(int arr[])...
Java Programming Write a program to find the number of comparison using binarySearch and the sequentialSearch algorithms as follows: Suppose list is an array of 2500 elements. 1. Use a random number generator to fill list; 2. Use a sorting algorithm to sort list; 3. Search list for some items as follows: a) Use the binary search algorithm to search list (please work on SearchSortAlgorithms.java and modify the algorithm to count the number of comparisons) b) Use the sequential search...