Java insertion sort not working properly [closed] - java

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.

Related

Using threads for insertion-sort [closed]

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

What is difference between below Code 1 & Code 2? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
Code 1
String ccMailAddresses="abc#co.in,xyz#co.in";
String ccMailAddressArray[]=ccMailAddresses.split(",");
for (int index = 0; index < ccMailAddressArray.length; index++) {
System.out.println(ccMailAddressArray[index]);
}
Code 2
String ccMailAddresses="abc#co.in,xy";
for (String ccMailAddress : ccMailAddresses.split(",")){
System.out.println(ccMailAddress );
}
Which is best practice ? Is any performance hit in any one of them ?
Obviously, the second piece of code is shorter and working on a higher level of abstraction - as it is using a for-each loop; instead of that counting-for loop.
In that sense: code2 requires less efforts for the reader to grasp what is going on. So, from that perspective, it is definitely "better" than code1.
Performance wise, there shouldn't be any different between the two.
The code does nearly the same thing. The if-statement in Code1 is useless. In my opinion this is alway true.
The difference is betweeen those two line:
for (int index = 0; index < ccMailAddressArray.length; index++)
Here your code will be executed until your index is bigger than the lenght of your array.
for (String ccMailAddress : ccMailAddresses.split(","))
The code in this loop is executed for every element in your array.
But keep in mind the result is the same!
hope that helps!
No difference between both code:
code1 is traditional for loop , and code2 is for each loop introduced for java collection to iterate.
Code 1
String ccMailAddresses="abc#co.in,xyz#co.in";
String ccMailAddressArray[]=ccMailAddresses.split(",");
if(ccMailAddressArray!=null){
for (int index = 0; index < ccMailAddressArray.length; index++) {
System.out.println(ccMailAddressArray[index]);
}
}
In this example you make an unnecessary check against ccMailAddressArray because split() will never return null. The for loop here provides you an index. This can be useful sometimes e.g. if you have a color array that has a color stored for each element. Then you could use color[index] to select the color for each element and draw it somewhere. Please now, that this is a bit more complex than just iterating over all items. So you use this version whenever you need the index.
Code 2
String ccMailAddresses="abc#co.in,xy";
for (String ccMailAddress : ccMailAddresses.split(",")){
System.out.println(ccMailAddress );
}
In this example you do essentially the same. The for-each loop you're using here does the same for you. The only difference is that you don't have the index here. It's just a bit shorter and simpler which makes it also easier to read.
So: Use for-each (Code 2) whenever you can because it is a bit simpler to understand what you are dong. If you use the index based for loop (Code 1) I assume you want to do something with index which also implies that some more complicated things may be involved.
Thanks for an answer stackoverflowgigs.
My question is solved now!. As one of my senior gave me the review comment about code 2 is performance hit by repeatedly calling ccMailAddresses.split(",") in for each loop.

nested for not working as intended in java [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I want to sum the elements of 2 different lists.
in my test list 1 contains 1 and 2
and list 2 contains 3 and 4.
I want to sum the elements of list 1 and list 2 like this:
1+3, 1+4, 2+3, 2+4.
I've tried the code below,
but it does not work.
my code:
for (int i = 0; i < l1.size(); i++) {
for (int j = 0; j < l2.size(); j++) {
System.out.println( l1.get(i) + l2.get(i) );
}
}
my output always shows
5
5
5
5
kindly help and correct me if im wrong or missed the logic.
you have to use j at some point, at which you should be able to figure out yourself
this is a very common error with starters I guess, because using "i" becomes a unquestioned habit
to avoid this problem you could start using another naming pattern,
eg. call them it1 and it2 when iterating lists, and x y when traversing coordinates etc., this way you know what you intended to do with them
this also can improve readability a lot,
if you ever have to refine a complex nested for you will curse at not using better names
you might want to consider using the refactoring (in eclipse strg+1 and "rename in file") to give the iterators a more meaningful name afterwards, or if you get confused midway through the algorithm even beforehand

java boolean array and turning every third value to false [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 8 years ago.
Improve this question
The Boolean array is initialized to true. I need to know how to turn every third value to false and also going through the array over and over. It is essentially duck duck goose without the randomness.
This sounds like a beginners programming exercise, so I'm going to just give you a hint or two:
Go back to your textbook, lecture notes, tutorial and reread the stuff on for loops. Focus on the old style ones.
Think about how to write a for loop that steps through integer index values in the pattern that you require.
Re "... going through the array over and over" - not sure what you mean, but maybe the hint for this is to think about using nested loops; i.e. a loop inside another loop.
But the most important advice is to try and work this out for yourself.
Well I'm really not sure what you mean by going through the array over and over but the following code will turn every third value to false.
for (int i = 0; i < myVar.length; i++) {
if (i % 3 == 0) {
myVar[i] = false;
}
}
Edit: Oops someone beat me to it while I was typing lol.

Is For loop in reverse better? [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 9 years ago.
Improve this question
Is there any difference in time space complexity for these two?
for (int i=0; i<= 100; i++) {
System.out.println("hi");
}
for (int i=100; i>= 0; i--) {
System.out.println("hi");
}
And what if the loop is larger and complex? Like when i <= 1000000
No, there is no difference in space or time complexity between the two.
I'd be curious to hear why you thought there might be.
P.S. Of course, if the actual code is different, then the answer might be different too.
For these simple loops? No. You're just working with a constant string, some of System.out.println()'s overhead, and an iterated primitive.
However, for more complex nested loops, there could be differences, especially if loops are nested with changing inner loop lengths.
No both has same time space complexity.
As such no difference in time complexity i.e O(N).
Deciding on which approach to follow is your design specific. For example if I have to find largest prime factor of a given number I would probably use 2nd approach. So basically it is design(of your intended project) specific.
For this particular example is not. For larger arrays it might be slower on some systems.
Usually systems are optimized for accessing memory sequentially. As long as memory access pattern is predictable both loops have same efficiency but on older machines or different architectures things may be different.
For space complexity is not (i miss-read the question)
As long as you don't break out of the loop with some logic (depending on index variable) there should be no difference in space/complexity.
If you need to iterate on every single value of your index, going in reverse order is exactly like going in forward.

Categories