Question

Implement both a brute-force and presorting-based algorithms (using C/C++) solving the problem below. Run the programs...

Implement both a brute-force and presorting-based algorithms (using C/C++) solving the problem below. Run the programs on same set of data and record and report the system-times for them.

Problem: Let A = {a1, …, an} and B = {b1, …, bm} be two sets of numbers. Consider the problem of finding their intersection, i.e., the set C of all the numbers that are in both set A and set B.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//C++ program

#include<iostream>
#include <chrono>
#include <ctime>
using namespace std;

void intersection(int a[],int b[],int n,int m){
   for(int i=0;i<n;i++){
       for(int j=0;j<m;j++){
           if(b[j]==a[i]){
               cout<<a[i]<<" ";
               break;
           }
       }
   }
}
int findpivot(int a[],int l,int r){
   int pivot=a[r];
   int i=0,j=-1;
   while(i<r){
       if(a[i]<pivot){j++;
           int temp=a[i];
           a[i]=a[j];
           a[j]=temp;
          
       }
       i++;
   }
  
   a[i]=a[++j];
   a[j]=pivot;
   return j;
}

void quicksort(int a[],int l,int r){
   int pivot;
   if(l<r){
       pivot=findpivot(a,l,r);
       quicksort(a,l,pivot-1);
       quicksort(a,pivot+1,r);
   }
}
void inter(int a[], int b[], int n, int m)
{
int i = 0, j = 0;
  
while (i < n && j < m)
{
  
if (a[i] > b[j])
{
j++;
}
  
else
if (b[j] > a[i])
{
i++;
}
else
{
cout << a[i] << " ";
i++;
j++;
}
}
}
int main(){
   std::chrono::time_point<std::chrono::system_clock> start, end;
   double time;
   std::chrono::duration<double> elapsed_seconds;

   int n =5 , m=6;
   int a[n] ,b[m];
   cout<<"Enter "<<n<<" element of array A\n";
   for(int i=0;i<n;i++)cin>>a[i];
   cout<<"Enter "<<m<<" element of array B\n";
   for(int i=0;i<m;i++)cin>>b[i];
  
  
   cout<<"Intersection of both array without sorting \n";
   start = std::chrono::system_clock::now();
   intersection(a,b,n,m);
   end = std::chrono::system_clock::now();
elapsed_seconds = end - start;
time= elapsed_seconds.count();
   cout<<"Time Taken : "<<time<<"\n";
  
   cout<<"\n\nsorting array A \n";
   quicksort(a,0,n-1);
   cout<<"\n\nsorting array B \n";
   quicksort(b,0,m-1);
  
   cout<<"\nIntersection of both array with sorting \n";
   start = std::chrono::system_clock::now();
   inter(a,b,n,m);
   end = std::chrono::system_clock::now();
elapsed_seconds = end - start;
time= elapsed_seconds.count();
   cout<<"Time Taken : "<<time<<"\n";
  
   return 0;
}

//sample output

Add a comment
Know the answer?
Add Answer to:
Implement both a brute-force and presorting-based algorithms (using C/C++) solving the problem below. Run the programs...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1. Let A = {a1, ..., an} and B = {b1, ..., bm} be two sets...

    1. Let A = {a1, ..., an} and B = {b1, ..., bm} be two sets of numbers. Consider the problem of finding their intersection, i.e., the set C of all the numbers that are in both A and B. a. Design a presorting based algorithm for solving this problem and determine its efficiency class. 2. Estimate how many searches will be needed to justify time spent on presorting an array of 103 elements if sorting is done by mergesort...

  • Program by force brute in (c++ or java language )

    The question of validity is to answer the question of whether for a Boolean expression consisting of n variables . There is a combination of values of variables that make the result of the expression true or not. In this phrase each The variable can be simple or contradictory. Force brute method of all compounds . Creates and evaluates the possible and the first time the result is true (if it is) the answer to the problem Come and stop...

  • Arrays Arravs Introduction Your ninth programming assignment will consist of two C++programs. Your programs should compile...

    Arrays Arravs Introduction Your ninth programming assignment will consist of two C++programs. Your programs should compile correctly and produce the specified output. Please note that your programs should comply with the commenting and formatting rules we discussed in class. Please see the descriptive ile on eLearning for details. Program 1- Linear Search Algorithm In Computer Science, it is often very important to be able to locate a specific data item inside a list or collection of data. Algorithms that perform...

  • DESCRIPTION Implement a program in C++ that generates a specified number of random integers, records them...

    DESCRIPTION Implement a program in C++ that generates a specified number of random integers, records them in three arrays, then sorts the arrays with Insertion Sort, Merge Sort, and Quick Sort, respectively. Augment the three sorting algorithms with counters and report the number of characteristic operations each performs in sorting the (same) random values. INPUT The program reads from the terminal the number of random integers to generate, a seed value for the pseudo-random number generator, and a character that...

  • 10. Consider the Traveling Salesperson problem (a) Write the brute-force algorithm for this proble that considers...

    10. Consider the Traveling Salesperson problem (a) Write the brute-force algorithm for this proble that considers (b) Implement the algorithm and use it to solve instances of size 6, 7, (c) Compare the performance of this algorithm to that of Algorithm all possible tours 8, 9, 10, 15, and 20 6.3 using the instances developed in (b) Algorithm 6.3 The Best-First Search with Branch-and-Bound Pruning Algorithm for the Traveling Salesperson problem Problem: Determine an optimal tour in a weighted, directed...

  • In C++ language, implement a class that can sort an array of numbers using all three...

    In C++ language, implement a class that can sort an array of numbers using all three algorithms we have seen in this course, but each method updates a “counter” value every time it accesses the array. Have it print this at the end of the sorting process. Store the array values in an “original” array so you don’t have to re-type it for different sorts (since each sort alters the array), and have the sort modify a copy. Note: IF...

  • In C++ language, implement a class that can sort an array of numbers using all three...

    In C++ language, implement a class that can sort an array of numbers using all three algorithms we have seen in this course, but each method updates a “counter” value every time it accesses the array. Have it print this at the end of the sorting process. Store the array values in an “original” array so you don’t have to re-type it for different sorts (since each sort alters the array), and have the sort modify a copy. Note: IF...

  • Problem statement For this program, you are to implement a simple machine-learning algorithm that uses a...

    Problem statement For this program, you are to implement a simple machine-learning algorithm that uses a rule-based classifier to predict whether or not a particular patient has diabetes. In order to do so, you will need to first train your program, using a provided data set, to recognize a disease. Once a program is capable of doing it, you will run it on new data sets and predict the existence or absence of a disease. While solving this problem, you...

  • c++ Program 2: Coin Toss Simulator For this program, please implement Problems 12 and 13 in...

    c++ Program 2: Coin Toss Simulator For this program, please implement Problems 12 and 13 in a single program (Gaddis, p812, 9E). Scans of these problems are included below. Your program should have two sections that correspond to Problems 12 and 13: 1) As described in Problem 12, implement a Coin class with the specified characteristics. Run a simulation with 20 coin tosses as described and report the information requested in the problem. 2) For the second part of your...

  • Major Homework #2 Implement a C program major_hw2.c to solve the 15-puzzle problem using the A*...

    Major Homework #2 Implement a C program major_hw2.c to solve the 15-puzzle problem using the A* search algorithm. 1. Objectives • To gain more experience on using pointers and linked lists in C programs. • To learn how to solve problems using state space search and A* search algorithm. 2. Background A* search and 15-puzzle problem have been introduced in the class. For more information, please read the wiki page of 15-puzzle problem at https://en.wikipedia.org/wiki/15_puzzle, and the wiki page of...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT