Jump to content

sorting numbers


aebstract

Recommended Posts

I have an array of numbers, something like:

 

1 > 5

2 > 10

3 > -2

4 > -6

5 > 3

 

I need to sort this array so that it is in numerical order IGNORING the negative signs. A correct ouput would be this order:

 

-2

3

5

-6

10

 

Right now I am having to basically duplicate my array and then use str_replace to remove the - and then sort the list keeping my keys in tact with the values, that way I can go back and compare to my original array. I could use one array and do away with all of the extra if I knew a way to do this sort.

Thanks in advance!

Link to comment
Share on other sites

Is this problem that singular in that the array simply contains these numbers?  If so, why even store them as negatives?

 

It doesn't sound like it since you say you want to "go back" and compare to the original array.  What are you comparing - all positives to something that may be negative?  Or just the index value?  OR is there other data in this array in which case sorting it in the first place is entirely more complex and would probably be solved with a user-defined sort.  Have you looked at those functions in the Manual?

Link to comment
Share on other sites

Is this problem that singular in that the array simply contains these numbers?  If so, why even store them as negatives?

 

It doesn't sound like it since you say you want to "go back" and compare to the original array.  What are you comparing - all positives to something that may be negative?  Or just the index value?  OR is there other data in this array in which case sorting it in the first place is entirely more complex and would probably be solved with a user-defined sort.  Have you looked at those functions in the Manual?

 

An output of the array would look identical to what I showed as an example. Basically trying to find the closest value to 0 and if the closest value is the same positive and negative like -2 and 2, I need to output the positive.

Link to comment
Share on other sites

How are you coming up with this array?  Is it the result of some loop that you already have?  If so, then while going thru that data simply test each absolute value against a saved 'lowest value' that you have already seen and if it is lower, replace that saved 'lowest value'.

 

 

$lowest = 999999;
(your loop)
{
...
if ( (abs(some_newval) < $lowest)
   $lowest = abs(some_newval);
...
}
Link to comment
Share on other sites

<?php
fscanf(STDIN,"%d",$n);
$t = explode(" ", stream_get_line(STDIN, 256 + 1, "\n"));

?>

 

This is how I get my original array, I don't have access to the source numbers beyond this. I'll play around with what you posted and see if I can figure something out.

Link to comment
Share on other sites

This sounds like school work...

 

This code can't be correct.  Where is the return value of fscanf being assigned?

fscanf is one of those nice C-style functions that puts its "return values" into the arguments by-ref. Check the docs.

 

But the code is weird. Why the first fscanf? If this is homework then my guess is that you're supposed to be getting the number of numbers to read from stdin, in which case you're supposed to use that rather than just pull in all numbers with stream reading. Otherwise you're just throwing away $n?

 

And are the numbers really all on one line? That's unusual. Sure it's not one per line? Again, if this is homework then you're probably supposed to use $n to set up a loop that reads numbers (one at a time) and put them into an array.

 

And if this is homework, we can help you (just not give you the solution, or too much code) but it would be really great if you told us the instructions. It could be that the sorting is a requirement, even though what ginerjm said earlier about not using sorting is a better solution.

 

tldr: more information please

Link to comment
Share on other sites

This sounds like school work...

 

fscanf is one of those nice C-style functions that puts its "return values" into the arguments by-ref. Check the docs.

 

But the code is weird. Why the first fscanf? If this is homework then my guess is that you're supposed to be getting the number of numbers to read from stdin, in which case you're supposed to use that rather than just pull in all numbers with stream reading. Otherwise you're just throwing away $n?

 

And are the numbers really all on one line? That's unusual. Sure it's not one per line? Again, if this is homework then you're probably supposed to use $n to set up a loop that reads numbers (one at a time) and put them into an array.

 

And if this is homework, we can help you (just not give you the solution, or too much code) but it would be really great if you told us the instructions. It could be that the sorting is a requirement, even though what ginerjm said earlier about not using sorting is a better solution.

 

tldr: more information please

 

 

It's not homework. It's just for a game online, for fun. It does give you a list of numbers in a row, and $n is the number of numbers that are in that row. I've solved the game through the code I have, but now I'm trying to shorten it as much as possible and use better methods to accomplish the task. It's just a fun learning thing, and can be done in any way you choose. The code I provided was the way I solved it, but I know there are other possibilities and better options which is why I'm looking for help on some of it. :)

I don't really know why I would need $n, unless like you said to go through that many iterations instead of pulling in all numbers. I know it mentions in the instructions something about it pulling in all numbers and that you can change it. I have tried to look in to the STDIN and those functions being used (they were provided to begin the game basically) but haven't found anything that really makes me understand how it's working. I tried the fscan code and it makes my code fail every test case, even though I'm not calling $n anywhere in my code. So I'm kinda lost as to what fscan is really doing I guess.

 

Here is the entire game:

 

 

Rules

Write a program that prints the temperature closest to 0 among input data. If two numbers are equally close to zero, positive integer has to be considered closest to zero (for instance, if the temperatures are -5 and 5, then display 5).

 

Game Input

Your program must read the data from the standard input and write the result on the standard output.
 
Input

Line 1: N, the number of temperatures to analyze

Line 2: The N temperatures expressed as integers ranging from -273 to 5526

 

Output
Display 0 (zero) if no temperatures are provided. Otherwise, display the temperature closest to 0.
Link to comment
Share on other sites

Homework, game, same difference.

 

What I said about reading $n values instead of the entire line is still true - it's likely what they intend for you to do. However it doesn't seem to be a requirement, which means you probably can just throw away the value. However they do not say that the integers are all space-separated...

 

They don't say anything about arrays (who even knows how they're grading it?) so ginerjm's method is best because of efficiency: sorting requires more operations than simply iterating through the list.

 

So,

1. Grab the first number and set that to be the current minimum

2. Loop through the remaining numbers. For each one,

3. If it is closer to zero than the current minimum (including checking the "equally close" clause) then update the current minimum

4. After the loop, print the value

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.