Question

Data Structures, C++, SORT running time. Would help is small explanation is included. Problem 1. Select the worst-case running time of each function. bool search (int x, int* A, int n) if (linear search(x, A, n)) return true; insertion_sort(A, n); return binary_search (x, A, n); bool search_n_sort (int* A, int n) for (int x = 1; x <= n; ++x) if (linear_search(x, A, n)) ae()og(n) n) return true; insertion_sort(A, n); return false; bool sort_n_search (int* A, int n) insertion_sort(A, n); for (int x = 1; x <= n; ++x) if (binary_search(x, A, n)) return true; return false; void very_sort (int* A, int n) for (int i θ; i < n; ++i) insertionsort (A, n);

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

Worst-case running time of insertionSort(A, n) = Θ(n2)

Worst-case running time of binary_Search(A, n) = Θ(log(n))

Worst-case running time of linear_search(A, n) = Θ(n)

a) Worst-case running time of search method will happen when the linear search method returns false. In that case, we go on and sort the array using insertion sort and then search x using binary search.

Hence, worst-case running time = O(n2) [for insertion sort] + O(log(n)) [for binary search]

= O(n2)

b) Worst-case running time of search_and_sort method will happen when the linear search method returns false for x ranging from 1 to n. In that case, we go on and sort the array using insertion sort.

Hence, worst-case running time = O(n2) [for linear search for x ranging from 1 to n] + O(n2) [for insertion sort] = O(n2)

c) Worst-case running time of sort_and_search method will happen when the binary search method returns false for x ranging from 1 to n.

Hence, worst-case running time = O(n2) [for insertion sort] + O(n log(n)) [for binary search for x ranging from 1 to n]

= O(n2) (Since n2 >= nlogn for larger values of n)

d) Please note that the best running time complexity of insertion sort is O(n) when the array is already sorted.

In this case, for i=0, we run insertion sort to sort the array. For subsequent runs of insertion sort, it takes O(n) time since the array is already sorted.

Hence, the worse-case running time complexity = O(n2) [for first insertion sort] + (n-2) * O(n) [for subsequent insertion sorts] = O(n2)

Add a comment
Know the answer?
Add Answer to:
Data Structures, C++, SORT running time. Would help is small explanation is included. Problem 1. Select...
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
  • Hello, I need help with my code. The code needs to display random number from 1...

    Hello, I need help with my code. The code needs to display random number from 1 to 50 every time the program runs but the program displays the same random numbers every time. Thanks #include #include using namespace std; void dynAlloc(int size, int *&arr); void displayArray(int *arr, int n); void insertionSort(int *arr, int n, int *temp); void linear_search(int *arr, int n, int key); void binary_search(int *arr, int n, int key); int main() {   const int n = 50; int *arr,...

  • Problem 1. Select the running time of each function. void print_array (int* A, int n) for...

    Problem 1. Select the running time of each function. void print_array (int* A, int n) for (int í 0; i < n; ++i) cout << A[i] << endl; void print_array pairs (int* A, int n) for (inti 0; i < n; ++i) for (int j 0; j < n; ++j) cout << Ai] ALj]< endl; void print_array_start(int* A, int n) for (int i 0; i < 100 ; ++i) cout << A[i] << endl; void print_array_alt (int* A, int n)...

  • C++ Time the sequential search and the binary search methods several times each for randomly generated...

    C++ Time the sequential search and the binary search methods several times each for randomly generated values, then record the results in a table. Do not time individual searches, but groups of them. For example, time 100 searches together or 1,000 searches together. Compare the running times of these two search methods that are obtained during the experiment. Regarding the efficiency of both search methods, what conclusion can be reached from this experiment? Both the table and your conclusions should...

  • Can someone help with this C++ code. I am trying to compile and I keep running...

    Can someone help with this C++ code. I am trying to compile and I keep running into these 4 errors. #include <iostream> #include <cassert> #include <string> using namespace std; typedef int fsm_state; typedef char fsm_input; bool is_final_state(fsm_state state) { return (state == 3) ? true : false; } fsm_state get_start_state(void) { return 0; } fsm_state move(fsm_state state, fsm_input input) { // our alphabet includes only 'a' and 'b' if (input != 'a' && input != 'b') assert(0); switch (state) {...

  • Select all the valid asymptotic runtime bounds for the following function f2 in the worst case:...

    Select all the valid asymptotic runtime bounds for the following function f2 in the worst case: public static int f1 (int n) { int x = 0; for (int i = 0; i < n; i++) { x++; } return x; } public static int f2 (int n) { if (n <= 1) { return 1; } return f1(n) + f2(n/2) + f2(n/2); } Θ(n^2) O(n^2) Θ(log(n)) Θ(log^2(n)) Θ(nlog(n)) Ω(n) Ω(n^2)

  • 4. Big-Oh and Rune time Analysis: describe the worst case running time of the following pseudocode...

    4. Big-Oh and Rune time Analysis: describe the worst case running time of the following pseudocode functions in Big-Oh notation in terms of the variable n. howing your work is not required (although showing work may allow some partial t in the case your answer is wrong-don't spend a lot of time showing your work.). You MUST choose your answer from the following (not given in any particular order), each of which could be re-used (could be the answer for...

  • I NEED HELP IN MAZE PROBLEM. Re-write the following program using classes. The design is up...

    I NEED HELP IN MAZE PROBLEM. Re-write the following program using classes. The design is up to you, but at a minimum, you should have a Maze class with appropriate constructors and methods. You can add additional classes you may deem necessary. // This program fills in a maze with random positions and then runs the solver to solve it. // The moves are saved in two arrays, which store the X/Y coordinates we are moving to. // They are...

  • c++, I am having trouble getting my program to compile, any help would be appreciated. #include...

    c++, I am having trouble getting my program to compile, any help would be appreciated. #include <iostream> #include <string> #include <string.h> #include <fstream> #include <stdlib.h> using namespace std; struct record { char artist[50]; char title[50]; char year[50]; }; class CD { //private members declared private: string artist; //asks for string string title; // asks for string int yearReleased; //asks for integer //public members declared public: CD(); CD(string,string,int); void setArtist(string); void setTitle(string); void setYearReleased(int); string getArtist() const; string getTitle() const; int...

  • I need help with the following and written in c++ thank you!: 1) replace the 2D...

    I need help with the following and written in c++ thank you!: 1) replace the 2D arrays with vectors 2) add a constructor to the Sudoku class that reads the initial configuration from a file 3) adds a function to the Sudoku class that writes the final Sudoku grid to a file or to the standard output device, cout. Sudoku.h #pragma once /* notes sudoku() default constructor precondition : none postcondition: grid is initialized to 0 sudoku(g[][9]) 1-parameter constructor precondition...

  • C++, data structure Write a solution to test 2 problem 3 that uses selection sort to sort the ite...

    c++, data structure Write a solution to test 2 problem 3 that uses selection sort to sort the items in the vector of integers. #include <iostream> #include <iterator> #include <string> #include <random> using namespace std; void insertionSort(int a[], int N) {    int sorted = 0;    for (int sorted = 0; sorted < N - 1; ++sorted)    {        int item_position = sorted + 1;        int item = a[item_position];        while (item_position > 0 && a[item_position - 1] > item)        {            a[item_position] =...

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