Win a copy of Head First Android this week in the Android 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
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

How to understand this algorithm in Java?

 
Ranch Hand
Posts: 97
C++ Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I can't to understand this algorithm:




Why enters on while loop, knowing than a[i] is less than key variable ?
 
Bartender
Posts: 825
5
Python Ruby Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Why enters on while loop, knowing than a[i] is less than key variable ?



No it doesn't. Why don't you put print statement at the beginning of while loop so you can see what are the values when it is entered (or even better, use a debugger)? Try to add this:

and you will see for which values the while loop is executed.
 
Gustavo Siqueira
Ranch Hand
Posts: 97
C++ Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I Know. However, I still not understand. See the output:



In the last check, the key is 30. In other words, a[i] is less than 30. Then, why enters on loop while ?
 
Ranch Hand
Posts: 34
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

It didn't enter while loop when key is 30.




you don't need for loop here. it is confusing.
 
Gustavo Siqueira
Ranch Hand
Posts: 97
C++ Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks you! I understood the logic. However, me appears that something is wrong. See:



See in "Out of the while: [1, 10, 12, 5, 30]". The correct would to be: 10,1,12,5,30. Alright ?
 
Suresh Sajja
Ranch Hand
Posts: 34
Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Gustavo Siqueira wrote:



See in "Out of the while: [1, 10, 12, 5, 30]". The correct would to be: 10,1,12,5,30. Alright ?




 
Kemal Sokolovic
Bartender
Posts: 825
5
Python Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I meant to put the print statement inside while loop to follow for what values the while is executed:



The result (of just this print) would be:
 
Gustavo Siqueira
Ranch Hand
Posts: 97
C++ Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Suresh Sajja wrote:

Gustavo Siqueira wrote:



See in "Out of the while: [1, 10, 12, 5, 30]". The correct would to be: 10,1,12,5,30. Alright ?






At thist point: a[i + 1] = key; The value is 1. In other words, 0 + 1 = 1.
 
Suresh Sajja
Ranch Hand
Posts: 34
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I added print statement to see value of i



When i = -1, while loop exits because it fails to honor condition "i >= 0"
reply
    Bookmark Topic Watch Topic
  • New Topic