IOS 排序算法
/**
* @brief 冒泡排序法
*
* @param arr 需要排序的数组
*/
-(void)BubbleSort:(NSMutableArray *)arr
{
// 取第一个与其邻接的对比,若大则交换
for (int i=0; i<[arr count]; i++)
{
for (int j= (int)[arr count]-1; j>i; j--) {
int temp1 = [[arr objectAtIndex:j]intValue];
int temp2 = [[arr objectAtIndex:j-1]intValue];
if (temp1 < temp2) // 从小到大 if (temp1 > temp2) // 从大到小
{
[arr exchangeObjectAtIndex:j withObjectAtIndex:j-1];
}
}
}
NSLog(@"排序后%@",arr);
}

浙公网安备 33010602011771号