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
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 3 years ago.
Improve this question
i am trying to make java program that ask number from user and runs following calculation: 1*3*5*7*....*usergivendouble. I think for loop is best for this but not sure how to make such loop. i tried
for(double i=1;i<=n;i+=2)
{
n*= 2;
}
but it just never stops asking new number.
im new to java and all help is appreciated!
Assuming n is the user given number, increasing n inside the loop is the problem. You increase n inside the loop and also use it as the loop's end condition. This causes an infinite loop, as the loop's condition is never met.
You need to change the code to be:
double multiplyRestult=1;
for(double i=1;i<=n;i+=2)
{
multiplyRestult*= i;
}
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 5 years ago.
Improve this question
hy , i'm a beginner java student and we were asked to take the sequence of indexes with the largest sum , the array have negative and positive numbers and may contain 0
our job is to get the largest sum of indexes that are in a row and it can be one index , with method not with algorithm because we haven't learned that yet
thank you for your help
I don't want to do your homework for you but here's some tips:
How do you know if a sequence is the largest?
When should you stop counting a sequence?
If the next number in the sequence is a negative is your sum getting larger?
How would you brute force this problem?
Do you have to brute force it? (No. Look at points 2 and 3 and think about it)
You should be able to figure it out from the questions above. If not I would definitely go ask your professor to get some help.
Now that you've figured it out, here are some questions to further your understanding:
What if I only wanted the largest 3 numbers in a row? 5? 7?
What if all of the numbers are negative? Positive?
If the parameters for question 5 and 6 were added into your question, even though you might think that they narrow down the problem, it actually makes it much harder to solve because of the constraints. This is why we need algorithms, so that we can solve problems with a set of constraints without having to brute force.
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 6 years ago.
Improve this question
enter image description here
This is the code screenshot link.
I am edit many time but every time the output is not completely sorted.
Your problem is very simply: you are increasing k far too often within your code!
Meaning: you are already looping with k; so you got:
for (int k=0; k < a.length; k++) { // dont use hardcoded "9" here btw!
and then you have
k++ again in the loop body. You simply dont have to do that!
Meaning: your k is growing faster than it should. But as your loop stops when k reaches 9; you are not processing all elements in your array!
Plus: insertion sort doesn't work with iterating your array once!
You have to keep iterating until all elements are in their place! You really want to study/think more about this algorithm. You, not us that is!
And as said, dont use hard-coded limits for arrays. You already say once that your array should contain 10 elements. From there on, you should be using a.length only! Besides: only use one-letter names for loop counters and such things. "a" is a pretty bad name for an array; why dont you call it "numbers" for example.
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
How can I go through several possibilities without exhausting loops in a program that sum of the proper divisors(divisors including 1 but not itself) of the number is greater than the number, but no subset of those divisors sums to the number itself.
For Example:
12=(factors sum)1+2+3+4+6
Atleast Any Subset Sum should be equal to the number i.e 12 in this case.
Question link
:http://www.practice.geeksforgeeks.org/problem-page.php?pid=301
Approach:
Step1 (Doubt)
By looking at the problem I found that its optimal solution will be Dynamic Programming.(Since I don't know that).
Apart from that I thought of the solution like this
For Example 12=1,2,3,4,6(divisors)
So,Solving it through like this with 1 starting having all permutations: 1+2+3+4+6 or 1+3+4+6 or 1+4+6 or 1+6(checking at each step whether it is <=12.
Similarly,I checked starting with 2: 2+3+4+6 or 2+4+6(I got and moved out of the loop)
The solution I am thinking here is very long.I have to put seperate loop for each number and also I am saving the divisors in a string.
Can anyone give me "Hint" how to start the problem without the Dynamic Programming(Dp) approach as I am learning Dp these days.
I assume you have a method
int[] getDivisors(int number)
the hard task here is to evaluate the possible sums.
An easy but long way would be, to iterate over all of those like that.
Think of such a sum as a binary code, 1 if its in sum, 0 if its not.
Now you can write a for-loop which, from 0...0 to 1...1 (in binary) with #1s = divisors.length;
Maybe a better way would be recursion methods. Like:
boolean hasSameSum(int number, Stack<Integer> divisors, int sum){
if(sum==number)
return false;
if(divisors.isEmpty())
return true;
int div = divisor.pop();
return hasSameSum(number, divisors, sum)&&hasSameSum(number, divisors, sum+div);
}
But honestly, that's not a programming problem, but more a mathematical approach problem :-)
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.