-4

enter image description here

This is the code screenshot link.

I am edit many time but every time the output is not completely sorted.

halfer
  • 19,824
  • 17
  • 99
  • 186
  • 2
    Questions asking for help should contain the code that you have tried. And they should contain a proper problem description. Please read at least about ["How do I ask a good question?"](http://stackoverflow.com/help/how-to-ask). – Seelenvirtuose Sep 10 '16 at 06:21
  • 1
    And side note: it is almost rude to ask for ASAP answers. Answers come when they come. If you want good answers in no time, then write good questions. Yours ... is not. – GhostCat Sep 10 '16 at 06:45

1 Answers1

1

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.

GhostCat
  • 137,827
  • 25
  • 176
  • 248