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

C++ ONLY Binary Search and Selection Sort A binary search first requires a sort. Use a...
C++ ONLY Binary Search and Selection Sort A binary search first requires a sort. Use a selection sort to organize an array, then find the value using a binary search. Input the array, and a value to find. Output the sorted array and the value if contained in the array. /* * File: main.cpp * Author: * Created on: * Purpose: Binary Search */ //System Libraries #include <iostream> //Input/Output Library #include <cstdlib> //Random Functions #include <ctime> //Time Library using namespace...
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...
Analysis of Algorithims Bubble sort for 12, 2, 3, 21, 11, 10,8 Binary search for K=12...
Analysis of Algorithims Bubble sort for 12, 2, 3, 21, 11, 10,8 Binary search for K=12 in the array A={2, 3, 5, 7,11,15, 16,18,19} selection sort for 12, 2, 3, 21, 11, 10,8
How can i bubble sort a sentence in a char array in c++ This is the...
How can i bubble sort a sentence in a char array in c++ This is the prototype of the function: char* sort(char string[], int numOfWords, int lengthOfWord); This is the testing code in the main file: char words[] = "CAT FAT BAT HAT RAT"; printf("Before sort: t%s\n";words); char result = sort(words; 5; 3); printf("After sort : t%s\n"; result); Expected output: Before sort: CAT FAT BAT HAT RAT After sort: BAT CAT FAT HAT RAT
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT