• 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
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Linear Search Method not Working as intended

 
Ranch Hand
Posts: 105
3
Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all, so I have been practicing with linear search, binary search, and various other sorting techniques. However, I have only had success with my different sorting methods correctly sorting my randomly generated array. The problem I am currently having is that my linear array should be finding the location of my middle three elements in my original unsorted array and returning their location, while my binary array should just be searching an already sorted array(since binary requires the array to be sorted) and returning the location of the middle three values there. By location I mean index location. I believe my binary is working as intended, but my linear does not seem to want to cooperate and as much as I can figure out, it should be printing out its locations in the random array. It does this somewhat accurately when I comment out other code to narrow in on the problem, but at the sacrifice of the binary method working as intended. I'm not sure where to go from here. Here is my code:

 
Sheriff
Posts: 28368
99
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm confused here:



You pass a double array named "elements" to this method, and then the method doesn't use it at all. Instead it uses the double array which is an instance variable of the class. On the other hand you have



which does use the double array passed to the method, and which ignores the double array which is an instance of the class.

So obviously there's some confusion with the design of this Search class. What's the purpose of the array which it contains? Are the search methods supposed to act on that array? If so then why do they have another array as first parameter? On the other hand if they are supposed to act on the arrays passed as first parameter, then why does the Search object contain an array which isn't used?
 
David Vach
Ranch Hand
Posts: 105
3
Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To be completely honest, I'm not entirely sure. When I was having trouble with my linear method only returning the value -1, my teacher had me add in a lot of that extra code that you referenced above. I'm not sure what she was trying to have me do, as what she told me to add didn't seem to fit. To the best of my understanding each parameter is supposed to be referenced from the SortSearchApp class which print out all of this information. Hence this code:

       

which should be taking the original unsorted array from the SortSearchApp class and running it through the linear search method while comparing it to the array3merge which is already sorted to find the middle values in array3merge and find their equivalent location in the unsorted array. Im not really sure why I have those extra arrays in my Search class, my teacher had me add those even though they made no sense as to why they were added.
 
Paul Clapham
Sheriff
Posts: 28368
99
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Seems like the only one of us who understands the code is your teacher, then. I'd suggest going back and asking for more explanation.
 
Paul Clapham
Sheriff
Posts: 28368
99
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the meantime, you're asserting that array3merge is a sorted array. I don't understand that because I don't see a declaration for it and I don't see any code which causes it to be sorted, or even to contain any data at all.

And:



If you're looking for the position of array3merge[14] in the array array3merge then you don't need a search, binary or otherwise. The position of that element is at index 14: that's what the "[14]" means there.
 
David Vach
Ranch Hand
Posts: 105
3
Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the code for my sorted methods. Sorry I didn't include it earlier, I could've sworn I did. Here is my full code:
 
Sheriff
Posts: 8988
652
Mac OS X Spring VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Was reading your initial post - it was hard to follow.

Can you identify one issue (first one) out of many you have, so we'd do one step at a time.
Please specify in which lines range you think that issue appears or method name. Please specify what inputs you were giving and what was exactly wrong.
 
Shiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic