Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Modify is Program to include for option 2- binary search two additional submenus

ID: 3671219 • Letter: M

Question

Modify is Program to include for option 2- binary search two additional submenus (b- bubble sort, and s – selection sort) that allows the user to choose the sorting method prior to binary search. The sorted array should be displayed and then the user should be allowed to choose a search value.

#include

using namespace std;

void linearsearch(int a[], int num, int x);

int binarysearch(int a[], int num, int x);

int main()

{

                int a[10], k, x, ch, ans, i;

                x = 10;

                cout << "Please Enter 10 number " << endl;

                for (i = 0; i

                                cin >> a[i];

                }

                while (1){

                                cout << "Enter:" << endl;

                                cout << "1. For linear search" << endl;

                                cout << "2. For binary search" << endl;

                                cout << "0. to Exit" << endl;

                                cout << "Enter your choice" << endl;

                                cin >> ch;

                                switch (ch){

                                case 1:

                                                cout << "Enter the search value" << endl;

                                                cin >> k;

                                                linearsearch(a, k, x);

                                               

                                                break;

                                case 2:

                                                cout << "Enter the search value" << endl;

                                                cin >> k;

                                                ans = binarysearch(a, k, x);

                                                if (ans != -1)

                                                                cout << "the value " << k << " is found at position # " << ans << endl;

                                                else

                                                                cout << "Element " << k << " not found " << endl;

                                                break;

                                case 0:

                                                return 0;

                                default:

                                                cout << "Invalid entry " << endl;

                                }

                }

}

void linearsearch(int a[10], int num, int x)

{

                int i;

                for (i = 0; i

                                if (num == a[i])

                                                cout << "the value " << num<< " is found at position # " << i << endl;

                }

                cout << "Element " << num << " is not found" << endl;

}

int binarysearch(int a[10], int num, int x)

{

                int high, low, mid;

                low = 0;

                high = x - 1;

                while (low <= high){

                                mid = (low + high) / 2;

                                if (a[mid] == num)

                                                return mid;

                                else if (a[mid]

                                                low = mid + 1;

                                else

                                                high = mid - 1;

                }

                return -1;

}

Explanation / Answer

#include <iostream>

using namespace std;

void linearsearch(int a[], int num, int x);
int binarysearch(int a[], int num, int x);
void bubbleSort(int a[], int n);
void selectionSort(int a[], int n);

int main()

{

int a[10], k, x, ch, ans, i, choice;
x = 10;

cout << "Please Enter 10 number " << endl;

for (i = 0; i<10;i++)
    cin >> a[i];

while (1){
    cout << "Enter:" << endl;
    cout << "1. For linear search" << endl;
    cout << "2. For binary search" << endl;
    cout << "0. to Exit" << endl;
    cout << "Enter your choice" << endl;
    cin >> ch;

    switch (ch){

      case 1:
      cout << "Enter the search value" << endl;
      cin >> k;
      linearsearch(a, k, x);
      break;

      case 2:
      cout<<"Press 1 for bubble sort Press 2 for selection sort Enter Choice::";
      cin>>choice;
      if(choice == 1)
        bubbleSort(a,x);
      else if(choice == 2)
        selectionSort(a,x);
      else
        break;

      cout<<"Sorted array:: ";
      for(i=0;i<x;i++)
        cout<<a[i]<<" ";

      cout << " Enter the search value" << endl;
      cin >> k;
      ans = binarysearch(a, k, x);
      if (ans != -1)
        cout << "the value " << k << " is found at position # " << ans << endl;
      else
        cout << "Element " << k << " not found " << endl;
      break;

      case 0:
      return 0;

      default:
      cout << "Invalid entry " << endl;
    }
}
}

void linearsearch(int a[10], int num, int x)
{

int i;
for (i = 0; i < x; i++)
    if (num == a[i])
    {    
      cout << "the value " << num<< " is found at position # " << i << endl;
      break;
    }

cout << "Element " << num << " is not found" << endl;

}

int binarysearch(int a[10], int num, int x)

{

int high, low, mid;
low = 0;
high = x - 1;

while (low <= high){
    mid = (low + high) / 2;
    if (a[mid] == num)
      return mid;
    else if (a[mid] < num)
      low = mid + 1;
      else
        high = mid - 1;
    }

   return -1;
}

void swap(int *xp, int *yp)
{
    int temp = *xp;
    *xp = *yp;
    *yp = temp;
}

void bubbleSort(int arr[], int n)
{
   int i, j;
   for (i = 0; i < n-1; i++)    
       for (j = 0; j < n-i-1; j++) //Last i elements are already in place
           if (arr[j] > arr[j+1])
              swap(&arr[j], &arr[j+1]);
}


void selectionSort(int arr[], int n)
{
    int i, j, min_idx;

    // One by one move boundary of unsorted subarray
    for (i = 0; i < n-1; i++)
    {
        // Find the minimum element in unsorted array
        min_idx = i;
        for (j = i+1; j < n; j++)
          if (arr[j] < arr[min_idx])
            min_idx = j;

        // Swap the found minimum element with the first element
        swap(&arr[min_idx], &arr[i]);
    }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote