初学算法,大学里学的东西都忘的一干二净,有什么不妥的地方以后再修改,先上代码
// 选择排序+ (void)sectionSort:(NSMutableArray *)arr{ for (NSInteger i = 0; i < arr.count; i ++) { NSInteger minIndex = i; for (NSInteger j = i + 1; j < arr.count; j ++) { if (arr[j] < arr[minIndex]) { minIndex = j; } } [arr exchangeObjectAtIndex:i withObjectAtIndex:minIndex]; }}// 插入排序+ (void)insertionSort:(NSMutableArray *)arr{ for (NSInteger i = 1; i < arr.count; i ++) { for (NSInteger j = i ; j > 0; j --) { if (arr[j] < arr[j - 1]) { [arr exchangeObjectAtIndex:j withObjectAtIndex:j - 1]; }else { break; } } }}
新闻热点
疑难解答