当前位置:首页 > 操作系统 > Ios

IOS 快速排序法

- (NSMutableArray *)QuickSort:(NSMutableArray *)list StartIndex:(NSInteger)startIndex EndIndex:(NSInteger)endIndex
{
    if(startIndex < endIndex)
    {
        NSString * temp = [list objectAtIndex:startIndex];
        NSInteger tempIndex = startIndex; //临时索引 处理交换位置(即下一个交换的对象的位置)for(int i = (int)startIndex  ; i <= endIndex ; i++)
        {
            NSString *temp1 = [list objectAtIndex:i];
            if([temp intValue] > [temp1 intValue]){
                tempIndex = tempIndex + 1;
                [list exchangeObjectAtIndex:tempIndex withObjectAtIndex:i];
            }
        }
        
        [list exchangeObjectAtIndex:tempIndex withObjectAtIndex:startIndex];
        [self QuickSort:list StartIndex:startIndex EndIndex:tempIndex-1];
        [self QuickSort:list StartIndex:tempIndex+1 EndIndex:endIndex];

    }
    
    return list;
    
}

原文:http://www.cnblogs.com/joesen/p/3591966.html


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!