• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

School assignment : quicksort

 
Rancher
Posts: 317
16
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello...
I have to implement a random quicksort and it does not work
Here is my code: I made it return an array to drive some tests.

The error I get is:
arrays first differed at element [0];
Expected :18
Actual   :1

arrays first differed at element [0];
Expected :7
Actual   :1

arrays first differed at element [2];
Expected :8
Actual   :7




The median function I wrote is :




My test:



Even when I try to almost litteraly copying a better implementation than mine, it does not work!
I get stack overflow.




Can anyone please tell me what I am doing wrong?

 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

This isn't what's causing your bug but why would you shuffle your input array on line 8? What's the point of that? It should be assumed that the input array is unsorted so why would you go an extra step to shuffle everything around again? Doesn't make sense to me.
 
D.J. Quavern
Rancher
Posts: 317
16
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's a recomendation from Sedgewick book. Algorithms 4th edition.


It is not what is causing the bug.

I actually solved that bug, but I have a new one... I will post shortly.

Can I close a thread?
Screenshot-2019-02-24-at-16.17.26.png
[Thumbnail for Screenshot-2019-02-24-at-16.17.26.png]
 
D.J. Quavern
Rancher
Posts: 317
16
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for checking Junilu.

Can I close this thread and open a new one for the new bug?
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interesting. Explained that way, it makes perfect sense. This is actually one case where an explaining comment would be perfect.

 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have marked the thread as resolved but it would be nice if you could post a description of the bug and what you did to fix it. Posting the fixed version of the code would be nice as well.
 
D.J. Quavern
Rancher
Posts: 317
16
IntelliJ IDE Firefox Browser Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:I have marked the thread as resolved but it would be nice if you could post a description of the bug and what you did to fix it. Posting the fixed version of the code would be nice as well.



Yes, absolutely. It was actually a multiple bug caused by the fact that ...I did not really understand how quicksort worked. I don't know how else to describe the issue honestly
I understood when I thoroughly implemented that tutorial:


The partition method was right, but in the recursive call I sent the pivot one time too much.



should be


My final code:



My Util class that supports 4 implementations of quicksort:

 
F is for finger. Can you stick your finger in your nose? Doesn't that feel nice? Now try this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic