Homework help please : The question is in comments.
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
what do you think? in the merge sort, don't i have to separate the array?
dividing [2143] [6587] [21] [43] [65] [87] left side: 1234 right side: 5678
its either 3 or 7
Can you provide a more specific question?
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.
sorry, I keep losing connection to this site.
Sorry Elle, I'm too busy at the moment to get to the bottom of this sorting algorithm.
@wio
For merge sort, I think merge sort will be called `n log(n)` times regardless.
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.
I would say it gets called 7 times.
can you show that through the divide, and how it would sort please?
1 [2143] -[6587] 2 [21]-[43] [65]-[87] 4 [2]-[1] [4]-[3] [6]-[5] [8]-[7] 1+2+4=7
``` [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] ```
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.
I'm having trouble with question 1.
``` 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
I'm looking in a textbook
I guess II could fix it as well
I'll check for III in another textbook.
Which one do you think is more correct?
I think III is more correct
I'll try to run it. :)
It looks like III fits better. :)
Thank you :) I'll be here on OS if i need any help :)
Join our real-time social learning platform and learn together with your friends!