Question

Benchmark Searching and Sorting Write a program that has an array of at least 20 strings...

Benchmark Searching and Sorting


Write a program that has an array of at least 20 strings that you will have your user enter. 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 uses the binary search algorithm to locate the same value. It should also keep count of the number of comparisons it makes. Display these values on the screen.

For sorting, your program should call a function that uses the bubble sort algorithm to sort one of the arrays in ascending order. The function should keep a count of the number of exchanges it makes. The program then should call a function that uses the selection sort algorithm to sort the other array. It should also keep count of the number of exchanges it makes. Display these values on the screen.

Programs in C++ please!

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

//Note: -> I have checked it for 4 strings ...

//c++ code

#include<iostream>
#include<string>
using namespace std;
//v is the value to be searched, n is the size of array
// s is the array itself
int linearSearch(string s[], int n, string v);
int binarySearch(string s[], int n,string v);
int bubbleSort(string s[], int n);
int selectionSort(string s[], int n);
void print(string s[], int n);
int main()
{
   int const SIZE = 20;
   string values[SIZE];
   cout << "Enter " << SIZE << " values: " << endl;
   for (int i = 0; i < SIZE; i++)
   {
       cin >> values[i];
   }
   string temp1[4],temp2[4];
   for (int i = 0; i < SIZE; i++)
   {
       temp1[i]= values[i];
       temp2[i] = values[i];
   }
   cout << "Enter value to be searched: ";
   string v;
   cin >> v;
   int n1 = linearSearch(values, SIZE, v);
   int n2 = binarySearch(values, SIZE, v);
   int n3 = selectionSort(temp1, SIZE);
   int n4 = bubbleSort(temp2, SIZE);
   cout << "Number of comparison in linear Search: " << n1 << endl;
   cout << "Number of comparison in binary Search: " << n2 << endl;
   cout << "Number of exchange in selection sort: " << n3 << endl;
   cout << "Number of exchange in bubble sort: " << n4 << endl;
   print(temp1, SIZE);
   print(temp2, SIZE);
   //pause
   system("pause");
   return 0;
}
void print(string s[], int n)
{
   for (int i = 0; i < n; i++)
   {
       cout << s[i] << " ";
   }
   cout << "\n";
}
int linearSearch(string s[], int n, string v)
{
   int count = 0;
   for (int i = 0; i < n; i++)
   {
       if (s[i] == v)
       {
           count++;
           cout << "Found...." << endl;
           break;
       }
   }
   return count;
}
int binarySearch(string s[], int n, string v)
{
   int l = 0;
   int r = n - 1;
   int count = 0;
   while (l <= r)
   {
       count++;
       int m = l + (r - l) / 2;
       if (s[m] == v)
           return count;
       if (s[m] < v)
           l = m + 1;
       else
           r = m - 1;
   }
   return count;
}
int bubbleSort(string s[], int n)
{
   int count = 0;
   for (int i = 0; i < n; i++)
   {
       for (int j = i + 1; j < n; j++)
       {
           if (s[i] > s[j])
           {
               string temp = s[i];
               s[i] = s[j];
               s[j] = temp;
               count++;
           }
       }
   }
   return count;
}
int selectionSort(string s[], int n)
{
   int minIndex = 0,count=0;
   for (int i = 0; i < n - 1; i++)
   {
       minIndex = i;
       for (int j = i + 1; j < n; j++)
       {
           if (s[j] < s[minIndex])
           {
               minIndex = j;
           }
       }
       string temp = s[i];
       s[i] = s[minIndex];
       s[minIndex] = temp;
       count++;
   }
   return count;
}

//Output

//If you need any help regarding this solution....... please leave a comment....... thanks

Add a comment
Know the answer?
Add Answer to:
Benchmark Searching and Sorting Write a program that has an array of at least 20 strings...
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
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