Recursive partitioning algorithm
Recursive partitioning algorithm
- Subject: Recursive partitioning algorithm
- From: Andrew Merenbach <email@hidden>
- Date: Wed, 18 Jun 2003 11:15:47 -0700
I've produced the following tail-recursive algorithm, to divide an
array into groups of 'size' (the remainder will have their own group):
- (NSArray *)partition1:(unsigned)size {
unsigned count = [self count];
return (count > size && size > 0) ? [[[self
subarrayWithRange:NSMakeRange(size, count - size)] partition1:size]
arrayByAddingObject:[self subarrayWithRange:NSMakeRange(0, size)]] :
[NSArray arrayWithObject:self];
}
in a category of NSArray. Is there any reason to use or not to use
this algorithm? (At least as compared to the following:)
- (NSArray *)partition2:(unsigned)size {
NSMutableArray *array = [NSMutableArray array];
if (size) {
unsigned count = [self count];
unsigned start = 0;
for (start = 0; start < count; start += size) {
if (start + size >= count) size = count - start;
[array addObject:[self
subarrayWithRange:NSMakeRange(start, size)]];
}
}
return (NSArray *)array;
}
I've looked at the program in both GDB and ObjectAlloc, but have not
been able to discern very much about the actual efficiency of each
particular operation.
Take care,
Andrew
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.