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...
Hi i have this for a practical tomorrow (C++): a) Write a function vector merge(vector a,...
Hi i have this for a practical tomorrow (C++): a) Write a function vector merge(vector a, vector b) that merges two vectors, alternating elements from both vectors. If one vector is shorter than the other, then alternate as long as you can and then append the remaining elements from the longer vector. For example, if a is 1 4 9 16 and b is 9 7 4 9 11 Then merge returns the vector 1 9 4 7 9 4...
Write a function void reverse(char * s) that reverses the string passed as an argument. Your...
Write a function void reverse(char * s) that reverses the string passed as an argument. Your code should use pointer arithmetic (it may increment and decrement pointers, but it may not use array indexing). Here is a piece of code that shows the behavior of reverse: char buf[100]; strcpy(buf, “hello”); reverse(buf); printf(“%s\n”, buf); // output should be olleh
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[] =...
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...
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))...
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[ ] ).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT