Hey guys can someone explain the big o notation for each of the following examples? Thanks in advance
Reading the element at index 28 in an array. Is this O(1)?
Comparing two ArrayList objects to determine if they contain the same elements (disregarding order, and without sorting). Is this O(n^2)?
Searching for a specific target value in an unsorted array. Is this O(n)
1- Because array elements can be accessed randomly (no need to move from one cell to another like linked list) so you can basically say array[28] which is O(1)
2- comparing two array list can done in a way which will lead to a a lower big-O But with sorting.
You can sort each arraylist using mergesort or quicksort O(nlg(n)) then compare the two sorted lists in O(n). the result is O(nlgn).
But another algorithm (without sorting) would iterate over each element in one array (n). And then checks whether the element is another array (n) (and marks it to handle duplicates properly). This latter algorithm is O(n^2).
3- Yes it’s. You have to go through the whole list one by one till you find the desired element.