iOS开发中的快速排序

快速排序:

快速排序是对冒泡排序的一种改进。

基本思想:

通过一趟排序将数据分割成两部分,其中一部分的所有数据都比另一部分的所有数据都小,但是两部分数据是无序的。然后再对两部分的数据分别进行第一趟的排序,直到最后的数据是有序的。
排序步骤:

1.选择所有数据中的第一个数据作为一个比较的标准。(左侧数据下标i 右侧数据下标j。最开始i = 0,j = 数据个数-1)

2.从数据的最右端开始找比这个标准数据小的一个数据(j–),找到后,将其赋值给第i个数据。(为了让左侧数据都小于这个比较的数据)

3.从数据的最左侧开始找比这个标准数据大的一个数据(i ++),找到后,将其赋值给第j个数据。(为了让右侧数据都大于这个比较的数据)

4.直到i和j相等,然后再分别对左右侧数据进行第3、4步的比较。最终得到的数据是一组递增有序数据。
代码:
- (void)quickSortArr:(NSMutableArray *)mutableArr withLowIndex:(NSInteger)lowIndex andHighIndex:(NSInteger)highIndex{

NSLog(@"lowIndex:%lu---highIndex:%lu", lowIndex, highIndex);
if (lowIndex >= highIndex) {
    return;
}
NSInteger i = lowIndex;
NSInteger j = highIndex;
NSInteger key = [mutableArr[i] integerValue];

while (i < j) {
    while (i < j && [mutableArr[j] integerValue] >= key) {  // 从后找比key小的
        j --;
    }
    mutableArr[i] = mutableArr[j];

    while (i < j && [mutableArr[i] integerValue] <= key) {  // 从前找比key大的
        i ++;
    }
    mutableArr[j] = mutableArr[i];
}
// 直到 i = j一次排序结束
mutableArr[j] = @(key);  // mutableArr[i] = @(key);

[self quickSortArr:mutableArr withLowIndex:lowIndex andHighIndex:i - 1];
[self quickSortArr:mutableArr withLowIndex:i + 1 andHighIndex:highIndex];

NSLog(@"%@", mutableArr);

}

本篇文章到这里就结束了,愿大家加班不多工资多,男同胞都有女朋友,女同胞都有男朋友。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在iOS开发获取图片二维码的定位,可以使用CoreImage框架。具体步骤如下: 1. 使用CIImage加载图片: ``` CIImage *image = [CIImage imageWithCGImage:image.CGImage]; ``` 2. 创建CIDetector并设置识别类型: ``` CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeQRCode context:nil options:@{CIDetectorAccuracy:CIDetectorAccuracyHigh}]; ``` 3. 识别二维码: ``` NSArray *features = [detector featuresInImage:image]; ``` 4. 遍历识别结果,获取二维码定位: ``` for (CIQRCodeFeature *feature in features) { NSArray *corners = feature.corners; // 获取定位四个角的坐标 // 在图像上绘制定位 UIGraphicsBeginImageContext(imageSize); CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetStrokeColorWithColor(context, [UIColor greenColor].CGColor); CGContextSetLineWidth(context, 2.0); CGContextMoveToPoint(context, corners[0].x, corners[0].y); CGContextAddLineToPoint(context, corners[1].x, corners[1].y); CGContextAddLineToPoint(context, corners[2].x, corners[2].y); CGContextAddLineToPoint(context, corners[3].x, corners[3].y); CGContextAddLineToPoint(context, corners[0].x, corners[0].y); CGContextStrokePath(context); UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); } ``` 这样就可以在原图上绘制出二维码的定位了。需要注意的是,CIDetector只能识别二维码,如果要识别其他类型的码,需要设置不同的detector类型。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值