Win a copy of Programmers Guide to Apache Thrift this week in the Open Source 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

How to check elements are out of order in list  RSS feed

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have elements in the list in below mentioned order.

1,2,4,3,5,3,6,3,7,8,9

Here I have to check whether elements in the list are out of order or not.
For example in the above list 3 is out of order since it comes after 4, its not in sequential order and hence I need to report that it is out of order.
But if we can see the later occurrences of 3 (3 after 5 and 3 after 6)  should not be considered because it is duplicate so only one out of order i.e 3 after 4 (the first occurrence) should be reported.

I tried to implement in java but its not working as expected.......please suggest how will I do this in simple logic because I have to check this for every element of the list.
 
Marshal
Posts: 64471
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Surekha Gaikwad wrote:. . . I have to check this for every element of the list.

Not quite. I suggest:-
  • 1: Look at this section in the Java™ Tutorials.
  • 2: You have to check one element fewer than there are elements in the List. I shall let you guess what you are checking for.
  • 3: If you had a type of element you had never seen before, how would you check whether they are in order? Write it down in words of one syllable, not using any “computing words”, assuming they have a straightforward test for whether they are in order.
  • 4: Be very specific about what you are checking. If you reach the first 3, in this instance you stop and report elements out of order.
  •  
    Surekha Gaikwad
    Greenhorn
    Posts: 10
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The thing is I have to check no of such elements which are out of order...not just to check whether the list is out of order hence I have to scan list further.
    Moreover the elements are all integers so no such issue of checking data type
     
    lowercase baba
    Posts: 12745
    51
    Chrome Java Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Do you have to report THAT the list is out of order, or do you have to report all the ELEMENTS that are out of order?  The former lets you stop as soon as you find one, the latter requires you to check them all (or almost all).
     
    Campbell Ritchie
    Marshal
    Posts: 64471
    225
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Surekha Gaikwad wrote:The thing is I have to check no of such elements which are out of order... . .

    That is different from what you said at first.

    Moreover the elements are all integers so no such issue of checking data type

    Yes, you do have to check the datatype. Since Java® is a strongly‑typed language, confirmation that all the elements are the correct type usually takes place at compile time.  Verifying they are all integers is therefore very very simple; it has been done for you already if you wrote List<Integer>. You also have to verify the technique for testing which elements are larger and smaller. Again, that is very very simple.

    But simple doesn't mean the same as not requiring any effort at all.
     
    Saloon Keeper
    Posts: 3250
    128
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    But first thing is removing duplicates while maintaining the order, and, depending on knowledge and experience,, that may not be that easy.
     
    Campbell Ritchie
    Marshal
    Posts: 64471
    225
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Why would you need to remove duplicates? The following List<Integer> has all its elements correctly ordered:-

    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

     
    Piet Souris
    Saloon Keeper
    Posts: 3250
    128
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    As OP writes, if you have 5, 3, 3 then that only counts for one inversion.
     
    Bartender
    Posts: 2277
    95
    Eclipse IDE Google Web Toolkit Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Surekha Gaikwad wrote:...I tried to implement in java but its not working as expected...


    You forgot to share your code, Please share your code so that we can help you fix it
     
    A lot of people cry when they cut onions. The trick is not to form an emotional bond. This tiny ad told me:
    ScroogeXHTML - small and flexible RTF to HTML converter library
    https://coderanch.com/t/710903/ScroogeXHTML-RTF-HTML-XHTML-converter
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!