冒泡排序(Bubble Sort)
一种交换排序,它的基本思想是:两两比较相邻的关键字,如果反序则交换,直到没有反序的记录为止。
原理:重复地走访过要排序的元素列,依次比较两个相邻的元素,如果他们的顺序(如从大到小、首字母从A到Z)错误就把他们交换过来。走访元素的工作是重复地进行直到没有相邻元素需要交换,也就是说该元素已经排序完成。
时间复杂度:O(n^2)
算法稳定性:相同元素的前后顺序不会发生改变,所以冒泡排序是一种稳定排序算法。
1 - (void)logArrayFunction { 2 int count = 0; 3 int forcount = 0; 4 NSMutableArray * arr = @[@16,@1,@2,@9,@7,@12,@5,@3,@8,@13,@10].mutableCopy; 5 6 for (int i = 0; i < arr.count; i++) { 7 forcount++; 8 // 依次定位左边的 9 for (int j = (int)arr.count-2; j >= i; j--) { 10 count++; 11 if ([arr[j] intValue]< [arr[j+1] intValue]) { 12 [arr exchangeObjectAtIndex:j withObjectAtIndex:j+1]; 13 } 14 } 15 [self logArr:arr]; 16 } 17 NSLog(@"循环次数:%d",forcount); 18 NSLog(@"共%d次比较",count); 19 } 20 //打印数组 21 - (void)logArr:(NSMutableArray * )array { 22 NSString * str = @""; 23 for (NSNumber * value in array) { 24 str = [str stringByAppendingString:[NSString stringWithFormat:@"%zd ",[value integerValue]]]; 25 } 26 NSLog(@"%@",str); 27 } 28 29 //打印如下 30 16 13 1 2 9 7 12 5 3 8 10 31 16 13 12 1 2 9 7 10 5 3 8 32 16 13 12 10 1 2 9 7 8 5 3 33 16 13 12 10 9 1 2 8 7 5 3 34 16 13 12 10 9 8 1 2 7 5 3 35 16 13 12 10 9 8 7 1 2 5 3 36 16 13 12 10 9 8 7 5 1 2 3 37 16 13 12 10 9 8 7 5 3 1 2 38 16 13 12 10 9 8 7 5 3 2 1 39 16 13 12 10 9 8 7 5 3 2 1 40 16 13 12 10 9 8 7 5 3 2 1 41 循环次数:11 42 共55次比较
相关文案:https://www.jianshu.com/p/e3294692966e
选择排序
原理:它的工作原理是每一次从待排序的数据元素中选出最小(或最大)的一个元素,存放在序列的起始位置,然后,再从剩余未排序元素中继续寻找最小(大)元素,然后放到已排序序列的末尾。以此类推,直到全部待排序的数据元素排完。
时间复杂度:选择排序的交换操作介于 0 和 (n - 1) 次之间。选择排序的比较操作为 n (n - 1) / 2 次之间。选择排序的赋值操作介于 0 和 3 (n - 1) 次之间
但是它是一种不稳定算法,相同元素的前后顺序有可能发生改变。
1 //选择排序 2 //每次把最大的选出来 放到前面,然后依次类推 3 -(NSArray*)selectSort:(NSArray*)array 4 { 5 if (!array || array.count == 1) 6 { 7 return array; 8 } 9 NSMutableArray *sortResult = [NSMutableArray arrayWithArray:array]; 10 NSInteger flag; 11 NSInteger execCount = 0;//交换次数 12 NSInteger forCount = 0;//for循环总共执行了多少次 13 for (NSInteger i = 0; i < sortResult.count; i++) 14 { 15 flag = i; 16 forCount++; 17 for (NSInteger j = i; j < sortResult.count - 1; j++) 18 { 19 if (sortResult[j + 1] > sortResult[flag]) 20 { 21 flag = j + 1; 22 } 23 forCount++; 24 } 25 if (i != flag) 26 { 27 [sortResult exchangeObjectAtIndex:i withObjectAtIndex:flag]; 28 execCount++; 29 [self displayArray:sortResult]; 30 } 31 } 32 NSLog(@"交换共执行了%ld次", execCount); 33 NSLog(@"for循环共执行了%ld次", forCount); 34 return sortResult; 35 } 36 -(void)displayArray:(NSArray*)array 37 { 38 NSMutableString *displayString = [NSMutableString stringWithString:@"["]; 39 for (NSInteger i = 0; i < array.count; i++) 40 { 41 i == array.count - 1 ? [displayString appendString:[NSString stringWithFormat:@"%ld", (long)[array[i] integerValue]]] :[displayString appendString:[NSString stringWithFormat:@"%ld%@", (long)[array[i] integerValue], @","]]; 42 } 43 [displayString appendString:@"]"]; 44 NSLog(@"%@", displayString); 45 } 46 47 执行结果如下: 48 [101,14,1,38,26,9,4,7] 49 [101,38,1,14,26,9,4,7] 50 [101,38,26,14,1,9,4,7] 51 [101,38,26,14,9,1,4,7] 52 [101,38,26,14,9,7,4,1] 53 交换共执行了5次 54 for循环共执行了36次