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 8 years ago.
Improve this question
I've implemented it successfully with count, the book I'm using to learn just shows the code but without any proper explanation.
I've tried doing some search on google but can't find a material explaining it.
All what I've understood that Front and Rear points will assume the same location if the array is empty or full, so we are using an extra slot in the array "to do some sort of checking I don't understand to get it done".
Does anybody have a tutorial on this? I'm sorry if this comes as waiting for someone to feed me information, but I truly cannot find anything.
The math is pretty simple: if you have the beginning and the ending indexes, you can have three situations there:
The ending index is lower,
The beginning index is lower, and
The indexes are the same.
When the ending index is lower, say, b=10 and e=3, all the data is located between the two indexes. You can compute the count by subtracting the ending index from the beginning index, b-e: in my example, you've got seven items in the queue.
When the beginning index is lower, say, b=3 and e=10, then the data goes around the end of the queue. Say, the total size of your queue is N=100. Then the number of data items is b+N-e, or 3+100-10=93 elements.
The fact that you would be able to use at most N-1 elements follows from these two formulas: the highest number of entries that you can have is N-1 for both formulas, because the situation when all N entries are filled up is indistinguishable from the situation when the queue is empty.
You can check if your queue is full by comparing (b+1)%N to e. If they are the same, you cannot put any more data into your queue.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 days ago.
Improve this question
I am using insertion-sort to sort the k first values in my array,
First i start by sorting the k first in decreasing order, after that i check the [k+1,n] values if they are larger thatn the lowest number in k, being k-1.
the check is done with threads, where I give every thread a segment of [k+1,n], where each of them will check the values in their segment if they are larger than k-1, if yes they will set the value in the correct place.
My problem is that the parallel version is MUCH slower than if I do it in a sequence, like 100x slower, the test i gave was makin k = 100 and n = 10milion.
Anyone know if you can parallel insertion sort?
If code is needed I can post
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 2 years ago.
Improve this question
I wonder how to get the number of items in a ShortBuffer.
I want the number of items that are really in the buffer, not the maximum capacity.
Thanks.
Buffer is not a Collection, but a (comparatively thin) wrapper around a primitive array that provides some useful methods for operating on groups of primitive values. Like a primitive array, it always contains values for each valid index.
Therefore the number of items is always equal to its capacity.
It does not keep track of which indices have already been written to since its creation. And as one major use case is to wrap an existing array while still reflecting all changes to the wrapped array, that would not even be possible to implement.
ShortBuffer holds a position that keeps track on where to put elements. You can use it to know how many elements you put in. The number of elements always equals its capacity as others mentioned.
#Test
fun testShortBuffer() {
val shortBuffer = ShortBuffer.allocate(1024)
println(shortBuffer.position()) // position 0
shortBuffer.put(1)
println(shortBuffer.position()) // position 1
shortBuffer.put(shortArrayOf(2, 3, 4))
println(shortBuffer.position()) // position 4
shortBuffer.clear()
println(shortBuffer.position()) // position 0
}
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 8 years ago.
Improve this question
Is it better to use linked list over arraylist for implementaion of Queue?
If I dequeue , the over load would be less in the case of liked list implementation?
The time it takes to dequeue an element from the beginning of a LinkedList is less than from ArrayList.
This is because ArrayList is based on an array, and when a first element is removed all elements except the dequeued need to be shifted one position left. The greater the number of elements there is in ArrayList, the longer it will take.
In case of LinkedList, no matter how big the list is, there just constant number of references that should be updated in order to dequeue a first element.
Of course you can dequeue elements from the end of an ArrayList and it will take constant time (most of the time), but than addition of a new element (to the beginning) will require to shift all elements one position to the right.
There is already a LikedList queue implementation in Java. I did not analyse the code but I would guess that a LinkedList if faster than an ArrayList in dequeuing an entry.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
This is how my text file looks
Cincinnati
Oxford
Chicago
New York
Las Vegas
Houston
Detroit
Miami
Denver
Boston
I want to populate an ArrayListCity randomly from the text file and then sort it with collections.sort. All of the cities from the text file should be in the array list, but in a different order each time the program is run.
Read the file sequentially - thats the easiest route.
Then randomly shuffle the collection.
Actually, another question. Could you use math.random() to look at the lines of the text file and if, lets say, the line 5 comes up then you remove it from the parameters you set for math.random()? It would pick from 0-9 and after the fifth line is used it would pick from 0-4,6-9
It is possible - but over complicates things. The best way to implement such a feature, is to have a 'pool' of numbers. (i.e. an arraylist of Integer objects), then you can use a Random number generator (between 0 and arrayList.size()) to get (and remove it from your arrayList too) one of these Integer objects. Then read that line. This approach needs several objects (Random, Arraylist, Integer, Reader).
At best, its overcomplicated for something so simple. Best thing to do, like I said, read each line and insert it into the arrayList. Then suffle.
Another take on this is, read each line, get the size of the arraylist and insert the new String, randomly within the arraylist. Heres some code:
arrayList.add(getRandomIndex(arrayList.size()),string);
public int getRandomIndex(int size){
return ((int)Math.random()*size)
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
Question is: You have Singly linkedlist which contains String[any no of chars] in its node. Now How to find combined String[no need of building string from linkedlist] is palindrome or not ?
Example: ["abc"]-->["d"]-->["ed"]-->["c"]-->["ba"] --> is valid input linkedList
combined string would be- abcdedcba
This is what I have thought of:
1.Traverse all list by node and compute combined string length-N.
2.take two pointer point to First Node, P1, P2.
3.advance P1 till reach to string length >=N/2.[we have to remember previous total length] now advance char pointer within node string to point to N/2+2th char[if N is odd] or N/2+1th[if N is even].
4.Start from P2 with char pointer to first char of node string. and perform equal on chars.
5.If P1 reach to end with all char exhausted. then string is palindrome.
but in this approach i'm traversing linkedlist 2.5 times. which will be not good if LinkedList will be huge.
Can anyone suggest better approach.
Your solution is optimal, you cannot do anything with the list faster than O(n) (because the problem requires reading all its elements), and your solution is O(n). Traversing 1.5 times (not 2.5 times - one iteration to the half, and one from the half to begining and from the halt to the end) is also very small constant. If you do not want to rebuild the string I am pretty sure that there is no faster solution (Although your solution consumes N/2 additonal space, but it is unavoidable if you have just one-directional list).