Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

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

Nadine Ernest
Greenhorn
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
Posts: 33
you gotta start somewhere, what do you have so far?

Nadine Ernest
Greenhorn
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
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
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
Posts: 171
And take a look at Sorting algorithms

Ove LindstrÃ¶m
Ranch Hand
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
Posts: 50714
83
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
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?

Campbell Ritchie
Sheriff
Posts: 50714
83
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
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
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
Posts: 50714
83
You're welcome