Question

In: Computer Science

Question 5 Write a program to find the number of comparisons using binarySearch and the sequential...

Question 5
Write a program to find the number of comparisons using binarySearch and the sequential search algorithm as follows:
Suppose list is an array of 1000 elements.
5.1 Use a random number generator to fill list.
5.2 Use any sorting algorithm to sort list.
5.3 Search list for some items as follows:
a. Use the binary search algorithm to search the list. (You may need to modify the algorithm given in this chapter to count the number of comparisons.)
b. Use the binary search algorithm to search the list, switching to a sequentialsearch when the size of the search list reduces to less than 15. (Use the sequential search algorithm for a sorted list.)
Print the number of comparisons for Questions 5.3a and b. If the item is found in the list, then print its position.

Solutions

Expert Solution

Hi

I have written the code for above functions and tested it and its working fine, kindly check and let me know if you have any issues.

#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define LIST_CNT 1000

int listArr[LIST_CNT];
int comparisionCnt = 0;

int binarySearch(int *ptr, int l, int r, int item)
{
   while (l <= r) {
       int m = l + (r - l) / 2;
       // Check if item is present at mid
       if (ptr[m] == item) {
           return m;
       }
       else {
           comparisionCnt++;
       }
       // If item greater, ignore left half
       if (ptr[m] < item)
           l = m + 1;
       // If item is smaller, ignore right half
       else
           r = m - 1;
   }
   // if we reach here, then element was
   // not present
   return -1;
}

int binarySearchAndLinearSearch(int *ptr, int l, int r, int item)
{
   char seqSearch = 0;
   int m;
   while (l <= r) {
       m = l + (r - l) / 2;
       if ((LIST_CNT - m) < 15) {
           seqSearch = 1;
           break;
       }
       // Check if item is present at mid
       if (ptr[m] == item) {
           return m;
       }
       else {
           comparisionCnt++;
       }
       // If item greater, ignore left half
       if (ptr[m] < item)
           l = m + 1;
       // If item is smaller, ignore right half
       else
           r = m - 1;
   }
   for (int i = m;i < LIST_CNT;i++) {
       if (ptr[m] == item) {
           comparisionCnt++;
           return m;
       }
   }
   // if we reach here, then element was
   // not present
   return -1;
}
void mySort(int *ptr, int cnt) {

   for (int i = 0; i<cnt;i++) {
       for (int j = i + 1;j < cnt;j++) {
           int tmp;
           if (ptr[i] > ptr[j]) {
               tmp = ptr[i];
               ptr[i] = ptr[j];
               ptr[j] = tmp;
           }
       }
      
   }

}


int main()
{
   //initialize array list
   for (int i = 0;i < LIST_CNT;i++) {
       listArr[i] = rand();
   }
   printf("Before Sorting list \n");
   for (int i = 0;i < LIST_CNT;i++) {
       printf("%d ",listArr[i]);
   }
   mySort(listArr, LIST_CNT);
   printf("After Sorting list \n");
   for (int i = 0;i < LIST_CNT;i++) {
       printf("%d ", listArr[i]);
   }

   comparisionCnt = 0;
   int index = binarySearch(listArr, 0, LIST_CNT - 1, 32678);
   printf("\n Binary Search index of item = %d comparision count = %d \n", index,comparisionCnt);
  
   comparisionCnt = 0;
   index = binarySearchAndLinearSearch(listArr, 0, LIST_CNT - 1, 32678);
   printf("\n Binary Search and linear search index of item = %d comparision count = %d \n", index, comparisionCnt);

   getchar();
   return 0;
}


Related Solutions

IN C++ Write a program to find the number of comparisons using binarySearch and the sequential...
IN C++ Write a program to find the number of comparisons using binarySearch and the sequential search algorithm as follows: Suppose list is an array of 1000 elements. 3 Search list for some items as follows: a. Use the binary search algorithm to search the list. (You may need to modify the algorithm given in this chapter to count the number of comparisons.) b. Use the binary search algorithm to search the list, switching to a sequentialsearch when the size...
IN C++ Write a program to find the number of comparisons using binarySearch and the sequential...
IN C++ Write a program to find the number of comparisons using binarySearch and the sequential search algorithm as follows: Suppose list is an array of 1000 elements. 2 Use any sorting algorithm to sort list.
In C++ Write a program to find the number of comparisons using binarySearch and the sequential...
In C++ Write a program to find the number of comparisons using binarySearch and the sequential search algorithm as follows: Suppose list is an array of 1000 elements. 5.1 Use a random number generator to fill list.
In JAVA!!! write a method called Comparisons that will return the number of comparisons to find...
In JAVA!!! write a method called Comparisons that will return the number of comparisons to find an element in the tree. The main program calls this method for each element, adding the comparisons each time in order to count the total number of comparisons. The program then outputs the total number of comparisons and the average number. You may use the program BuildTreeWIthMethod to build your tree. Then, after you have made the call to inOrder(root), add the following code:...
write a method called Comparisons that will return the number of comparisons to find an element...
write a method called Comparisons that will return the number of comparisons to find an element in the tree. The main program calls this method for each element, adding the comparisons each time in order to count the total number of comparisons. The program then outputs the total number of comparisons and the average number. You may use the program BuildTreeWIthMethod to build your tree. Then, after you have made the call to inOrder(root), add the following code: int totalComparisons=0;...
2. Answer the following question (a) Write a Java program to find the number of integers...
2. Answer the following question (a) Write a Java program to find the number of integers within the range of two specified numbers and that are divisible by another number. For example, x = 5, y=20 and p =3, find the number of integers within the range [x, y] and that are divisible by p. (b) Write explicitly on the following OOP concepts: i. Encapsulation ii. Inheritance iii. Polymorphism (c) Develop a Java application that computes the two roots of...
Write a c program to calculate the factorial of a number using recursion    [8] Question five...
Write a c program to calculate the factorial of a number using recursion    [8] Question five Write a stack algorithm to POP an item                                                         [6] What does FRONT and REAR signify in a queue?                                                                 [6] Write an algorithm for a dequeue operation                                                                       [8]
Using c++, Write a program to perform the multiplication of 10 consecutive number starting from 5?...
Using c++, Write a program to perform the multiplication of 10 consecutive number starting from 5? Write a program to perform the summation of 10 even number starting from 2? Write a program to perform the summation of 10 odd number starting from 2? Write a program to perform the summation of 10 number starting from 2 and increment is given by user? Write a program to combine all operations from 1 to 4 in a single program using ‘Switch’...
Analyze the worst-case, best-case, and average-case number of comparisons of sequential search if exactly three-tenths of...
Analyze the worst-case, best-case, and average-case number of comparisons of sequential search if exactly three-tenths of the time, the element x to search for is not in the list and if x is in the list, it is equally likely to be in any position.
write C++ program using functions (separate function for each bottom) Write a program to find if...
write C++ program using functions (separate function for each bottom) Write a program to find if a number is large word for two given bottom base - bottom1 and bottom2. You can predict that a number, when converted to any given base shall not exceed 10 digits. . the program should ask from user to enter a number that it should ask to enter the base ranging from 2 to 16 after that it should check if the number is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT