码迷,mamicode.com
首页 > 编程语言 > 详细

插入排序

时间:2018-08-14 14:30:41      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:objects   nsnumber   algo   tar   log   tin   intvalue   count   order   

- (void)insertSort
{
    NSMutableArray *dataArray = [NSMutableArray arrayWithObjects:@3,@4,@6,@2,@9,@7,@8, nil];
    for (int i = 0; i<dataArray.count-1; i++) {
        NSInteger j = i+1;
        while (j>=1 && [dataArray[j-1]integerValue]<[dataArray[j]integerValue]) {
            id obj = dataArray[j];
            [dataArray replaceObjectAtIndex:j withObject:dataArray[j-1]];
            [dataArray replaceObjectAtIndex:j-1 withObject:obj];
            j--;
            [AlgorithmSort printArray:dataArray];
        }
    }
    [AlgorithmSort printArray:dataArray];
    
}

- (void)kInserSort:(NSMutableArray *)array{
    array = [NSMutableArray arrayWithObjects:@3,@4,@6,@2,@9,@7,@8, nil];
    for (int i = 0; i < array.count; i++) {
        NSNumber *temp = array[i];
        int j = i-1;
        while (j >= 0 && [array[j] compare:temp] == NSOrderedDescending) {
            [array replaceObjectAtIndex:j+1 withObject:array[j]];
            j--;
        }
        [array replaceObjectAtIndex:j+1 withObject:temp];
        NSLog(@"插入排序排序中:%@",array);
    }
}



+ (void)printArray:(NSArray *)array
{
    for(NSNumber *number in array) {
        printf("%d ",[number intValue]);
    }
    
    printf("\n");
}

插入排序

标签:objects   nsnumber   algo   tar   log   tin   intvalue   count   order   

原文地址:https://www.cnblogs.com/guligei/p/9473490.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!