zoukankan      html  css  js  c++  java
  • iOS(OC)中的冒泡排序

    NSMutableArray *array = [NSMutableArray arrayWithObjects:@"12",@"84", @"35", @"70", @"85", @"99", nil];
            NSInteger count = [array count];
            for (int i = 0; i < count; i++) {
                for (int j = 0; j < count - i - 1; j++) {
                   // if ([[array objectAtIndex:j] intValue] > [[array objectAtIndex:(j + 1)] intValue]) {   //这里在用[array objectAtIndex:j]时候必须intValue
    //                if([[array objectAtIndex:j] compare:[array objectAtIndex:j + 1]] == -1){  //这里整体必须有一个返回值,-1,0,1,因为compare的返回值NSComparisonResult是一个枚举类型的值,所以要返回一个值
                     
                    if([[array objectAtIndex:j] compare:[array objectAtIndex:j + 1] options:NSNumericSearch] == 1){  //同上potions  NSNumericSearch = 64,
                        [array exchangeObjectAtIndex:j withObjectAtIndex:(j + 1)];  //这里可以用exchangeObjectAtIndex:方法来交换两个位置的数组元素。
                    }
                }
            }
            for (NSString *i in array) {
                NSLog(@"%@", i);
            }
             
             
             
            NSMutableArray *array1 = [NSMutableArray arrayWithObjects:@"12",@"84", @"35", @"70", @"85", @"99", nil];
            [array1 sortUsingSelector:@selector(compare:)];
            NSLog(@"%@", array);
             
    
  • 相关阅读:
    apache httpd配置问题
    php文件上传进度控制模块
    “fixed” css style in mobile safari
    第八周进度表
    大白鱼团队项目计划表
    第七周进度表
    结对开发团队成员以及题目介绍
    软件工程结对作业01
    软件工程子数组求和1
    软件工程子数组求和2
  • 原文地址:https://www.cnblogs.com/ios988/p/5766201.html
Copyright © 2011-2022 走看看