首页 > 学院 > 开发设计 > 正文

排序

2019-11-06 09:03:18
字体:
来源:转载
供稿:网友

初学算法,大学里学的东西都忘的一干二净,有什么不妥的地方以后再修改,先上代码

// 选择排序+ (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;            }        }    }}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表