Question

In: Computer Science

8) Sorting - C++ You have this function that sorts any vector of char data: void...

8) Sorting - C++

You have this function that sorts any vector of char data:

    void good_bubble(vector<char> & data, vector<char>::size_type start,
                                          vector<char>::size_type end)
    {
            vector<char>::size_type loop{0}, cur;
            bool done{false};
            while (loop <= end-start+1 && !done)
            {
                    done = true;
                    for (cur = start; cur <= end-1-loop; ++cur)
                    {
                            if (data[cur] > data[cur+1])
                            {
                                    swap(data[cur], data[cur+1]);
                                    done = false;
                            }
                    }
                    ++loop;
            }
            return;
    }

But now you have to sort Date objects! As luck would have it, the Date class provides the method:

    bool Date::greater(const Date & other) const;

Please show your changes to only the lines from above that would need to change to make your overload of good_bubble sort a vector of Date objects.

Solutions

Expert Solution

Updated code that would sort Date objects:

.

void good_bubble(vector<Date> &data, vector<Date>::size_type start, vector<Date>::size_type end)

{

    vector<Date>::size_type loop{0}, cur;

    bool done{false};

    while (loop <= end - start + 1 && !done)

    {

        done = true;

        for (cur = start; cur <= end - 1 - loop; ++cur)

        {

            // use greater instead of >

            if (data[cur].greater(data[cur + 1]))

            {

                swap(data[cur], data[cur + 1]);

                done = false;

            }

        }

        ++loop;

    }

    return;

}

.

Changed code has been highlighted.


Related Solutions

In basic C++ Create a function that takes in a vector, sorts it, then outputs to...
In basic C++ Create a function that takes in a vector, sorts it, then outputs to the console the result.
[ Write in C, not C++] Define a function char* deleteSymbol(char *s, char x) that removes...
[ Write in C, not C++] Define a function char* deleteSymbol(char *s, char x) that removes the character x from string s. For s[] = “America”, a call to deleteSymbol(s, ‘a’) converts s[] = “Ame”
Write a function that receives a StaticArray and sorts its content in non-descending order. Sorting must...
Write a function that receives a StaticArray and sorts its content in non-descending order. Sorting must be done ‘in place’, meaning the original input array will be modified. You may assume that the input array will contain at least one element and that values stored in the array are all of the same type (either all numbers, or strings, or custom objects, but never a mix of those). You do not need to write checks for these conditions. You can...
Write in C programming language Write the function replace(char b[], char f[], char t[]). which finds...
Write in C programming language Write the function replace(char b[], char f[], char t[]). which finds the string 'f' in the string 'b' and replaces it with the string 't'. You can assume that f and t same length Example: char string[] = "zap";     replace(string, "ap", "oo"); --changes 'string' to "zoo".     *don't assume substring being replaced is singular, or that its own substrings are unique.     *Don't re scan letters already checked and replaced         char string[] =...
Why is my C code counter returning the wrong output? void removeLn(char *tPtr) { for (;...
Why is my C code counter returning the wrong output? void removeLn(char *tPtr) { for (; *tPtr != '\0'; tPtr++) { if (*tPtr == '\n') { *tPtr = '\0'; } } } This is a method I wrote that essentially replaces the \n in a text file with a null. int counter(FILE *filePtr) { char ln[length]; int counter = 0; while (fgets(ln, length, filePtr) != NULL) { char *r;    for (r = ln; *r != '\0';) { while (isspace(*r))...
Situation (Data Structures and Sorting C++) You have been employed at BmoCel, an internet service provider....
Situation (Data Structures and Sorting C++) You have been employed at BmoCel, an internet service provider. Data is sent as packets from their server to their clients. Each packet has a fixed size of 16 alphanumeric characters. However, they are currently having problems with managing their data transmissions. Your bosses have asked you to rectify the problems, in the following order: 1. The data at the server is to be stored as packets, in a First‐In, Last‐Out manner. If there...
in C i have a 2d array char m[8][8] = {{1,1,1,1,1,1,1,1}, {1,0,0,0,1,0,0,1}, {1,0,1,0,1,1,0,1}, {1,0,1,0,0,0,0,1}, {1,0,1,1,1,1,0,1}, {1,0,0,0,0,0,0,1},...
in C i have a 2d array char m[8][8] = {{1,1,1,1,1,1,1,1}, {1,0,0,0,1,0,0,1}, {1,0,1,0,1,1,0,1}, {1,0,1,0,0,0,0,1}, {1,0,1,1,1,1,0,1}, {1,0,0,0,0,0,0,1}, {1,0,1,0,1,0,1,1}, {1,1,1,1,1,1,1,1}} turn this array into a 8 integer array int new[8]={-1,-119,-83, ......} i.e Two's Complement to integer 10001001 --> -119
Write a function in C that uses the Merge Sort sorting algorithm with arrays. The function...
Write a function in C that uses the Merge Sort sorting algorithm with arrays. The function must not be void and must output type int* i.e. it must take the form: int* merge_sort(int a[], int n) where a[ ] is the input matrix and n is the size of the matrix. You may use an auxiliary functions such as "merge." The returned array should be sorted using merge_sort and should not modify the array that was input (a[ ] ).
C++ Write the C++ code for a void function that prompts the user to enter a...
C++ Write the C++ code for a void function that prompts the user to enter a name, and then stores the user's response in the string variable whose address is passed to the function. Name the function getName.
There is a C function decodeMorse(const String & string, char message[]). This function examines the binary...
There is a C function decodeMorse(const String & string, char message[]). This function examines the binary string and iteratively constructs a decimal value (val) and width of each binary pattern (separated by spaces), until a space or a null character ('\0') is encountered in the string. Once a space or a null character is found, this function should call the assembly code (decode_morse()) to obtain the corresponding ASCII value, for the current val and width, and place the ASCII value...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT