• Post Reply Bookmark Topic Watch Topic
  • New Topic

Getting error in insertion sort  RSS feed

 
Sakshi Jaine
Greenhorn
Posts: 26
Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was trying to write code for insertion sort but there is some error in my code.
Can you please help....


 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can see lots of errors, which unfortunately are obscuring the logic error you are noticing.
  • 1: The bit about class being called Main is a red herring; that is probably a NetBeans‑specific features.
  • 2: Your method probably does not throw any Exceptions. Remove the declaration. If you have that declaration in a template, edit the template.
  • 3: Variable names shouldn't start with CapitalLetters.
  • 4: Break that code up into multiple methods; the main method shou‍ld only contain one statement. The array‑swapping for example shou‍ld be a method of its own.
  • 5: You have written the for loop wrongly. Get rid of the - 1 and the <=. Write for (int i =  0  1; i < myArray.length; i++) ... If you need to change anything, make those changes from the usual form of the loop.
  • 6: Put spaces around all binary operators including =
  • 7: Get rid of the imports because you don't need any of them.
  • When you have done that you will be able to see what is happening. All those things make the code harder to read and are obscuring whatever you are noticing going wrong. Note most for loops start from 0 but you have chosen to start from 1. It is confusing that you have lots of − 1s and + 1s in that loop, which make it difficult to read. You might be better going 0...< myArray.length − 1.

    Please explain simply what the algorithm for insertion sort is, and tell us what is happening. I suspect you have actually written a bubble sort program.
     
    Sakshi Jaine
    Greenhorn
    Posts: 26
    Chrome
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I do not know how to make methords.
    I am quite new to coding.

    I have written this code according to the book I am reffering (Algorithms by Coremen)
    I am new to algorithms too

    I àam trying to prepare for Indian Computing Olympiad
     
    fred rosenberger
    lowercase baba
    Bartender
    Posts: 12565
    49
    Chrome Java Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I don't think you need the Len variable at all...it is much more obvious what "A.length" is than a stray variable named "Len".

    "A" is a terrible name for a variable.  it doesn't tell you what it is...something like "myArray" is clearer.

    When you say "there is some error in my code", could you elaborate?  does it compile? does it throw an error when it runs? does it not insert elements? 

    The more details you can give us on what is wrong, the easier it is for someone to help you.
     
    Sakshi Jaine
    Greenhorn
    Posts: 26
    Chrome
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    There is a runtime error in the code.
    I think the error is in the swapping part, line 24,25,26.
     
    Sakshi Jaine
    Greenhorn
    Posts: 26
    Chrome
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator


    the output is comming
    [5,5,4,6,1,3]
     
    Piet Souris
    Master Rancher
    Posts: 2044
    75
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    hi Sakshi,

    You start off well. You compare myArray(1) to myArray(0) (2 vs 5), and so you shift myArray(0) to myArray(1).
    That is as it should be. Then, you decrement i so that it becomes -1, and so you get an ArrayIndexOutOfBoundsException when you compare myArray[i] to the key.
    So, in your while condition, you must check that i >= 0, indicating that you have reached the beginning of  myArray and you have to stop there.

    Now, two simple ways to do this:

    One of those two lines still raises the exception. Can you tell which one and why it will give the exception?

    Have a go, and then inspect the output of what should be the sorted arrays. What do you notice?
     
    Sakshi Jaine
    Greenhorn
    Posts: 26
    Chrome
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The first one will still give exception.

    thank you very much
    my program is now running just fine.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!