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.
Write a program in C++ to find the number of comparisons using binarySearch and the sequential...
Write a program in C++ to find the number of comparisons using binarySearch and the sequential search algorithm as follows: Suppose list is an array of 1000 elements. a. Use a random number generator to fill list. b. Use any sorting algorithm to sort list. c. Search list for some items as follows: i. 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.)...
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;...
Write a parallel program using Pthread based on given sequential solution. Please set the thread number...
Write a parallel program using Pthread based on given sequential solution. Please set the thread number as 10 in your code. Given Sequential Solution: #include <stdlib.h> #include <stdio.h> #include <string.h> #define MAX 10240 int total = 0; int n1,n2; char *s1,*s2; FILE *fp; int readf(FILE *fp) { if((fp=fopen("strings.txt", "r"))==NULL){ printf("ERROR: can't open string.txt!\n"); return 0; } s1=(char *)malloc(sizeof(char)*MAX); if(s1==NULL){ printf("ERROR: Out of memory!\n"); return -1; } s2=(char *)malloc(sizeof(char)*MAX); if(s2==NULL){ printf("ERROR: Out of memory\n"); return -1; } /*read s1 s2 from...
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...
Q1. Write a Java program to do sequential search to find element 55 in array 10,20,35,45,55,65,75,85....
Q1. Write a Java program to do sequential search to find element 55 in array 10,20,35,45,55,65,75,85.                                                                                                                                                                    Q2.Write a Java program to find element 54 in array 45,41,65,53,76,90 using Binary Search. (Hint: Binary search is applied to sorted array elements) Q4.   Write a java program to create array list subject        - add English, Maths, Science to the list        - add Computers at index 2        - display first occurrence index of Maths        - traverse the list using...
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]
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT