【发布时间】:2017-01-07 03:33:27
【问题描述】:
当我尝试对一个包含十万个整数的数组进行排序时收到一条错误消息(我已经测试过排序,它适用于五万个整数):
警告:无法加载任何 Objective-C 类信息。这将显着降低可用类型信息的质量。
我也有迭代的比较方法,出现同样的错误
为什么会出现此错误消息?
是否可以对该列表进行排序,如果可以,如何解决?
-(void) quickSort{
return [self quickSort:0 pivot:[self.toSort count]-1 right: [self.toSort count]-1];
}
-(void) quickSort:(int) left pivot:(int) pivot right:(int) right {
if(left < right){
pivot = [self compareToPivot:[[self.toSort objectAtIndex:right] intValue] start:left finish:right position:left];
[self quickSort:pivot+1 pivot:pivot right:right];
[self quickSort:left pivot:pivot right:pivot-1];
}
}
-(int) compareToPivot:(int) pivot start:(int)start finish:(int)finish position:(int)pos{
if(pos == finish){
[self switchNums:start second:finish];
return start;
}
if([[self.toSort objectAtIndex:pos] intValue] <= pivot){
[self switchNums:pos second:start];
return [self compareToPivot:pivot start:start+1 finish:finish position:pos+1];
}
return [self compareToPivot:pivot start:start finish:finish position:pos+1];
}
-(void) switchNums:(int)first second:(int)second{
id temp = [self.toSort objectAtIndex:second];
[self.toSort replaceObjectAtIndex:second withObject:[self.toSort objectAtIndex:first]];
[self.toSort replaceObjectAtIndex:first withObject:temp];
}
迭代比较,运行良好:
-(int) compareToPivot:(int) pivot start:(int)start finish:(int)finish position:(int)pos{
while(pos != finish){
if([[self.toSort objectAtIndex:pos] intValue] <= pivot){
[self switchNums:pos second:start];
start+=1;
}
pos++;
}
[self switchNums:start second:finish];
return start;
}
【问题讨论】:
-
您几乎肯定会在这里看到堆栈溢出——这递归太深并导致不相关的内存损坏。您能否也发布您的迭代版本,以便我们可以看到它在做什么?另见*.com/questions/31431287/…
标签: objective-c sorting