• 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

Search Element in array Problem

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

i am getting Couldn't Find even if i found the element. please help me. How to omit that message for found elements.
 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can keep a flag whose value will change to say "true" , just before break. If value of this flag is not "true" then print "element not found"
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And you see- the print- "Couldn't find" executes everytime the code comes out of the loop. So as said by Siddhesh, you would have to use a flag (or even the same index- checking if its reached the end of the array) to print the message- "Couldn't Find"
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also for searching you can use the method Arrays.binarySearch() which uses a fast searching algorithm.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, if this is real production code (and not just an exercise) - there are more efficient ways of sorting an array than the bubble sort algorithm that you're using. There are methods for sorting arrays in class java.util.Arrays, which implement an optimized quicksort algorithm.
 
Marshal
Posts: 79076
376
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesper de Jong wrote:. . . There are methods for sorting arrays in class java.util.Arrays, which implement an optimized quicksort algorithm.

Only for primitives, I see from the API. For Object[] sorting, it uses a merge sort, because merge sort is "stable".
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Only for primitives, I see from the API. For Object[] sorting, it uses a merge sort, because merge sort is "stable".


Aha, ok. "Stable" means that two elements that are equal will not switch order (they will be left in the original order that they were in in the array). Note that merge sort is still more efficient than bubble sort: the API docs mention that it's guaranteed O(N * log(N)) performance, whereas bubble sort has average and worst case performance of O(N^2).
 
Campbell Ritchie
Marshal
Posts: 79076
376
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I had always thought that sort() used merge sort throughout, but that is because I didn't read the whole of the API. That is the correct interpretation of "stable"; since primitives only have one value, there is no need for a stable sort. And bubble sort is only quicker for the very smallest arrays, otherwise it is notorious for its inefficiency.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic