SMutableArray * muArr =[NSMutableArray arrayWithArray:@[@(12),@(34),@(97),@(456),@(21)]];
[self bubbleSort:muArr];
[self selectSort:muArr];
//冒泡从大到小排序
- (void)bubbleSort:(NSMutableArray *)array{
for (int i = 0; i< array.count-1; i++) {
for (int j= 0; j < array.count - 1 - i; j++) {
if ([array[j] intValue] < [array[j+1] intValue]) {
[array exchangeObjectAtIndex:j withObjectAtIndex:j+1];
}
}
}
NSLog(@"%@",array);
}
//选择排序
- (void)selectSort:(NSMutableArray *)array{
for (int i = 0; i<array.count - 1; i++) {
for (int j = 1; j< array.count - 1 - i; j++) {
if ([array[i] intValue] < [array[j] intValue]) {
[array exchangeObjectAtIndex:i withObjectAtIndex:j];
}
}
}
NSLog(@"%@",array);
}
网友评论