Element of array takes long to do action with - java

I need a way if an array element takes long time to do something with , the next element jump over it without waiting much for it

Related

Take n elements at a time from a list in RxJava2

I have a list of elements and I want to process n elements at a time from that list. How can I do it in RX way?
I took a look at take operator but that only takes first n or last n elements. I need to process all elements in a list but n at a time.
Ideally I should get multiple lists of size n from a bigger list.
You could use window() (or one of its overloaded variants):
observable.window(batchSize).subscribe(...)
Or, if you don't want to have to wait for the window to 'fill' then perhaps buffer() (or one of its overloaded variants):
observable.buffer(batchSize).subscribe(...)

LinkedList Searching time complexity in java

I am confused over the searching complexity of LinkedList in java. I have read that time complexity to search an element from a LinkedList is O(n).
say for example,
LinkedList<String> link=new LinkedList<String>();
link.add("A");
link.add("B");
link.add("C");
System.out.println(link.get(1));
Now, from here by get(index) method we can say that to search an element It should take O(1) times. But I have read that it will take O(n).
Can anybody help me out to get clear concept?
Access in a linked list implementation, like java.util.LinkedList, is O(n). To get an element from the list, there is a loop that follows links from one element to the next. In the worst case, in a list of n elements, n iterations of the loop are executed.
Contrast that with an array-based list, like java.util.ArrayList. Given an index, one random-access operation is performed to retrieve the data. That's O(1).
A linked list is as such, a list of items that are linked together by a means such as a pointer. To search a linked list, you are going to iterate over each item in the list. The most time this will take, will be T(n) where n is the length of your list.
A big-O notation stands for the upper bounds or the worst case scenario.
If you are searching for an item at index 1 it will finish almost immediately, T(1), since it was the first item in the list. If you are to search for the n^th item it will take T(n) time, thus staying in O(n).
A visual representation of a linked list:
[1] -> [2] -> [3] -> [4] -> ... [n-1] -> [n]
An example of what a get() method might look like
get(int i)
{
Node current = head
while (i > 0)
{
current = current.getNext()
i--
}
return current
}
As you see, it iterates over each node within the list.
The Ordo function means a maximal approximation. So it is O(n) because if the list has n entries and you want to get the last entry, then the search goes through n items.
On the other hand, lookup for an ArrayList is O(1), because the lookup time is close to constant, regardless the size of the list and the index you are looking for.

Heapsorting algorithm

so I have a program that performs a heap sort and I have a remove element function. I do this by taking the last element all the way on the right side and then replace the n-th element with it. To maintain the sort, I then bubble the element down to its correct place in the heap. My friend doesn't seem to think that this will work. will it?
Heapsort is using max-heap, each time, you need to switch the root node, which is the max one, with the nth element, then put the max element into result list from right to left.
Then, bubble the element down to its proper position, and decrease the heap's size from n to n-1.
Then, the new root node of new heap is the max element among the remaining n-1 elements. Just recursively switch root element with the n-1th element, and again put the max element to the result list, decrease heap's size by 1 until the heap's size is 0

ArrayList Constant-time and Linear Time Access

I have been learning the tips of Java SE 7. I have read a statement about ArrayList:
Access is performed in constant time.
Insertion/deletion is performed in linear time.
I would like to know what is constant and linear time access?
constant time means there is a hard bound how much time each op will take to perform.
Linear time means the longer the ArrayList is (more object it contains) the longer time the op will take. The connection is linear, i.e. time(op) <= CONST * #elements
In complexity analysis, we refer it as big O notation and linear time is O(n), and constant time is O(1)
The reason for it is:
Access is plain array access, and it is done in constant time in RAM machine (such as out PCs).
Insertion/Deletion - if it is not in the last element, requires shifting all following elements: (Insertion requries shifting to the right, and deletion to the left) - thus you actually need a linear number of OPs to perform insertion/deletion (unless it is the last element)
The meanings are:
constant means that the time is always the same, it doesn't matter the length of the List.
[constant time is also called O(1) in Big-O notation]
linear means that the more the List grows, the longer is the time, but in a linear way, so for example to perform a linear operation on a list that contains 20 elements it will take two times the time needed for a list with 10 elements.
[linear time is also called O(n) in Big-O notation]
A precisation: when comparing algorithms is normally provided the worst case performance, so it means that the time needed is less or equal than linear.
In your case the implementation of the List is based on arrays (so the name ArrayList) like this:
The access time is constant because when the program knows where the first element of the list is and how big is every cell, it can directly get the n-th element using simple math like:
element_n_cell = element_1_cell + (cell_size * n)
Insertions and deletions are more time-expensive for two reasons:
When you insert or delete an element in a position, all the following elements need to be shifted.
An array can't be resized, so when you instantiate a new ArrayList, Java will create an array with a pre-defined length s, and it will use the same array as long as it fits. When you add the (s+1)-th element, the program needs to create a bigger array and copy all the elements in the new one.
Undestand Constant time access
java.util.ArrayList implements java.util.RandomAccess interface, which is a marker interface that signifies that you can directly access any element of this collection. This also implies that it takes the same amount of time (constant time) to access any element.
If we take java.util.LinkedList, it takes more time to access the last element than the first element.

java List processing time

This is from wikipedia: http://en.wikipedia.org/wiki/Arraylist under Performance.
ArrayList: constant time for remove(), add() at end of array, linear time to add(), remove() at beginning.
LinkedList: both of operations stated : constant time, indexing: linear.
1)Why the difference in arraylist processing time between the two operations?
2)Linkedlist is linear for indexing, constant for adding at the end, why?
1) Because to add/remove at the beginning it has to shift everything and reindex.
2) Because it maintains references to the head and tail (beginning & end). Indexing means traversing the list.
When you add to the end of an ArrayList, it will grow itself to have some room to spare. So if you have a ten-element ArrayList, adding at the end will cause it to internally allocate room for twenty elements, copy the ten you already had, and then add one. Then, when you add another element at the end, it just sticks that twelfth element into the space it already created.
This does not technically give it constant time insertion at the end, but it does give it amortized constant time insertion. That is to say, over a large number of operations, the cost approaches constant time; each time it grows, it doubles, so you'll have an ever-larger number of "free" constant-time inserts before you have to grow-and-copy again.
When you insert at the beginning, it can't do this and must always copy the whole list into a new location (linear time).
Removal from the end is always constant time because you just switch the last cell from being "filled" to "free space". You never need to copy the list.
As for your second question, a LinkedList keeps a pointer to the end of the list, so add and remove there just use that pointer and are thus constant time. There are no quick pointers into the middle of the list, so accessing an arbitrary element requires a linear-time traversal from start to (potentially) finish.
i) ArrayList -> You've got to push all the elements by one position in case of removal/addition in the beginning, hence linear time. At the end of array, you simply add or remove.
ii)LinkedList -> You have references of head and tail, hence you can add/remove anything there (in constant time).
Because removing at the end does not require moving the data. Adding may require copying to resize the storage array, but it's time is amortized.
Because adding at the end does not require walking the list, but indexing does.

Categories