Looking for a number in a group (array) - objective-c

Is there a way to perform a group conditional statement?
For example, I have a process which returns random numbers from an array (x) which I want to compare to the first 100 prime numbers.
Is there a function or method available by which I could do:
if (x in first_100_prime_numbers)
...

As Almo already said in a comment, many collection classes in Cocoa have a containsObject: method that you can use for this purpose. Note that performing containsObject: on an array will probably be much slower (especially when the array is large) than on a set or hash table as the method has to iterate over every item in the array until it finds a match.

Related

What would be the binary search complexity to find second largest number in array

Can someone explain how to calculate the binary search complexity to find second largest number in array.
Binary search is done on a sorted array.
If you already have a sorted array, why do you need to do anything at all?
The second to last number in the array (sorted in ascending order) would be the second largest number.(O(1))
If the array contains duplicates:
For example,
{0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,... }
The time complexity would be O(log n) where n is the number of elements in the array.
The smallest number is the one at index 0 (call it x), now you can use binary search to find the array bounds within which all elements are equal to x. The immediate neighbour outside these bounds would be the second largest number in the array.
If you are using C++, you can use this method to get the upper_bound.
Binary search for an element with any given property is always logarithmic, provided that you can determine in constant time whether that property holds.
If the array can’t contain duplicates, you don’t need a binary search and the complexity is constant.
One way to do it in python efficiently can be to convert list[which allows duplicates] to set[which does not allow duplicates] almost in O(1) time and then fetching item at index[-2] again in O(1) time, assuming that as it is binary search list would be sorted in ascending order.

Is there any built in method for sorting in Objective-c?

I have two sorted NSMutableArrays (or I can use any other collection, not critical), I need to insert objects from the first array to the second and preserve sort order in the second array. What is the optimal (fastest) method to do that? I can implement all the known good algorithms, but my question is, if there is already some built-in method? If not, what is the best algorithm in my case?
The real answer would be: it depends, since you are asking: what is the fastest way of inserting objects from one array into another while preserving sort order.
There is no built in way of inserting in the right place of a sorted array. You can achieve the same effect by just adding the two arrays together but it won't be "the fastest way".
What is actually faster depends on many things like: how much data does the arrays contain, what is the ratio of data in array1 vs array2 (does one array contain much more data than the other)?, etc.
NOTE: You should probably begin with the simple solution and only optimize once you experience performance problems. Do measurements with a large data set though, to see that your solution works with whatever data your users may have.
Inserting items from one sorted array into another sorted array
If you want to merge the two arrays by inserting objects in the right place then normal algorithms apply. You should insert the smaller array into the bigger array and try to insert entire sorted sequences where possible instead of every item one by one.
For best performance you should try to make a batch insert using insertObjects:atIndexes: instead of inserting the object one by one.
You can use indexOfObject:inSortedRange:options:usingComparator: to find the index that each item should be inserted in the other array if you specify NSBinarySearchingInsertionIndex for the options. Also, the comparator you are using must be the same as the comparator that sorted the array, otherwise the result is "undefined".
With this in mind you would do something like this
Create mutable index
For every ITEM in SMALLER ARRAY
Find the index where to insert ITEM in LONGER ARRAY
Add (the insertion location + the location in the short array) as the index in the mutable set.
Next item.
Batch insert all items.
The documentation for insertObjects:atIndexes: tells you that "the corresponding location specified in indexes after earlier insertions have been made." Which in your case with two sorted array mean all items with a lower index will already have been added and thus you should add the index of the object in the short array to the value returned from indexOfObject:inSortedRange:options:usingComparator:.
Another (probably very premature optimization) you can do is decrease the sortedRange for every item in the loop so that you don't have to search through parts of the array that you know the item to be inserted is bigger than.
There are probably many other optimizations that can be made. You should still measure first! Hopefully this will get you started.
NSArray *newArray=[firstArray arrayByAddingObjectsFromArray:secondArray];
newArray = [newArray sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)];
I would start by simply adding all of the objects of the first array to the second and then resorting the second. Time how long it takes. If it is acceptable, stop there.
If not, you could try a binary search to find the insertion point in the second array for each item in the first array. Since both arrays are sorted, you might be able to optimise the search by using the last insertion point as the lower bound each time round. Something like this:
NSInteger insertionPoint = -1;
for (id object in array1)
{
insertionPoint = [self binarySearch: array2 for: object lowerBound: insertionPoint + 1];
[array2 insertObject: object atIndex: insertionPoint];
}
The Cocoa class NSSortDescriptor together with sortedArrayUsingDescriptors: from NSArray should do what you are after.
Since you are using mutable arrays, you might want to use sortUsingDescriptors: which sorts the mutable array without creating a new one.
Look at the documentation here to see if any of the NSArray sort methods work for you. http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/NSArray.html. You can scroll down to the methods and there's 7 built-in ones for sorting. You could probably just combine the two arrays and run the sortedArrayUsingComparator: or one of the other methods.

Shuffling two NSMutableArrays in a predictable manner

I have two NSMutableArrays of strings that I wish to combine and shuffle together. However, I want it so that every time it shuffles the original two arrays, it always yields the same array in the same order. How can this be done?
Could I seed some constant value into the random function like in C++ srand(constant) but in Objective-C?
Edit: Or say I just have one array (not sure if this makes any difference) and I wanted to shuffle that one array in a predictable manner?
The way to do it is just to do srandom(constant) and use the random() function in Objective C. This way you'll always get the same shuffle and random values.

Order Integers In Ascending and Descending Orders

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.
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
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:
Sorting Algorithms in Wikipedia.
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.
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/
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.
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.

How to return objects in an NSArray at specified indexes?

Example:
I have an NSArray with 40 objects. What is the most efficient way to return ONLY those objects with index equal to a number specified in another NSArray (e.g {0, 27, 36} for example)?
Can a predicate be used here? Or is there a more simple and efficient approach?
Why don't you just iterate over the index array and look up each index in the data array replacing the index with the looked-up object.
In the end, the array that held the indices now holds the objects. If you don't want to wipe out your index array, then just create a new array of the same size as the index array and put the objects there.
You might be over-thinking the design of this and falling prey to micro-optimization. (Which is a bad thing.)
There is a method (objectsAtIndexes) for returning specified indexes from an original array, but it requires an NSIndexSet as its argument, and there isn't a built-in way to convert your array of indices to an index set. On the plus side, you can generate the index set with a short loop, as follows:
NSMutableIndexSet indexes = [NSMutableIndexSet indexSet];
for (NSNumber * number in indexArray)
{
[indexes addIndex:[number intValue]];
}
return [originalArray objectsAtIndexes:indexes];
Is this any more efficient than simply looping through the original array? I have no idea. You would have to profile your app to be sure.
I remember reading one time, and I cant say where but it stuck in my mind, that if this was something you need to do many times and the array is fairly large that there is a more efficient way. You could create a new dictionary from that array where each entry has the key of the index number and the value is the array item. You only create the dictionary once so the hard work is over in one shot. Then every time you need to access values at certain indexes you just ask for valueForKey:#"indexNumber". But I never had a need for this so I never tested if it was more efficient in the long run... it's just a thought.