Win a copy of Microservices Testing (Live Project) this week in the Spring 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Why the same algorithm does not print out the input array ?

 
Ranch Hand
Posts: 896
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Today, I practised more algorithms and I am stumped by this same algorithm that gives a different result



Hope someone can explain where did i go wrongly. Again, my heartfelt thanks to this forum members who have been coaching me so much.
 
Bartender
Posts: 5021
186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And we will keep helping you for as long as you need us!

But if I run your code (after outcommenting the second sort-routine, since that is a duplicate of the first) I get this result:

1 4 3 5 6 2
1 3 4 5 6 2
1 3 4 5 6 2
1 3 4 5 6 2
1 2 3 4 5 6
3 4 2 1
2 3 4 1
1 2 3 4


And that seems okay to me.
 
Piet Souris
Bartender
Posts: 5021
186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And if you want to start with printing the input arrayList, then add this line:

at line 3 and a half.
 
tangara goh
Ranch Hand
Posts: 896
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:And we will keep helping you for as long as you need us!

But if I run your code (after outcommenting the second sort-routine, since that is a duplicate of the first) I get this result:


3 4 2 1
2 3 4 1
1 2 3 4


And that seems okay to me.



It should print out 4 3 2 1 but it doesn't.  This is the part that puzzles me.
 
Piet Souris
Bartender
Posts: 5021
186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this:
 
Piet Souris
Bartender
Posts: 5021
186
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you figure out the answer to your question?

In your code, the first arraylist printed is the arraylist after the first switch from element 1 and element 0.

Now, in that first list, since 4 > 1, no switch took place and so it looks as if the inputlist is printed. In the second arraylist, 4 > 3, so these get swapped and therefore it seems as if now the inputlist is NOT shown.

In the snippet that I gave, you can follow exactly what happens during the sort.
 
tangara goh
Ranch Hand
Posts: 896
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:Did you figure out the answer to your question?

In your code, the first arraylist printed is the arraylist after the first switch from element 1 and element 0.

Now, in that first list, since 4 > 1, no switch took place and so it looks as if the inputlist is printed. In the second arraylist, 4 > 3, so these get swapped and therefore it seems as if now the inputlist is NOT shown.

In the snippet that I gave, you can follow exactly what happens during the sort.



Without what you have pointed out to me, I would be clueless till today.  Thank you so much.

Now, I tried to put in :


But, it will be printed with the brackets.  Now, there are solutions offered by stackoverflow like this one

https://stackoverflow.com/questions/32774059/output-arraylist-to-string-without-brackets-appearing

I just wonder if there is anything that can be added to the code that it will print out the array without what is suggested in SO or another way to do this insertion sort without adding those lines above ?

Really, i find all these hackerRank questions very tricky but once again thank you all the great people here.
 
Piet Souris
Bartender
Posts: 5021
186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Tangara,

at StackOverflow several methods are given to print the arraylist in several ways. How would you like the list to be printed (can you give an example) and under what condition do you want that print? Only when a.get(0) > a.get(1)?
 
What does a metric clock look like? I bet it is nothing like this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic