好尴尬,现在还在啃冒泡排序
- (void)sortArray{
NSMutableArray *sortArray = [NSMutableArray arrayWithArray:@[@(10), @(3), @(7), @(2), @(8), @(4), @(11), @(4), @(9)]];
NSLog(@"排序前:%@", sortArray);
for (int i = 0; i < sortArray.count-1; i ++) {
for (int j = 0; j < sortArray.count-1-i; j ++) {
NSInteger left = [sortArray[j] integerValue];
NSInteger right = [sortArray[j+1] integerValue];
if (left>right) {
[sortArray exchangeObjectAtIndex:j withObjectAtIndex:j+1];
}
NSLog(@"\ni=%d\tj=%d\n%@", i, j, sortArray);
}
}
NSLog(@"排序后:%@", sortArray);
}
网友评论