Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA INTRO PROBLEM Suppose we have an ArrayList of n Integer objects, sorted in

ID: 3919474 • Letter: J

Question

JAVA INTRO PROBLEM

Suppose we have an ArrayList of n Integer objects, sorted in increasing order. Consider the following algorithms for removing a particular Integer value from that Arraylist. It is already known that this value is in the ArrayList. Identify the best and worst case running times (using Big O) for each algorithm. Explain each of your answers. Algorithm I: Perform a lincar search to find the value, starting from the BEGINNING of the list. When the value is found, remove it by calling the ArrayList'S remove (index) method. Algorithm II: Perform a linear search to find the value, starting from the END of the list. When the value is found, remove it by calling the ArrayList's remove (index) method.

Explanation / Answer

//Algorithm1:
int find1(ArrayList<Integer> a,int k)
{
//a array list of integers
//k value to search in a
//performing linear search
  
//from the beginning
int i=0,n=a.size();
while(i<n)
{
if(a.get(i)==k)
{
//if founc then
//removing from list..
a.remove(i);//removing...
break;
}
  
i++;
}
return i;

}
  
  
//Algorithm2
int find2(ArrayList<Integer> a,int k)
{
//a array list of integers
//k value to search in a
//performing linear search
  
//from the end of the list
int i=a.size()-1;
while(i>=0)
{
if(a.get(i)==k)
{
//if founc then
//removing from list..
a.remove(i);//removing
break;
}
  
i--;;
}
return i;

}