Ask your own question, for FREE!
Computer Science 8 Online
OpenStudy (ellecullen):

Homework help please : The question is in comments.

OpenStudy (ellecullen):

The following version of Selection Sort is supposed to sort an array in ascending order. For better performance it tries to tackle the array from both ends simultneously: public void sort (int a[ ]) { int left = 0, right = a.length - 1; int k; while (left < right) { for (k = left + 1; k < right; k++ ) { if (a[k] < a[left]) swap(a,k,left); else if (a[k] > a[right]) swap(a,k,right); } left++; right--; } } swap(a,i,j) correctly swaps a[i] and a[j]. This code has a bug, though. Which of the following changes would assure that the method sorts all arrays correctly? I. Remove else in else if (a[k] > a[right]) . . . II. Replace for (k = left + 1; k < right; k++ ) with for (k = left; k <= right; k++ ) III. Add if (a[left] > a[right]) swap(a, left, right); at the beginning of the while loop (before the for loop). I only II only III only I or II II or III 2. Suppose Mergesort is implemented as follows: public void sort(int [ ] a, int n1, int n2) { if (n1 == n2) return; else if (n2 == n1 + 1) { if (a[n2] < a[n1]) swap(a,n1,n2); // swaps a[n1] and a [n2] return; } int m = (n1 + n2) / 2; sort(a,n1,m); sort (a, m+1, n2); if (a[m] > a [m+1]) merge (a,n1,m,n2); // merges a[n1] . . . a[m] and a[m+1] . . . a[n2] } How many times will the method merge be called if an array a contains the values 2 1 4 3 6 5 8 7 7 4 3 1 0

OpenStudy (ellecullen):

what do you think? in the merge sort, don't i have to separate the array?

OpenStudy (ellecullen):

dividing [2143] [6587] [21] [43] [65] [87] left side: 1234 right side: 5678

OpenStudy (ellecullen):

its either 3 or 7

OpenStudy (woodrow73):

Can you provide a more specific question?

OpenStudy (ellecullen):

I can't make them anymore specific. I asked my Computer Science teacher, and she said that the questions are no more specific than what I have.

OpenStudy (ellecullen):

sorry, I keep losing connection to this site.

OpenStudy (woodrow73):

Sorry Elle, I'm too busy at the moment to get to the bottom of this sorting algorithm.

OpenStudy (ellecullen):

@wio

OpenStudy (anonymous):

For merge sort, I think merge sort will be called `n log(n)` times regardless.

OpenStudy (ellecullen):

Really? well for my hw questions its asking how many times will the merge be called for the values 2, 1, 4, 3,6, 5, 8, 7 out of the answers, its either 3 or 7.

OpenStudy (anonymous):

I would say it gets called 7 times.

OpenStudy (ellecullen):

can you show that through the divide, and how it would sort please?

OpenStudy (anonymous):

1 [2143] -[6587] 2 [21]-[43] [65]-[87] 4 [2]-[1] [4]-[3] [6]-[5] [8]-[7] 1+2+4=7

OpenStudy (anonymous):

``` [2143] [6587] [21] [43] [65] [87] [2] [1] [4] [3] [6] [5] [8] [7] Merge 4: [12] [34] [56] [78] Merge 2: [1234] [5678] Merge 1: [12345678] ```

OpenStudy (ellecullen):

Thank you :) How would you do the first question? I thought that the answer was II or III, but my teacher told me its wrong.

OpenStudy (ellecullen):

I'm having trouble with question 1.

OpenStudy (anonymous):

``` public void sort (int a[ ]) { int left = 0, right = a.length - 1; int k; while (left < right) { for (k = left + 1; k < right; k++ ) { if (a[k] < a[left]) swap(a,k,left); else if (a[k] > a[right]) swap(a,k,right); } left++; right--; } } ``` I'm thinking III

OpenStudy (ellecullen):

I'm looking in a textbook

OpenStudy (anonymous):

I guess II could fix it as well

OpenStudy (ellecullen):

I'll check for III in another textbook.

OpenStudy (ellecullen):

Which one do you think is more correct?

OpenStudy (anonymous):

I think III is more correct

OpenStudy (ellecullen):

I'll try to run it. :)

OpenStudy (ellecullen):

It looks like III fits better. :)

OpenStudy (ellecullen):

Thank you :) I'll be here on OS if i need any help :)

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!