Time complexity of LinkedList, HashMap, TreeSet? [closed] - java

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I am a student of CS, learning about Java Collections.
At my school we have received a chart with with time complexity of different operations on data structures. There are some things in the chart that don't make sense to me.
Linked List
It says that time complexity for inserting at the end and finding the number of elements is implementation dependent. Why is that? Why isn't it O(n)?
HashMap
For the HashMap it says that tc for finding number of elements or determining whether the hashMap is empty has also tc implementation dependent.
TreeMap same goes for the TreeMap. According to the chart, the tc of the operation to determine the number of elements is implementation dependent. Which implementation do they mean?

Some implementations of LinkedList can keep a count of the number of elements in their list as well as a pointer to the last element for quick tail insertion. This means that they are done O(1) as it is a quick data access.
Similar reasoning can be followed for implementations of HashMap and TreeSet, if they keep a count of the number of elements with their data structure then functions such as isEmpty() and size() can be done O(1) also.

Related

Are there any Java collection types that are unordered but sorted? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 months ago.
Improve this question
Ordered collections in Java can be sorted or unsorted, but is it possible to have the reverse? Can unordered collections ever be sorted?
Any pointers are appreciated. Thanks.
I think we need to define what we mean by ordered and sorted.
I assume that:
by order you mean insertion order
by sorted you mean sorting according to a comparator
In that case, yes. A NavigableSet/SortedSet such as TreeSet does not remember the insert order but will keep (and return) the items offered in their natural order.

get subset of elements from set in circular way [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Consider a set with like 100 elements.
Set<String> originalSet; //[1....100] size is 100
From originalSet, (m) subset of elements of some size(n) with some starting index(i) have to retrieved.
Example:
m = 4, n = 45, i = 1
Following have to be retrieved
subset1[1-45], subset2[46-90], subset3[91-35], subset4[36-80]
Whats the best way of doing this.
First of all, Set is unordered so it does not make sense to talk about indices etc. List would make more sense here.
Next, you have to be explicit about what you mean by "best". Performance on insertion? Random access? Creation of your n-from-i subset? These are important question to choose the implementation.
I think two primary options would be linked list with special handling of the last element in subList operation or an array-based list.
Assuming your set has some notion of order, you could write this with
Iterable<Iterable<String>> slices =
Iterables.limit(
Iterables.partition(
Iterables.skip(
Iterables‌​.cycle(originalSet),
i),
n),
m);
If you wanted a set out of this, you'd have to do a transform or something; if you have Java 8 that'd just be something like Iterables.transform(..., ImmutableSet::copyOf).

What is Complexity of a program and how to calculate it? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am a Java Developer, I wanted to know about Complexity of Program and its calculation ? (i am beginner please answer in simple terms that i can understand
thanks in advance..!!)
Generally complexity is a number of operation you have to perform to achieve your goal.
Complexity is marked as O(n) where n is the complexity. For example complexity of assignment is O(1).
Complexity of access to array element is O(1) too. Complexity of iteration over all elements of array, collection, map etc is O(n) where n is number of elements of the collection. For example if you want to find sum of all elements of n-elements array you have to perform operation with complexity O(n).
Please note that complexity of looking for specific element of array is n also although average number of operations is n/2 because the element may be at first, last or any other position.
Complexity of sorting depends on the algorithm. Simple algorithms sort arrays with complexity of O(n^2), while better algorithms like quick short have O(n*ln(n)).
There are 2 types of complexity
1. space complexity
2. Time complexity
The time required for the execution of a program(or loops or statement) is reffered to as time complexity. The space or memory required for the program is reffered to as space complexity. Both complexities are measured in Big Oh notations .

Is it possible to insert a number into a sorted array, time: log(n) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have a sorted array of integers (of size n) and would like to insert a new element into the array, is it possible to do so in O(log(n))? I need to keep an insertion and find computational complexity of O(log(n))).
Right now the only idea I have is to do binary search in order to find the desired index for insertion - this would take O(log(n)), but then I would have to create a new array and copy the entire cells which would take O(n).
EDIT:
It was solved by using an AVL Tree instead, that way any new elements added takes O(log(n)) and finding an element takes O(log(n))
"is it possible to do so in log(n)? " - in short no. From my recollections and experience inserting into an arbitrary position in an array is always O(N), almost by definition. If you want faster performance use something like the TreeMap class.

Sorting algorithm implementation [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
First time poster, sorry if I break any etiquette.
I'm studying for my Data structures and algorithms midterm and I have a practice problem I don't really understand.
Suppose you are given a sorted list of N elements
followed by f(N) randomly-order elements.
How would you sort the entire list if f(N) = O(1)?
How would you sort the entire list if
f(N) = O(log N)?
We have gone over lots of sorting algorithms but the exam focuses on Insertion, Quick and Merge Sort. I don't really understand what it means by if F(N) = O(log N). Is that using Big oh notation to say that the most amount of random elements on the end would be either a constant or log(N) in each respect case.
Thanks for any insight.
Edited: Fixed my obvious mistake in terms of Big Oh notation, not sure where to go from here though.
In the first example you are given a problem, where a constant number of non-ordered elements follow the sorted sequence. In essence this means that you may implement an algorithm to insert a single non-ordered element and then repeat it several times. The overall complexity of inserting all f(N) = O(1) elements will be the same. One of the algorithms you mention is best to perform this operation.
In the second case you have number of elements to be inserted in the order of log(n). In this case you can not ignore this number as it is dependent on the input size. Thus you need to think of a smarter way to merge it with the remaining part that is already sorted. (TIP: maybe the operation you need to perform will help you choose an algorithm?)

Categories

Resources