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
  • HOME WORK 9. Sorting Benchmarks Write a program that uses two identical arrays of at least...

    HOME WORK 9. Sorting Benchmarks Write a program that uses two identical arrays of at least 20 integers. It 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...

  • Java: Write an application that has an array of at least 20 integers. It should call...

    Java: Write an application that has an array of at least 20 integers. It should call a method that uses the sequential search algorithm to locate one of the values. The method should keep a count of the number of comparisons it makes until it finds the value. Then the program should call another method 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...

  • C++ Write a program that uses an arrays of at least 20 string . It should...

    C++ Write a program that uses an arrays of at least 20 string . It should call a function that uses the linear search algorithm to locate one of the name. Read list of name from an input file call "StudentName.txt" The function should keep a count of the numbers 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...

  • This is a beginner's C++ class. Please use basic C++ knowledge or approaches provided in the...

    This is a beginner's C++ class. Please use basic C++ knowledge or approaches provided in the first 10 chapters of "starting out with >>>C++ From Control Structures through Objects 9th Edition". I will leave a thumbs up rate if the code works and done quickly. Thank you. Write a program that uses two identical arrays of at least 20 integers. It should call a function that uses the bubble sort algorithm to sort one of the arrays in ascending order....

  • 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 ar...

    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...

  • Looking for a solution done in a Flowgorithm flowchart please. Design an application that has an...

    Looking for a solution done in a Flowgorithm flowchart please. Design an application that has an array of at least 20 integers. It should call a module that uses the sequential search algorithm to locate one of the values. The module should keep a count of the number of comparisons it makes until it finds the value. Then the program should call another module that uses the binary search algorithm to locate the same value. It should also keep a...

  • 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 ar...

    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...

  • What is the maximum number of comparisons made when searching a 60 element array with Binary...

    What is the maximum number of comparisons made when searching a 60 element array with Binary Search? 60 30 5 6 Question 3 (3 points) What is the average number of comparisons made when searching a 60 element array with Linear Search? 06 5 A selection sort algorithm is used to sort an array containing the following values into ascending order. Give the order of the elements after each pass of the sorting algorithm. 6 4 7 2 3 5...

  • Using Arrays with Sorting and Searching Algorithms 1) This program has six required outputs and involves...

    Using Arrays with Sorting and Searching Algorithms 1) This program has six required outputs and involves searching and sorting an array of integers. Write a java application that initializes an array with the following numbers, in this order: 23, 17, 5, 90, 12, 44, 38, 84, 77, 3, 66, 55, 1, 19, 37, 88, 8, 97, 25, 50, 75, 61, and 49. Then display the unsorted values. This is required output #1 of 6 for this program. 2) Using a...

  • C++ Sorting and Searching 1. Mark the following statements as true or false. a. A sequential...

    C++ Sorting and Searching 1. Mark the following statements as true or false. a. A sequential search of a list assumes that the list elements are sorted in ascending order. b. A binary search of a list assumes that the list is sorted. 2. Consider the following list: 63 45 32 98 46 57 28 100 Using a sequential search, how many comparisons are required to determine whether the following items are in the list or not? (Recall that comparisons...

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