Homework Help Question & Answers

Can someone help me with this please: #include <iostream> #include <cstdlib> #include <string> #include <cstring> using namespace std; #define ARSIZ 150000 int debug=0; void du...

Can someone help me with this please:

#include <iostream>
#include <cstdlib>
#include <string>
#include <cstring>

using namespace std;

#define ARSIZ 150000

int debug=0;

void dump(int ar[], int len)
{
for(int i=0; i<len; i++)
{
cout<<" DUMP: data = : "<< ar[i] << endl;
}
}

void sort(int *ar, int length)
{
if(length == 1 || length == 0) return;

cout << "BREAKING DOWN: " << endl;

//for(int i=0; i<length; i++)

// cout << " DUMP: DATA = : " << ar[i] << endl;

int left_length = length/2, right_length = length - (length/2);
int left_list[left_length], right_list[right_length];
for(int i=0; i<right_length; i++)

right_list[i] = ar[i + left_length];

for(int i=0; i<left_length; i++)

left_list[i] = ar[i];
cout << "left = " << endl;


for(int i=0; i<left_length; i++)

cout << " DUMP: data = : " << right_list[i] << endl;

sort(left_list, left_length);
sort(right_list, right_length);

int left_index = 0, right_index = 0;

while(left_index + right_index < length)
{
if(left_list[left_index] < right_list[right_index])
{
if(left_index <left_length)
{
ar[left_index + right_index] = left_list[left_index];
left_index++;
}
else
{
ar[left_index + right_index] = right_list[right_index];
right_index++;
}
}
else
{
if(right_index < right_length)
{
ar[left_index + right_index] = right_list[right_index];
right_index++;
}
else
{
ar[left_index + right_index] = left_list[left_index];
left_index++;
}
}
}
cout << "MERGED: list :" << endl;

for (int i=0; i<length; i++)

cout << " DUMP: data = : " << ar[i] << endl;

}
int main(int argc, char **argv)
{
int ar[ARSIZ];

int i, v, len;

if ((argc == 2) && (strstr(argv[1],"debug"))) debug = 1;

for (i=0; i<ARSIZ; i++) {
cin >> v;

if (v < 0) break;

ar[i] = v;
}

len = i;

cout << "main: before sort:\n";
for (i=0; i<len; i++) {
cout << "main: ar[" << i << "] = " << ar[i] << endl;
}

sort(ar, len);

cout << "\nmain: after sort:\n";
for (i=0; i<len; i++) {
cout << "main: ar[" << i << "] = " << ar[i] << endl;
}
}
And this is what it should print out:

[[email protected] lab8]$ cat nums.7
3
0
4
2
5
1
6
-1
[[email protected] lab8-msort]$ lab8 < nums.7
main: before sort:
main: ar[0] = 3
main: ar[1] = 0
main: ar[2] = 4
main: ar[3] = 2
main: ar[4] = 5
main: ar[5] = 1
main: ar[6] = 6

main: after sort:
main: ar[0] = 0
main: ar[1] = 1
main: ar[2] = 2
main: ar[3] = 3
main: ar[4] = 4
main: ar[5] = 5
main: ar[6] = 6

[[email protected] lab8]$ lab8 debug < nums.7
main: before sort:
main: ar[0] = 3
main: ar[1] = 0
main: ar[2] = 4
main: ar[3] = 2
main: ar[4] = 5
main: ar[5] = 1
main: ar[6] = 6
BREAKING DOWN:
DUMP: data = : 3
DUMP: data = : 0
DUMP: data = : 4
DUMP: data = : 2
DUMP: data = : 5
DUMP: data = : 1
DUMP: data = : 6
left =
DUMP: data = : 3
DUMP: data = : 0
DUMP: data = : 4right =
DUMP: data = : 2
DUMP: data = : 5
DUMP: data = : 1
DUMP: data = : 6
BREAKING DOWN:
DUMP: data = : 3
DUMP: data = : 0
DUMP: data = : 4
left =
DUMP: data = : 3
right =
DUMP: data = : 0
DUMP: data = : 4
BREAKING DOWN:
DUMP: data = : 0
DUMP: data = : 4
left =
DUMP: data = : 0
right =
DUMP: data = : 4
MERGED: list:
DUMP: data = : 0
DUMP: data = : 4
MERGED: list:
DUMP: data = : 0
DUMP: data = : 3
DUMP: data = : 4
BREAKING DOWN:
DUMP: data = : 2
DUMP: data = : 5
DUMP: data = : 1
DUMP: data = : 6
left =
DUMP: data = : 2
DUMP: data = : 5
right =
DUMP: data = : 1
DUMP: data = : 6
BREAKING DOWN:
DUMP: data = : 2
DUMP: data = : 5
left =
DUMP: data = : 2
right =
DUMP: data = : 5
MERGED: list:
DUMP: data = : 2
DUMP: data = : 5

BREAKING DOWN:
DUMP: data = : 1
DUMP: data = : 6
left =
DUMP: data = : 1
right =
DUMP: data = : 6
MERGED: list:
DUMP: data = : 1
DUMP: data = : 6
MERGED: list:
DUMP: data = : 1
DUMP: data = : 2
DUMP: data = : 5
DUMP: data = : 6
MERGED: list:
DUMP: data = : 0
DUMP: data = : 1
DUMP: data = : 2
DUMP: data = : 3
DUMP: data = : 4
DUMP: data = : 5
DUMP: data = : 6

main: after sort:
main: ar[0] = 0
main: ar[1] = 1
main: ar[2] = 2
main: ar[3] = 3
main: ar[4] = 4
main: ar[5] = 5
main: ar[6] = 6

But for some reason is not in order, help please.

0 0
Next > < Previous
ReportAnswer #1

i st MERGED: DUMP: data: 0 DUMP: data: 4 I i st MERGED: DUMP: data: 0 DUMP: data: 3 DUMP: data: 4 BREAKING DOWN: left- DUMP:

#include <iostream>
#include <cstdlib>
#include <string>
#include <cstring>

using namespace std;

#define ARSIZ 150000

int debug = 0;

void dump(int ar[], int len)
{
    for (int i = 0; i < len; i++) {
        cout << " DUMP: data = : " << ar[i] << endl;
    }
}

void sort(int* ar, int length)
{
    if (length == 1 || length == 0)
        return;

    cout << "BREAKING DOWN: " << endl;

    //for(int i=0; i<length; i++)

    // cout << " DUMP: DATA = : " << ar[i] << endl;

    int left_length = length / 2, right_length = length - (length / 2);
        
    int left_list[left_length], right_list[right_length];

    for (int i = 0; i < left_length; i++)
        left_list[i] = ar[i];
    for (int i = 0; i < right_length; i++)
        right_list[i] = ar[i + left_length];
                
    cout << "left = " << endl;
        dump(left_list, left_length);
    cout << "right = " << endl;
        dump(right_list, right_length);

    sort(left_list, left_length);
    sort(right_list, right_length);

    int left_index = 0, right_index = 0;
        int out_index = 0;

    while ((left_index < left_length) && (right_index < right_length)) {
        if (left_list[left_index] < right_list[right_index]) {
            ar[out_index++] = left_list[left_index++];
        } else if(left_list[left_index] > right_list[right_index]) {
            ar[out_index++] = right_list[right_index++];
        } else {
            ar[out_index++] = left_list[left_index++];
            ar[out_index++] = right_list[right_index++];
        }
    }
        
        // Its possible that one of the list is still remaining
        while(left_index < left_length) {
                ar[out_index++] = left_list[left_index++];
        }
        while(right_index < right_length) {
                ar[out_index++] = right_list[right_index++];
        }
        
    cout << "MERGED: list :" << endl;

    for (int i = 0; i < length; i++)
        cout << " DUMP: data = : " << ar[i] << endl;
}

int main(int argc, char** argv)
{
    int ar[ARSIZ];

    int i, v, len;

    if ((argc == 2) && (strstr(argv[1], "debug")))
        debug = 1;

    for (i = 0; i < ARSIZ; i++) {
        cin >> v;

        if (v < 0)
            break;

        ar[i] = v;
    }

    len = i;

    cout << "main: before sort:\n";
    for (i = 0; i < len; i++) {
        cout << "main: ar[" << i << "] = " << ar[i] << endl;
    }

    sort(ar, len);

    cout << "\nmain: after sort:\n";
    for (i = 0; i < len; i++) {
        cout << "main: ar[" << i << "] = " << ar[i] << endl;
    }
}

please upvote. Thanks!

Add Homework Help Answer
Add Answer of:
Can someone help me with this please: #include <iostream> #include <cstdlib> #include <string> #include <cstring> using namespace std; #define ARSIZ 150000 int debug=0; void du...
Your Answer: Your Name: What's your source?
Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
More Homework Help Questions Additional questions in this topic.
Need Online Homework Help?
Ask a Question
Get FREE Expert Answers
WITHIN MINUTES
Related Questions