Question

In: Computer Science

I would like to integrate a bubble sort into this binary search in c ++ Thank...

I would like to integrate a bubble sort into this binary search in c ++ Thank you!

#include <iostream>

using namespace std;
// Binary search algorith
// f is the first , l is the last , t is the target
int binarySearch(int stgrade[], int f, int l, int t)
{
        while (f <= l)
        {
            int m = f + (l - l) / 2;

                // Check if x is present at mid
            if (stgrade[m] == t)
                return m;

                // If x greater, ignore left half
            if (stgrade[m] < t)
                f = m + 1;

                // If x is smaller, ignore right half
            else
                l = m - 1;
        }

        // if we reach here, then element was not present
        return -1;
}
int main(void)
{
    int target;
    int stgrade[] = { 100, 73, 94, 100, 70, 86, 55, 90, 74 };
    // size of the array
    int s = sizeof(stgrade) / sizeof(stgrade[0]);

    cout << "This is your list" << endl;
    for (int i = 0; i < s; i++)
        cout << stgrade[i] << "    ";
    cout << endl;

    cout << "Input the target to find , in or out side of your list" << endl;
    cin >> target;

    // 0 is the index of 1st element
    // s is the size and s-1 is in dext of last element
    // target is what you want to find

    int result = binarySearch(stgrade, 0, s - 1 , target);
   
    if (result == -1)
        cout << "Your target NOT FOUND in the list" << endl;
    else
        cout << "Your target is in the list " << result + 1 << endl;

    system("pause");

    return 0;
}

Solutions

Expert Solution

#include <iostream>

using namespace std;

// Binary search algorith
// f is the first , l is the last , t is the target
int binarySearch(int stgrade[], int f, int l, int t) {
    while (f <= l) {
        int m = f + (l - l) / 2;
        // Check if x is present at mid
        if (stgrade[m] == t)
            return m;
        // If x greater, ignore left half
        if (stgrade[m] < t)
            f = m + 1;
            // If x is smaller, ignore right half
        else
            l = m - 1;
    }
    // if we reach here, then element was not present
    return -1;
}

// Bubble sort algorithm
// size is the size of the array
void bubbleSort(int stgrade[], int size) {
    int temp;
    for (int i = 0; i < size; ++i) {
        for (int j = 0; j < size - 1; ++j) {
            if (stgrade[j] > stgrade[j+1]) {
                temp = stgrade[j];
                stgrade[j] = stgrade[j + 1];
                stgrade[j + 1] = temp;
            }
        }
    }
}

int main(void) {
    int target;
    int stgrade[] = {100, 73, 94, 100, 70, 86, 55, 90, 74};
    // size of the array
    int s = sizeof(stgrade) / sizeof(stgrade[0]);

    bubbleSort(stgrade, s); // sort the array

    cout << "This is your list" << endl;
    for (int i = 0; i < s; i++)
        cout << stgrade[i] << "    ";
    cout << endl;
    cout << "Input the target to find , in or out side of your list" << endl;
    cin >> target;
    // 0 is the index of 1st element
    // s is the size and s-1 is in next of last element
    // target is what you want to find
    int result = binarySearch(stgrade, 0, s - 1, target);
    if (result == -1)
        cout << "Your target NOT FOUND in the list" << endl;
    else
        cout << "Your target is in the list " << result + 1 << endl;
    system("pause");
    return 0;
}


Related Solutions

Rank the algorithms from slowest to fastest. . Bubble Sort Linear Search Binary Search Shellsort
Rank the algorithms from slowest to fastest. . Bubble Sort Linear Search Binary Search Shellsort
How would I make a bubble sort and an optimized bubble sort with the code given?...
How would I make a bubble sort and an optimized bubble sort with the code given? I also need to implement a timer into each sort and display runtime with the sorts. NODE.H _______________________________________________________________________________________________________ /* node.h */ /* two classes 1: node.h 2. singlylinkedlist.h nod1 (value + pointer) ---> node2 ---> node3 ---> |||| <--- node.h ^ | singlylinkedlist ----------------*node head; */ #ifndef NODE_H #define NODE_H #include <iostream> using namespace std; class Node {    friend class singlyLinkedList; public:   ...
Please use the code I provided!! Use either bubble sort or selection sort!! Thank you in...
Please use the code I provided!! Use either bubble sort or selection sort!! Thank you in advance This lab involves adding a sorting function to an existing C++ template. In this module, a file is provided for you -- SortableBag.h -- that implements a simple "bag" (unsorted collection of numbers) structure with array storage. Part 1 Add a sort() method to this template, which should not return any values or take any arguments ('void' for both the return type and...
This question has three parts: a) binary search, b) selection sort, and c) merge sort. a)...
This question has three parts: a) binary search, b) selection sort, and c) merge sort. a) Suppose we are performing a binary search on a sorted list called numbers initialized as follows: #index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 numbers = [-2, 0, 1, 7, 9, 16, 19, 28, 31, 40, 52, 68, 85, 99] #search for the value 5 index = binary_search(numbers, 5) Write the indexes of the elements that would...
Iterative Linear Search, Recursive Binary Search, and Recursive Selection Sort I need it in Java with...
Iterative Linear Search, Recursive Binary Search, and Recursive Selection Sort I need it in Java with comments and I need the input file to be placed with Scanner not BufferedReader Please help I need Class River Class CTRiver and Class Driver Class River describes river’s name and its length in miles. It provides accessor methods (getters) for both variables, toString() method that returns String representation of the river, and method isLong() that returns true if river is above 30 miles...
How would I create a program in C++ where you build and maintain two binary search...
How would I create a program in C++ where you build and maintain two binary search trees of information? I have already created the file reader which I will list on the end. The 2 binary search trees should be controlled by the Operations: L -- for launching a satellite, which will save it's info to the first set or D -- for deorbit the satellite. The other operations I'm looking to implement are: F -- for find, where user...
i have an array of strings, how do i sort them into a binary tree? C...
i have an array of strings, how do i sort them into a binary tree? C Program
Write Insertion Sort and Bubble Sort Program for C# also write their algorithm and Explain their...
Write Insertion Sort and Bubble Sort Program for C# also write their algorithm and Explain their working.
Sorting with Binary Search Tree This assignment asks you to sort the lines of an input...
Sorting with Binary Search Tree This assignment asks you to sort the lines of an input file (or from standard input) and print the sorted lines to an output file (or standard output). Your program, called bstsort (binary search tree sort), will take the following command line arguments: % bstsort [-c] [-o output_file_name] [input_file_name] If -c is present, the program needs to compare the strings case sensitive; otherwise, it's case insensitive. If the output_file_name is given with the -o option,...
(C++) I need to Create a Copy function of a Binary Search Tree recursively providing these...
(C++) I need to Create a Copy function of a Binary Search Tree recursively providing these structure emplate <typename T> class Tree {    struct TreeNode    {        T mData;        TreeNode* mLeft = nullptr;        TreeNode* mRight = nullptr;        TreeNode* mParent = nullptr;        bool mIsDead = false;        TreeNode()        {        }        TreeNode(T tData) : TreeNode()        {            mData = tData;...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT