Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Insertion sort - arrayOutOffBoundException.

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well hello every one!
I don't get working code of insertion sort algorithm.
I tried to write:


What do you think - that can be something wrong?
and why is arrayOutOffBoundException occur?
thank you.
 
Saloon Keeper
Posts: 8609
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First off, look at the line number the exception message gives you. Then place a println() call just before that and display the index values.
 
Carey Brown
Saloon Keeper
Posts: 8609
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't use bit-wise AND (&) in your conditional expression, use logical AND (&&).
 
Ranch Hand
Posts: 954
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You made it very complex. Why don't just close a computer monitor and take pen and paper. Write pseudo code in plain English and try to implement it then.

I bet if you do then your code could not be more than 5-6 lines more.
 
Greenhorn
Posts: 9
Netbeans IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No need to complicate things. Here's the method :-

 
Marshal
Posts: 74085
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch
 
Anton Sotnikov
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


yes, thanks it's work!
I can't do this code couple days - think i am not good.

By the way, what do you think about time coplexity of sorting algorithm:
it can be that:


Or there is another way?

 
Animesh Shaw
Greenhorn
Posts: 9
Netbeans IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anton Sotnikov wrote:

yes, thanks it's work!
I can't do this code couple days - think i am not good.

By the way, what do you think about time coplexity of sorting algorithm:
it can be that:


Or there is another way?




The Time you tried to find using the Current time millies is not accurate, also you shouldn't call this as time complexity. Time Complexity is a mathematical definition to measure the performance of an algorithm. The Worst case performance in case of Insertion Sort is O(n^2) which is no good than Bubble or Selection sort but still its should be preferred over the latter two in the sense that the no. of comparisons made to sort the array is less.

If you wanna really measure the time taken to sort the list then use System.nanoTime() and make sure that all other application processes or windows are closed. Moreover This measurement differs from System to System and depends on your hardware specification.

If you wanna measure the time complexity then use Mathematical definition rather than currentTimeMillies()



Thank you.
Sincerely,
Psycho_Coder.
 
Destroy anything that stands in your way. Except this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic