用objective-c 实现常用算法(冒泡、选择、快速、插入)

来源:互联网 发布:欢乐麻将辅助软件 编辑:程序博客网 时间:2024/05/14 07:34

下午研究了下用oc实现常用的算法,参考了一些资料后自己用代码检验了下,以下代码均测试可用。其中arr参数是一个可变数组,其中存的是NSNumber类型的数据,具体如下:

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. NSArray *arr = @[[NSNumber numberWithInt:10],[NSNumber numberWithInt:1],[NSNumber numberWithInt:3],[NSNumber numberWithInt:12],[NSNumber numberWithInt:22],[NSNumber numberWithInt:5],[NSNumber numberWithInt:33]];  
  2. NSMutableArray *oldArr = [[NSMutableArray alloc]initWithArray:arr];  
  3. NSLog(@"排序前:%@",oldArr);  


一.冒泡排序

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. - (void)sort:(NSMutableArray *)arr  
  2. {  
  3.     for (int i = 0; i < arr.count; i++) {  
  4.         for (int j = 0; j < arr.count - i - 1;j++) {  
  5.             if ([arr[j+1]integerValue] < [arr[j] integerValue]) {  
  6.                 int temp = [arr[j] integerValue];  
  7.                 arr[j] = arr[j + 1];  
  8.                 arr[j + 1] = [NSNumber numberWithInt:temp];  
  9.             }  
  10.         }  
  11.     }  
  12.     NSLog(@"冒泡排序后:%@",arr);  
  13. }  


二.选择排序

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. - (void)sort:(NSMutableArray *)arr  
  2. {  
  3.     for (int i = 0; i < arr.count; i ++) {  
  4.         for (int j = i + 1; j < arr.count; j ++) {  
  5.             if ([arr[i] integerValue] > [arr[j] integerValue]) {  
  6.                 int temp = [arr[i] integerValue];  
  7.                 arr[i] = arr[j];  
  8.                 arr[j] = [NSNumber numberWithInt:temp];  
  9.             }  
  10.         }  
  11.     }  
  12.       
  13.     NSLog(@"选择排序后:%@",arr);  
  14. }  

三.快速排序

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. - (void)quickSort:(NSMutableArray *)arr leftIndex:(int)left rightIndex:(int)right  
  2. {  
  3.     if (left < right) {  
  4.         int temp = [self getMiddleIndex:arr leftIndex:left rightIndex:right];  
  5.         [self quickSort:arr leftIndex:left rightIndex:temp - 1];  
  6.         [self quickSort:arr leftIndex:temp + 1 rightIndex:right];  
  7.     }  
  8. }  
  9.   
  10. - (int)getMiddleIndex:(NSMutableArray *)arr leftIndex:(int)left rightIndex:(int)right  
  11. {  
  12.     int tempValue = [arr[left] integerValue];  
  13.     while (left < right) {  
  14.         while (left < right && tempValue <= [arr[right] integerValue]) {  
  15.             right --;  
  16.         }  
  17.         if (left < right) {  
  18.             arr[left] = arr[right];  
  19.         }  
  20.           
  21.         while (left < right && [arr[left] integerValue] <= tempValue) {  
  22.             left ++;  
  23.         }  
  24.         if (left < right) {  
  25.             arr[right] = arr[left];  
  26.         }  
  27.     }  
  28.     arr[left] = [NSNumber numberWithInt:tempValue];  
  29.     return left;  
  30. }  

四.插入排序

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. - (void)sort:(NSMutableArray *)arr  
  2. {  
  3.     for (int i = 1; i < arr.count; i ++) {  
  4.         int temp = [arr[i] integerValue];  
  5.           
  6.         for (int j = i - 1; j >= 0 && temp < [arr[j] integerValue]; j --) {  
  7.             arr[j + 1] = arr[j];  
  8.              arr[j] = [NSNumber numberWithInt:temp];  
  9.         }  
  10.          
  11.           
  12.     }  
  13.     NSLog(@"插入排序后:%@",arr);  
  14. }  
0 0