9 for in 0 i forj 0 j aMPos MPos J swapi MPos 10 for i n

9) for (i-n-> 0; i-) for(j = 0; j aMPos) MPos = J; swap(i, MPos); 10) for (i = n: i > 0; i = i/2) { for (j = i; j > 0; j = j/2) { //constant time operations (3) Running time of LinkedList and ArrayList functions. Following each expression, include a 1-2 sentence argument about why an algorithm to perform the given operation could not run faster than the bound you give. As above, full credit will be given only for tight Big-omega bounds. That is, it is not sufficient to say that all operations take omega(1). This is trivially true for any piece of code. (Though in some cases this will be the tightest Big-omega bound). Example Question: Adding a value to the start of the list. Example Answer: Running time: (1). You have a pointer to the head node in the list so adding an element involves creating a new node (which is not dependent on the length of the list), setting the links in the new node, and changing the values of the head references. This takes somewhere around 10 steps to perfprn, and 10-42(1) Example Question: Adding a value to the start of the array list. Example Answer: Running time: (n). You have to miove everything over one cell to create a room for a new element. It will be done using a for loop that iterates through all elements. Since there are n elements in the list, it takes (n) to add a new element to the beginning of the array list

Solution

Add

Remove

Get

Contains

Data Structure

ArrayList

O(1)

O(n)

O(1)

O(n)

Array

LinkedList

O(1)

O(1)

O(n)

O(n)

Linked List

CopyonWriteArrayList

O(n)

O(n)

O(1)

O(n)

Array

Operation

Array

ArrayList

Singly Linked List

Read (any where)

O(1)

O(1)

O(n)

Add/Remove at end

O(1)

O(1)

O(n)

Add/Remove in the interior

O(n)

O(n)

O(n)

Resize

O(n)

N/A

N/A

Find By position

O(1)

O(1)

O(n)

Find By target (value)

O(n)

O(n)

O(n)

Array:
1- Excessive read, as time complexity of read is always O(1)
2- Random access to element using index: if you

ArrayList:
1- Excessive read
2- Random access to elements using their index
3- More flexible in coding
4- Effective use of memory space as items get allocated as needed

LinkedList:
1- Effective use of memory space as items get allocated as needed
2- Excessive Add/Remove of elements (It\'s better than ArrayList, because since array list items get stored in memory in adjacent place. when adding a new element in the middle of the array list, all the items after the inserted one have to be shifted, with Linked list the new item gets injected in the list without the need to shift the other items as they are not adjacent in the memory)

The time complexity comparison is as follows:

ArrayList has O(n) time complexity for arbitrary indices of add/remove, but O(1) for the operation at the end of the list.

LinkedList has O(n) time complexity for arbitrary indices of add/remove, but O(1) for operations at end/beginning of the List.

Add

Remove

Get

Contains

Data Structure

ArrayList

O(1)

O(n)

O(1)

O(n)

Array

LinkedList

O(1)

O(1)

O(n)

O(n)

Linked List

CopyonWriteArrayList

O(n)

O(n)

O(1)

O(n)

Array

 9) for (i-n-> 0; i-) for(j = 0; j aMPos) MPos = J; swap(i, MPos); 10) for (i = n: i > 0; i = i/2) { for (j = i; j > 0; j = j/2) { //constant time oper
 9) for (i-n-> 0; i-) for(j = 0; j aMPos) MPos = J; swap(i, MPos); 10) for (i = n: i > 0; i = i/2) { for (j = i; j > 0; j = j/2) { //constant time oper
 9) for (i-n-> 0; i-) for(j = 0; j aMPos) MPos = J; swap(i, MPos); 10) for (i = n: i > 0; i = i/2) { for (j = i; j > 0; j = j/2) { //constant time oper

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site