以前没怎么用oc中写过算法,在首次在oc中写冒泡就踩到雷了
1. NSArray 保存的是对象
2.NSArray 是不可变的
/**
* 对数组安从大到小的顺序排序
*/
-(void)sortArrayNum
{
NSArray *array = [NSArray arrayWithObjects:@12,@34,@7,@56,@8,@1, nil];
NSMutableArray *mutableArray = [array mutableCopy];
for (int i = 0; i<mutableArray.count - 1; i++) {
for (int j = i+1; j<mutableArray.count; j++) {
if ([self isMaxNum:[mutableArray objectAtIndex:i] Num2:[mutableArray objectAtIndex:j]]) {
[mutableArray exchangeObjectAtIndex:i withObjectAtIndex:j];
}
}
}
NSLog(@"%@",mutableArray);
}
/**
* 比较两个NSNumber的大小
*/
-(BOOL)isMaxNum:(NSNumber *)num1 Num2:(NSNumber *)num2
{
int n1 = [num1 intValue];
int n2 = [num2 intValue];
return n1>n2;
}