快速排序算法
/*******************************快速排序 start**********************************/
//随即取 当前取第一个,首先找到第一个的位置,然后分成left和right两组子集 ,分别对left和right继续执行分割(同上操作)
-(void)QuickSort:(NSMutableArray *)list StartIndex:(NSInteger)startIndex EndIndex:(NSInteger)endIndex{
if(startIndex >= endIndex)return;
NSNumber * temp = [list objectAtIndex:startIndex];
NSInteger tempIndex = startIndex; //临时索引 处理交换位置(即下一个交换的对象的位置)
for(int i = startIndex + 1 ; i <= endIndex ; i++){
NSNumber *t = [list objectAtIndex:i];
if([temp intValue] > [t 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];
}
/*******************************快速排序 end**********************************/
冒泡排序算法
//取第一个 与其邻接的对比,若大则交换
-(void)BubbleSort:(NSMutableArray *)list{
for (int j = 1; j<= [list count]; j++) {
for(int i = 0 ;i < j ; i++){
if(i == [list count]-1)return;
NSInteger a1 = [[list objectAtIndex:i] intValue];
NSInteger a2 = [[list objectAtIndex:i+1] intValue];
if(a1 > a2){
[list exchangeObjectAtIndex:i withObjectAtIndex:i+1];
}
}
}
}
/*******************************冒泡排序 end**********************************/