iOS冒泡、选择排序算法

来源:互联网 发布:数控机床的数据参数 编辑:程序博客网 时间:2024/06/05 07:58

冒泡

NSMutableArray * array = [NSMutableArray arrayWithObjects:@2,@1,@4,@3,@6,@10,@5,@9, nil];    for (int i = 0; i < array.count; i++)    {        for (int j = 0; j < array.count - 1; j++)        {            if (array[i] > array[j])            {                [array exchangeObjectAtIndex:i withObjectAtIndex:j];            }        }    }    NSLog(@"%@",array);

选择

NSMutableArray * array = [NSMutableArray arrayWithObjects:@2,@1,@4,@3,@6,@10,@5,@9, nil];    for (int i = 0; i < array.count; i++)    {        for (int j = i+1; j < array.count; j++)        {            if (array[i] > array[j])            {                [array exchangeObjectAtIndex:i withObjectAtIndex:j];            }        }    }    NSLog(@"%@",array);


原创粉丝点击