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

原文网址:http://www.360doc.com/content/14/0508/15/11029609_375813213.shtml


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

<span style="font-size:14px;">NSArray *arr = @[[NSNumber numberWithInt:10],[NSNumber numberWithInt:1],[NSNumber numberWithInt:3],[NSNumber numberWithInt:12],[NSNumber numberWithInt:22],[NSNumber numberWithInt:5],[NSNumber numberWithInt:33]];  
NSMutableArray *oldArr = [[NSMutableArray alloc]initWithArray:arr];  
NSLog(@"排序前:%@",oldArr); </span>


.冒泡排序

- (void)sort:(NSMutableArray *)arr  
{  
    for (int i = 0; i < arr.count; i++) {  
        for (int j = 0; j < arr.count - i - 1;j++) {  
            if ([arr[j+1]integerValue] < [arr[j] integerValue]) {  
                int temp = [arr[j] integerValue];  
                arr[j] = arr[j + 1];  
                arr[j + 1] = [NSNumber numberWithInt:temp];  
            }  
        }  
    }  
    NSLog(@"冒泡排序后:%@",arr);  
} <strong> 
</strong>


.选择排序

- (void)sort:(NSMutableArray *)arr  
{  
    for (int i = 0; i < arr.count; i ++) {  
        for (int j = i + 1; j < arr.count; j ++) {  
            if ([arr[i] integerValue] > [arr[j] integerValue]) {  
                int temp = [arr[i] integerValue];  
                arr[i] = arr[j];  
                arr[j] = [NSNumber numberWithInt:temp];  
            }  
        }  
    }  
      
    NSLog(@"选择排序后:%@",arr);  
}  


.快速排序


- (void)quickSort:(NSMutableArray *)arr leftIndex:(int)left rightIndex:(int)right  
{  
    if (left < right) {  
        int temp = [self getMiddleIndex:arr leftIndex:left rightIndex:right];  
        [self quickSort:arr leftIndex:left rightIndex:temp - 1];  
        [self quickSort:arr leftIndex:temp + 1 rightIndex:right];  
    }  
}  
  
- (int)getMiddleIndex:(NSMutableArray *)arr leftIndex:(int)left rightIndex:(int)right  
{  
    int tempValue = [arr[left] integerValue];  
    while (left < right) {  
        while (left < right && tempValue <= [arr[right] integerValue]) {  
            right --;  
        }  
        if (left < right) {  
            arr[left] = arr[right];  
        }  
          
        while (left < right && [arr[left] integerValue] <= tempValue) {  
            left ++;  
        }  
        if (left < right) {  
            arr[right] = arr[left];  
        }  
    }  
    arr[left] = [NSNumber numberWithInt:tempValue];  
    return left;  
}  



.插入排序

- (void)sort:(NSMutableArray *)arr  
{  
    for (int i = 1; i < arr.count; i ++) {  
        int temp = [arr[i] integerValue];  
          
        for (int j = i - 1; j >= 0 && temp < [arr[j] integerValue]; j --) {  
            arr[j + 1] = arr[j];  
             arr[j] = [NSNumber numberWithInt:temp];  
        }  
         
          
    }  
    NSLog(@"插入排序后:%@",arr);  
}  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值