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

2. Suppose we have an ArrayList of n Integer objects, sorted in increasing order

ID: 3920284 • Letter: 2

Question

2. 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 linear 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. 3. Suppose an ArrayList is implemented with an array as discussed in the lecture. Write a method that reverses the order of elements in an ArrayList without creating a new ArrayList. You may only use the methods remove, add, and size.

Explanation / Answer


import java.util.ArrayList;

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author Vamsi
*/
public class linearSearch {
  
  
//2: Alogrithm I
//remove by searching from begining
static void remove1(ArrayList<Integer> a,int n)
{
//searching element using linear search
int i=0;
for(i=0;i<a.size();i++)
if(a.get(i)==n)break;//if found then stopping..
//remove element
if(i<a.size())
a.remove(i);//removing element at found index
  
}
  
//2: Alogrithm II
//remove by searching from end
static void remove2(ArrayList<Integer> a,int n)
{
//searching element using linear search
int i=a.size()-1;
for(;i>=0;i--)
if(a.get(i)==n)break;//if found then stopping..
//remove element
if(i>=0)
a.remove(i);//removing element at found index
  
}
  
static void printArray(ArrayList<Integer> a)
{
int i=0;
for(i=0;i<a.size();i++)
System.out.print(a.get(i)+" ");
System.out.println();
  
}
  
//3
//reversing array list
static void reverse(ArrayList<Integer> a)
{
//reversing logic....
ArrayList<Integer> l = new ArrayList<Integer>();//creating new arraylist
int i=0;
for(;a.size()>0;)
{ l.add(a.get(i));
a.remove(i);//adding to new list and removing from a
}
i = l.size()-1;
for(;i>=0;i--)
a.add(l.get(i));//adding in reverse order...
}
public static void main(String argv[])
{
  
ArrayList<Integer> a = new ArrayList<Integer>();
  
//adding elements to arraylist
  
a.add(2);
a.add(4);
a.add(8);
a.add(5);
a.add(1);
printArray(a);
//removing
remove1(a,2);
printArray(a);
//removing
remove2(a,5);
printArray(a);
  
System.out.println(" After reversing: ");
reverse(a);
  
printArray(a);
  
  
  
  
  
  
  
}
  
}

output:

run:
2 4 8 5 1
4 8 5 1
4 8 1

After reversing:

1 8 4
BUILD SUCCESSFUL (total time: 0 seconds)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote