Question

In: Computer Science

PLEASE COMPLETE BOTH PARTS (LANGUAGE: C++ if necessary) Part I For the following example, determine the...

PLEASE COMPLETE BOTH PARTS (LANGUAGE: C++ if necessary)

Part I

For the following example, determine the number of comparisons that will take place during a search using both searching algorithms (Linear search and binary search). Also, list each comparison for each algorithm(Linear and binary). The number being searched for is 13.

List = {1,3,5,9,10,11,15,17,19,23,24,25,29,31,33,37,38,39,43,45,47,51,52,53,57,59,61}

Part 2

For the following example, determine the number of swaps that will take place during a sort using both sorting algorithms (bubble and selection sort). Also, list each swap for each algorithm (bubble and selection)

List = {23, 10, 3, -5, 2, 7, 0, 20, 15, 17, 9, 11, 4, 1}

Solutions

Expert Solution


#include <iostream>

using namespace std;

int c=0;
int s=0;

void swap(int *xp, int *yp)
{
    cout<<"Swapped "<<*xp<<" and "<<*yp<<"\n";
    int temp = *xp;
    *xp = *yp;
    *yp = temp;
}

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

    for (i = 0; i < n-1; i++)
    {
        min_idx = i;
        for (j = i+1; j < n; j++)
        if (arr[j] < arr[min_idx])
            min_idx = j;

        swap(&arr[min_idx], &arr[i]);
        s=s+1;
    }
}


void bubbleSort(int arr[],int n)//bubble sort to sort both vector on the basis of scores
{
    s=0;
    bool swapp = true;
    int temp;
    while(swapp)
    {
        swapp = false;
        for (int i = 0; i < n-1; i++)
            {
            if (arr[i]>arr[i+1])
                {
                    cout<<"Swapped "<<arr[i]<<" and "<<arr[i+1]<<"\n";
                    s=s+1;
                    temp=arr[i];
                    arr[i]=arr[i+1];
                    arr[i+1]=temp;
                    swapp = true;
                }
            }
    }
}

bool binary_search(int arr[], int key,int n)//function for searching names in name vector
{
    c=0;
   int mid, left = 0 ;
   int right = n;
   while (left < right) {
      mid = left + (right - left)/2;

      if (key > arr[mid])
        {
            c=c+1;
            cout<<"Compared "<<key<<" and "<<arr[mid]<<"\n";
            left = mid+1;
        }
      else if (key < arr[mid])
      {
          c=c+1;
          cout<<"Compared "<<key<<" and "<<arr[mid]<<"\n";
            right = mid;
      }
      else
      {

            return true;
      }
   }

   return false;
}
bool linear_search(int arr[], int key,int n)
{
    c=0;
    for (int i=0;i<n;i++)
    {
        c=i+1;
        cout<<"Compared "<<key<<" and "<<arr[i]<<"\n";
        if (arr[i]==key)
        {
            return true;
        }
    }
}

int main()
{

  // int arr[] = {1,3,5,9,10,11,15,17,19,23,24,25,29,31,33,37,38,39,43,45,47,51,52,53,57,59,61};

    int arr1[] = {23, 10, 3, -5, 2, 7, 0, 20, 15, 17, 9, 11, 4, 1};
        int n = sizeof(arr1)/sizeof(arr1[0]);
        selectionSort(arr1,n);
        cout << "Selection sort swaps: "<<s<<"\n";
        cout<<"__________________________________________________\n";

    int arr2[] = {23, 10, 3, -5, 2, 7, 0, 20, 15, 17, 9, 11, 4, 1};
    n = sizeof(arr2)/sizeof(arr2[0]);
        bubbleSort(arr2,n);
        cout << "bubble sort swaps: "<<s<<"\n";
        cout<<"__________________________________________________\n";

        //////////////////////////////////////////////////////////////
        int arr3[]={1,3,5,9,10,11,15,17,19,23,24,25,29,31,33,37,38,39,43,45,47,51,52,53,57,59,61};
    n = sizeof(arr3)/sizeof(arr3[0]);
    linear_search(arr3,13,n);
    cout << "Linear search comparisons: "<<c<<"\n";
        cout<<"__________________________________________________\n";

    binary_search(arr3,13,n);
    cout << "binary search comparisons: "<<c<<"\n";
        cout<<"__________________________________________________\n";
    return 0;

}

IF YOU HAVE ANY DOUBT THEN JUST LEAVE A COMMENT AND I WILL CONNECT WITH YOU AS SOON AS POSSIBLE


Related Solutions

Please complete both parts. Part I (1) Jill, a supervisor at a construction company, distributes payroll...
Please complete both parts. Part I (1) Jill, a supervisor at a construction company, distributes payroll and has authority to hire and fire employees. How might the owner of the construction company determine whether Jill has fictitious employees on the payroll and is secretly cashing their payroll checks for herself? (2) Please explain why it is desirable to have at least two officials approve pay rate changes. Part II Jet-Clean sold washing machines totaling $1 million. Each washing machine carries...
Part I and Part II are independent. Please answer both parts. Part I: During a year...
Part I and Part II are independent. Please answer both parts. Part I: During a year of operation, a firm collects $450,000 in revenue and spends $100,000 on labor expense, raw materials, rent and utilities. The firm’s owner has provided $750,000 of her own money instead of investing the money and earning a 10 percent annual rate of return. 1A. The accounting costs of the firm are 1B. The opportunity cost is 1C. Total economic costs are 1D. Accounting profits...
Part 1 and Part II are independent. Please answer both parts. Part I: You are advising...
Part 1 and Part II are independent. Please answer both parts. Part I: You are advising company ABC on its merger and acquisition case. The buyer company offers ABC two options. Option #1= $100 million cash at the acquisition date. Option #2 = $25 million cash at the acquisition date and another additional $90 million AFTER one year. The management team of ABC perceives a 30 percent annual discount rate. Which option should ABC choose? Show your work. Part II:...
I NEED THE ANSWER FOR BOTH THE PARTS PLEASE Part (I) Year Investment A Investment B...
I NEED THE ANSWER FOR BOTH THE PARTS PLEASE Part (I) Year Investment A Investment B 0 -$5,000,000 -5,000,000 1 $1,500,000 $1,250,000 2 $1,500,000 $1,250,000 3 $1,500,000 $1,250,000 4 $1,500,000 $1,250,000 5 $1,500,000 $1,250,000 6 $1,500,000 $1,250,000 7 $2,000,000 $1,250,000 8 0 $1,600,000 Calculate Payback period, NPV, IRR and profitability Index for the two projects Part (II) You have been hired as a consultant for Pristine Urban-Tech Zither, Inc. (PUTZ), manufacturers of fine zithers. The market for zithers is growing...
Complete the journal entries as necessary for both Part 1 and Part 2. Part 1. Transaction...
Complete the journal entries as necessary for both Part 1 and Part 2. Part 1. Transaction 1. On January 1st of 2020, Casey bought 10% of Apple Company’s 100,000 shares of outstanding common stock at $20 a share. 2. On December 31, 2020, Apple reported $40,000 of net income and paid $20,000 of dividends. 3. On December 31, 2020, the market price of the stock was $ 25 a share. Assume there was a zero balance in the fair value...
Complete the journal entries as necessary for both Part 1 and Part 2. Part 1. Transaction...
Complete the journal entries as necessary for both Part 1 and Part 2. Part 1. Transaction 1. On January 1st of 2020, Casey bought 10% of Apple 100,000 shares of outstanding common stock at $20 a share. 2. On December 31, 2020 Apple reported $40,000 of net income and paid $20,000 of dividends. 3. On December 31, 2020, the market price of stock was $ 25 a share. Assume there was a zero balance in the fair value adjustment account....
I HAVE ALREADY CORRECTLY COMPLETED PART A AND B PLEASE COMPLETE PART C ONLY Part A...
I HAVE ALREADY CORRECTLY COMPLETED PART A AND B PLEASE COMPLETE PART C ONLY Part A In late 2020, the Nicklaus Corporation was formed. The corporate charter authorizes the issuance of 6,000,000 shares of common stock carrying a $1 par value, and 2,000,000 shares of $5 par value, noncumulative, nonparticipating preferred stock. On January 2, 2021, 4,000,000 shares of the common stock are issued in exchange for cash at an average price of $10 per share. Also on January 2,...
Can you explain and answer part e and part f please? I already understand parts c...
Can you explain and answer part e and part f please? I already understand parts c and d Firm 1 and Firm 2 are functioning in a market as competitors. The inverse market demand for chicken is given by P (Y ) = 100 − 2Y , and the total cost function for any firm in the industry if given by TC(y) = 4y. (c) Suppose that two Cournot firms operated in the market and the reaction firm for Firm...
IN C++ ONLY... please do both parts of this task ..1 part- Menu driven program First...
IN C++ ONLY... please do both parts of this task ..1 part- Menu driven program First load an array(lists) with the values 3, 4, 84, 5, 2, 47, and 7 in this order by using a function. Then sort the array(list) Use a function to display the menu. Create a program with a menu, with choices 1 for factorial (single value, which everyone will use 20!), 2 for median value , 3 for average, 4 for maximum, 5 for minimum,...
Complete the following two parts. Your responses to both Part 1 and 2 should total 2...
Complete the following two parts. Your responses to both Part 1 and 2 should total 2 to 3 double-spaced pages in Microsoft Word, using 12-point font. Follow APA style for citations and references. Part 1 Consider the following scenario: Central Manufacturing Company needs 20,000 units of a certain part to use in its production cycle. The following information is available: Cost to Central to make the part: Direct materials â $4.00. Direct labor â $16.00. Variable factory overhead â $15.00....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT