File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes How to compare the elements of the array and remove the smallest item?! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to compare the elements of the array and remove the smallest item?!" Watch "How to compare the elements of the array and remove the smallest item?!" New topic
Author

How to compare the elements of the array and remove the smallest item?!

Nadine Ernest
Greenhorn

Joined: Dec 10, 2008
Posts: 27
Hey guys,
im having some trouble!

I have an array of integers x.

I need to implement a method sort ( int []x) to sort the array x using a priority queue.

This method should return an array which is sorted!!

The solution to this problem is:

I already have the array int [] x.
so what im going to do is search for the smallest item and then remove them by order from smallest to biggest!

My PROBLEM IS : i dont know know how to search my array int[]x to find the smallest item in the array, i know i have to compare each item with the one before it but i cant seem to write the code of it!
help guys!
Thanks a lot!!
BV Boose
Ranch Hand

Joined: Jul 26, 2008
Posts: 33
you gotta start somewhere, what do you have so far?
Nadine Ernest
Greenhorn

Joined: Dec 10, 2008
Posts: 27
I got the following:

The next step im going to remove the smallest item from the array so that it displays in sorted order!

Please correct me if im wrong!
[ December 10, 2008: Message edited by: nadine ernest ]
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

There are three problems with your code so far, but perhaps I'm not seeing something...

1) you are not incrementing (moving) the index. So that loop, as written, is infinite.

edit: oops, scratch that... it exits after one iteration. I should have said "loops that don't increment the loop index, are generally infinite loops". But because of problem #2, your loop actually just exits after a single iteration. (or none, depending on the values being compared).

2) having solved that (you've thrown in an i++ somewhere) all you're doing in the loop is overwriting every array entry with whatever value is in 'smallest'. I think you mean to set smallest equal to the array value (or perhaps the value of the index, i)?

3) having solved that, it will exit, the moment the condition **first** becomes true. That doesn't mean though, it couldn't have been true again, if you kept going. You want to find the smallest number in the entire array, not just the first time you get a smaller number than the 'next' one.
[ December 10, 2008: Message edited by: Mike Curwen ]
Faraz Masood
Ranch Hand

Joined: May 03, 2002
Posts: 55
hi nadine,

you can also have a look at :

http://java.sun.com/j2se/1.4.2/docs/api/java/util/Arrays.html

and see if it helps.

/Faraz
Manuel Leiria
Ranch Hand

Joined: Jul 13, 2007
Posts: 171
And take a look at Sorting algorithms


Manuel Leiria<br /> <br />--------------<br />Peace cannot be kept by force; it can only be achieved by understanding. <br /> Albert Einstein
Ove Lindström
Ranch Hand

Joined: Mar 10, 2008
Posts: 326

That is how Sun implemented it. I do not allow my students to use the Collection classes for this kind of problems until they have shown that they understand how to implement sorting and find algorithms.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
Ove is correct; this is an exercise in writing your own "find the minimum" algorithm. I don't think this is a job for the Arrays class. If the title of this thread accurately reflects the task required, it's not a job for for sorting algorithms either.

What follows will require lots of work with a pencil and paper; you will have to go through it in your head and write down what the variables will be before you start, after the first stage, etc., etc. My interpretation depends on the title of the thread; if that is not precise, then you will have to alter what follows accordingly.

What you are looking for it the logic behind doing it. Suggest
  • Write out an array of numbers 9483 12 045654 2845 7980 -3948593 23 -389475 etc.
  • Work out which is the smallest
  • Write down how you work out the smallest. Hint you do need a "smallest" variable, but you will need to work out what its initial value ought to be.
  • Code that lot and get it working.
  • Work out how you record the index of the array (probably easiest done at the same time as working out the smallest).
  • Code that lot and get it working.
  • Put the smallest item into your queue
  • Work out what "remove the item" means. Does it mean to create an array one element smaller and copy the items into the new array?
  • Draw a diagram of what that means, then code it and get it working.
  • Repeat until your array is empty
  • Empty your queue in order into a new array.Eureka!
  • Notice you want to do this task in tiny bits. "How do you eat an elephant? One mouthful at a time."
    Garrett Rowe
    Ranch Hand

    Joined: Jan 17, 2006
    Posts: 1296

    I need to implement a method sort ( int []x) to sort the array x using a priority queue.


    The requirement as stated by the OP was to use a priority queue to do the sort. Once you have a correctly implemented priority queue, the sorting process is as easy as continually taking the next item from the top of the queue until the queue is empty. So the next question is, do you (OP) know what a priority queue is? Do you know how to implement one?


    Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 36478
        
      16
    Originally posted by Garrett Rowe:
    The requirement as stated by the OP was to use a priority queue . . .


    That sounds a lot simpler than what I thought. Thank you.
    Manuel Leiria
    Ranch Hand

    Joined: Jul 13, 2007
    Posts: 171
    Originally posted by Campbell Ritchie:


    That sounds a lot simpler than what I thought. Thank you.


    Very easy, indeed. Just find out how the PriorityQueue works and you're almost done.
    PriorityQueue
    Nadine Ernest
    Greenhorn

    Joined: Dec 10, 2008
    Posts: 27
    Hi guys, thank youu soo much for your great help!!

    You all have been great!!

    ThANK YOU!!


    I GIVE MY SPECIAL THANKS TO:

    CAMPBELL RITCHIE

    FARAZ MASOOD

    MIKE CURWEN

    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 36478
        
      16
    You're welcome
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: How to compare the elements of the array and remove the smallest item?!
     
    Similar Threads
    Searching an array
    How would i break this down into seperate metods?
    ArrayList
    Find smallest element in arraylist
    I can NOT compile first class below