views:

64

answers:

5

I'm working with Objective-C, but probably it doesn't matter the programming language for this. So basically I have an array, with say, the integers 12, 5, and 17, and I want to be able to pull out the largest number, or the smallest, or second smallest, etc.

Basically I want to be able to sort them into ascending or decending order so I could pick out, for instance, the second smallest number by retrieving the objectAtIndex: 1 if it is sorted in ascending order. I feel like this is incredibly obvious but I can't think of how to do it at the moment, so I would love it if someone could enlighten me.

A: 

Just sort the array in ascending order (I don't use objective C, but I am sure there is a function for it) and then get the element wherever you want...

To get the largest

array[array.length - 1]

Second largest

array[array.length -2]

Smallest

array[0]

Second smallest

array[1]

You should check to make sure that the array index is valid:

if (array.length - 2> 0) //Second largest element

    return array[array.length - 2];

Or:

if (array.length > 1) //Second smallest element

    return array[1];

See here for how to sort an array in objective C:

http://howtomakeiphoneapps.com/2009/03/how-to-sort-an-array-in-objective-c/

David Watson
A: 

If you want to preserve the order of the original array, one method is to create a second array that just contains the numbers 0, 1, ... n, representing indexes into the first array. Then sort the second array, but instead of comparing its values, compare the corresponding values that it points to in the first array. (You could also just store pointers and sort based on the dereferenced pointers.)

Then to find the second-largest number, look up the index in the second-to-last position in the second array and see where it points to in the first array.

If you want to get fancy and avoid sorting, this lecture describes an algorithm for finding the k-largest element in linear time. I haven't actually used it, but it looks like it might be a good method if your data changes often, as you wouldn't have to maintain the extra array.

James M.
+1  A: 

Almost every high level language, including objective-c, have library to sort an array. But as you said that language does not matter, probably you are looking for the algorithm itself. There are a number a sorting algorithms with different computational complexity. You can find them in any standard algorithm book. Or these 2 pages might be helpful:

  1. Sorting Algorithms in Wikipedia.
  2. sorting-algorithms.com. Contains nice explanation with animation.

And if you are interested particularly in objective-c, check the Sorting section of NSArray reference. This contains an example to sort an array of integer.

taskinoor
+1  A: 

If you have an NSArray with NSNumber instances, then the sort you are looking for is as easy as this:

NSArray* sortedNumbers = [unorderedNumbers sortedArrayUsingSelector:@selector(intValue)];

It will sort ascending, so [sortedNumbers lastObject] will be the greatest value.

There are many more sorting methods on NSArray if you have more specific needs. NSArray sorting

PeyloW
I tried the code you posted, and it simply didn't work. There weren't any syntax errors or warnings, but nothing happened, the order stayed the same, so I don't know what is going on with that.
Regan
@Regan: Note that `sortedArrayUsingSelector:` do not sort in place. It returns a new array with the sorted values, the original array is left untouched.
PeyloW
Yes, I know, I copied your exact code, but I ended up going with the method described here anyway: http://stackoverflow.com/questions/1844031/how-to-sort-nsmutablearray-using-sortedarrayusingdescriptors
Regan
A: 

If your goal is to get the highest number, or lowest, or second-lowest, or what have you, and you only need one number from the result, then sorting is overkill. Instead you should just iterate over the entire array and keep track of the highest (or lowest, or 2 lowest (for the second-lowest)) number seen so far. If your language supports this, it'll be called a "fold". The only reason to actually sort the array is if you need to access multiple different ranked values from the array.

Kevin Ballard