Can someone help me fix this particular error in my java code: private static in
ID: 3712403 • Letter: C
Question
Can someone help me fix this particular error in my java code:
private static int partition (ArrayList list, int start, int end) { // Provide this code and add in numCompares Person pivot-list.get(start); // First element is the pivot int low start + 1; //Index for forward search int high - end; /I Index for backward search while (high > low) { while (low?= high && list [low!?pivot) low++; I/ Search forward from left while (low pivot) { high--; // Search backward from right ?f (high low) // Swap two elements in the list int temp = 11stLhighl; list.set(high,list.get(low)); st[low]- temp; while (high > start && list[high] - pivot) { high- // Swap pivot with list[high] if (pivot > listlhighl) list[start]-list[highl; list[highlpivot; return high;Explanation / Answer
Solution:
1. The error in partition method is with respect to a member named list (Which is a type of ArrayList).
In java, ArrayList contents cannot be accessed using []. If list was a type of Array, then list[start] or list[high] would work fine. But, as list is type of ArrayList, list[start] or list[high] is invalid.
It can be replaced with get and set methods of ArrayList.
For Ex:
int pivot = list.get(start);
and
list.set(high,list.get(low)); //list[high] = list[low];
---------------------------------------------------------------------------------------------------------------------------------------------------------
Let me type the entire code.
1. Replace
while(low<=high && list[low]<=pivot)
with
while(low<=high && list.get(low)<=pivot)
2. Replace
while(low<=high && list[high]>pivot)
with
while(low<=high && list.get(high)>pivot)
3. Replace
int temp = list[high];
with
Person temp = list.get(high);
4. Replace
list[low] = temp;
with
list.set(low,temp);
5. Replace
while(high > start && list[high]>=pivot)
with
while(high > start && list.get(high)>=pivot)
6. Replace
if(pivot > list[high])
with
if(pivot > list.get(high) )
7. Replace
list[start] = list[high];
with
list.set(start,list.get(high));
8. Replace
list[high] = pivot;
with
list.set(high,pivot);
-------------------------------------------------------------------------------------------------------------------------------------------------
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.