Question

In: Computer Science

In C++: In this lab we will creating twos arrays of Generic objects, searching through those...

In C++:

In this lab we will creating twos arrays of Generic objects, searching through those arrays using BINARY and SEQUENTIAL sorts, calculate the run-time of each method, and determine the BIG O, OMEGA and THETA notation.

In our object we should have some way to compare elements to determine if one element is LESS THAN, GREATER THAN, and EQUAL TO. After this Generic Class is defined with a method that allows for comparison is completed, then you will create 2 arrays, one which is unsorted, the other which is sorted from the smallest to the largest element.

You will write one method that performs the SEQUENTIAL SEARCH for an individual element on an array. This method should be named SequentialSearch, and should take in the parameters of an Generic array and a Generic Element to be searched for. If the element exist in the array return its index, if the element doesn't exist return -1.

You will write one method that performs the BINARY SEARCH for an individual element on an array. This method should be named BinarySearch, and should take in the parameters of an Generic array and a Generic Element to be searched for. If the element exist in the array return its index, if the element doesn't exist return -1.

You will also count the number of operations that is performed in each method and will calculate the RUN-TIME of each method, as well as calculating the BIG O, OMEGA and THETA notation of each method. This information should be written in a comment block before each method ( you may count the number of operations on each line if you want ).

You are required to write self commenting code for this Lab, refer to the Google Style Sheet if you haven't become familiar with it.

Solutions

Expert Solution

#include <bits/stdc++.h>
using namespace std;

// The time complexity of Sequential Search is O(n), Theta(n), Omega(1).
// Best case when srch is present at the beginning
int SequentialSearch(int arr[], int n, int srch)
{ 
    int i, count=0; 
    for (i = 0; i < n; i++)
        {
                count++;
                // Check if srch is present at ith place
        if (arr[i] == srch) 
        {
                cout<<"Total operations used using Sequential search: "<<count<<endl;
            return i; 
                }
        }        
    // if we reach here, then element was not present
        cout<<"Total operations used using Sequential search: "<<count<<endl; 
    return -1; 
} 

// The time complexity of Binary Search is O(log n), Theta(log n), Omega(1).
// Best case when srch is present at middle
int BinarySearch(int arr[], int l, int r, int srch) 
{ 
        int count=0;
    while (l <= r) 
        { 
                count++;
        int m = l + (r - l) / 2; 
  
        // Check if srch is present at mid 
        if (arr[m] == srch)
                {
                        cout<<"Total operations used using Binary search: "<<count<<endl;
                        return m;
                } 
  
        // If srch greater, ignore left half 
        if (arr[m] < srch) 
            l = m + 1; 
  
        // If srch is smaller, ignore right half 
        else
            r = m - 1; 
    } 
  
    // if we reach here, then element was not present
        cout<<"Total operations used using Binary search: "<<count<<endl; 
    return -1; 
}

void printArray(int array[], int n)
{
        for(int i=0; i<n; i++)
        {
                cout<<array[i]<<" ";
        }
        cout<<endl;
}

int main()
{
        int n=10, res1, res2, val1, val2;
        cout<<"Sequential Search...\n";
        int arr[]={9, 39, 20, 11, 32, 45, 67, 82, 1, 96};
        sort(arr, arr+n);
        printArray(arr, n);
        res1 = SequentialSearch(arr, n, 82);
        if(res1==-1)
                cout<<82<<" is not present!";
        else
                cout<<82<<" found at index: "<<res1<<endl;
                
        cout<<"Binary Search...\n";
        int brr[]={46, 63, 77, 91, 51, 30, 12, 8, 99, 75};
        sort(brr, brr+n);
        printArray(brr, n);
        res2 = BinarySearch(brr, 0, n, 77);
        if(res2==-1)
                cout<<77<<" is not present!";
        else
                cout<<77<<" found at index: "<<res2<<endl;
        
        return 0;
}

I have typed everything in the code itself. Hope it will help.

If you require anything else post it in the comment section please.

Good Luck :)


Related Solutions

In C++ In this lab we will be creating a stack class and a queue class,...
In C++ In this lab we will be creating a stack class and a queue class, both with a hybrid method combining linked list and arrays in addition to the Stack methods(push, pop, peek, isEmpty, size, print) and Queue methods (enqueue, deque, peek, isEmpty, size, print). DO NOT USE ANY LIBRARY, implement each method from scratch. Both the Stack and Queue classes should be generic classes. Don't forget to comment your code.
In C++ In this lab we will creating two linked list classes: one that is a...
In C++ In this lab we will creating two linked list classes: one that is a singly linked list, and another that is a doubly linked list ( This will be good practice for your next homework assignment where you will build your own string class using arrays and linked list ) . These LinkedList classes should both be generic classes. and should contain the following methods: Print Add - Adds element to the end of the linked list. IsEmpty...
In C++ please: In this lab we will creating two linked list classes: one that is...
In C++ please: In this lab we will creating two linked list classes: one that is a singly linked list, and another that is a doubly linked list ( This will be good practice for your next homework assignment where you will build your own string class using arrays and linked list ) . These LinkedList classes should both be generic classes. and should contain the following methods: Print Add - Adds element to the end of the linked list....
Lab 1 – Databases, Schemas, and Basic Tables For this lab we will be creating a...
Lab 1 – Databases, Schemas, and Basic Tables For this lab we will be creating a small Student Loan Database. Make sure to open your screenshot word document. Include the required screenshots of your code in the document. Database: Create a new database: StudentLoan_LastName. Schemas: Create the following schemas. Make sure to screenshot and run your code: 1. Student 2. Guarantor 3. Institution 4. Activity 5. Loan 6. Lender Tables: First complete the word document for designing the tables like...
We started creating a Java class for Car. In this lab we are going to complete...
We started creating a Java class for Car. In this lab we are going to complete it in the following steps: 1- First create the Car class with some required instance variables, such make, model, year, price, speed, maxSpeed, isOn, isMoving, and any other properties you like to add. 2- Provide couple of constructors for initializing different set of important properties, such as make, model, year, and price. Make sure that you do not repeat initialization of instance variables in...
In Java In this lab we will creating two linked list classes: one that is a...
In Java In this lab we will creating two linked list classes: one that is a singly linked list, and another that is a doubly linked list ( This will be good practice for your next homework assignment where you will build your own string class using arrays and linked list ) . These LinkedList classes should both be generic classes. and should contain the following methods: Print Add - Adds element to the end of the linked list. IsEmpty...
9.25 LAB*: Program: Online shopping cart (Part 2) Note: Creating multiple Scanner objects for the same...
9.25 LAB*: Program: Online shopping cart (Part 2) Note: Creating multiple Scanner objects for the same input stream yields unexpected behavior. Thus, good practice is to use a single Scanner object for reading input from System.in. That Scanner object can be passed as an argument to any methods that read input. This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program). (1) Extend the ItemToPurchase class per the following specifications: Private fields string itemDescription -...
You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with...
You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with //comments . Please include a screen shot of the output Part 4: Calorie Counting Specifications: Write a program that allows the user to enter the number of calories consumed per day. Store these calories in an integer vector. The user should be prompted to enter the calories over the course of one week (7 days). Your program should display the total calories consumed over...
You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with...
You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with //comments . Please include a screen shot of the output Part 1: Largest and Smallest Vector Values Specifications: Write a program that generates 10 random integers between 50 and 100 (inclusive) and puts them into a vector. The program should display the largest and smallest values stored in the vector. Create 3 functions in addition to your main function. One function should generate the...
Write a C++ program to compare those two searching algorithms and also compare two sorting algorithms....
Write a C++ program to compare those two searching algorithms and also compare two sorting algorithms. You need to modify those codes in the book/slides to have some counters to count the number of comparisons and number of swaps. In the main function, you should have an ordered array of 120integers in order to test searching algorithms, and the other two identical arrays of 120integers not in order to test sorting algorithms. Display all counters in the main functions.Counter for...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT