is there any way to sort a Hashtable in J2ME? because I'm trying to adapt the bubble sort to this case but I did not find a solution. Could anybody help me please?
You will have to use a Vector or array.
In this question you can check an implementation of the sort:
How to sort a Vector of String in java-me?
Related
I have an array
arr = [2,1,3,1,4,1,3]
I have to find and remove the sequence such that only unique elements remain in an array.
output
suppose we found [1,1,3] subsequence and after deletion of 1,1,3
the array will look like this [2,4,1,3]
now this array has become unique.
I am thinking of some solution using dynamic programming but I am not able to think of some good solution. Can anyone help me with some hint?
You don't need dynamic programming.
In the simplest case for every element find the first occurence in array and delete item if index differs. Quadratic algorithm.
If you have memory, make hash map, put elements if they are new and delete otherwise (linear approach if we don't consider removing)
I am trying to implement an (Language1 - Language2) dictionary.
I want to make an search algorithm in both direction that speed is faster than O(n)
For example, if (hello, hola) is one pair,
SEARCH_SPANISH_BY_ENGLISH (hello) = "hola", and
SEARCH_ENGLISH_BY_SPANISH (hola) = "hello"
If you have an idea for an algorithm, can you tell me how to set up an dictionary and implement an search algorithm? It seems like I have to use an divide and conquer but I am not sure how. Thanks.
The side of dictionary should be singular, which means I cannot build both English-Spanish and Spanish-English dictionary.
You can have two dictionary as the search time will remain constant. However, if you still wish to have two way dictionary, then Jon Skeet's version is here:
Getting key of value of a generic Dictionary?
Any balanced tree, or sorted array, or hashtable data structure will give you better than O(n) when searching.
For the bidirectionality, you can just have two data structures, one for English-Español the other for Español-English. That doesn't necessarily mean two complete dictionaries, just that you need a search index of some description for either direction.
However, since you appear to have a one-to-one mapping of words, you can just maintain one dictionary as persistent and build the reverse one at run time.
In randomized Quicksort, should I randomize the input data first and then use the first element as the pivot, or should I not change the input data but choose a random pivot?
I am a bit confused about what needs to be randomized.
If your array is not randomly distributed already, then just select a random pivot on the array, that is what randomised quicksort is for.
If your array is previously randomised, then use a normal quicksort
An easy way to do this is:
Simply take the array input as it is. Choose a random index in the array everytime, and replace it with the last element. And then take the last element as the pivot, as you would in a normal quick sort.
Hope it helps.
I know KMP (Knuth–Morris–Pratt) is used for one dimensional search. Can it be applied on 2 dimensional array of data? Or is there a more advanced one?
Maybe you can find something in this question. You should be able to use KMP with some adaption if you flatten the matrices you are looking for.
Could anybody help me with the bucket sort algorithm for integers? Often people mistakenly say they have this algorithm, but actually have a counting sort! Maybe it works similarly, but it is something different.
I hope you will help me find the right way, because now I have no idea (Cormen's book and Wikipedia are not so helpful).
Thanks in advance for all your respones.
Bucket sort can be seen as a
generalization of counting sort; in
fact, if each bucket has size 1 then
bucket sort degenerates to counting
sort.
Counting sort will only work on integers, while bucket sort can work on anything with a value, also, the final loop is a bit different.
Counting Sort maintains an additional array of ints, which basically counts how many of a certain number there is, and then creates the number again when it goes through the additional array in the final loop, what I mean by this is - in a OOP way of looking at it, it's not the same object, but a new object with identical value.
Then, we have bucket sort. Bucket sort goes through the array, but instead of just going ++ in the relevant place in the array, it inserts the item into a list of some kind (I like to use a queue, that way it's a stable sort). Then, in the final loop, the algorithm goes through the entire additional array, and dequeues the elements in each bucket into the array. That way it's the same object.
If you're sorting anything and you know that the range of numbers is smaller then nlogn, it's simple - use counting sort if it's integers, and bucket sort if the object has some additional data. You can use bucket sort for integers, sure, but counting sort will take much less space.