您的位置:首页 > 移动开发 > Objective-C

冒泡排序的两个例子(objective-c)

2015-12-17 20:03 435 查看
mian.m

#import <Foundation/Foundation.h>
#import "NSArray+Sort.h"
#import "Student.h"
int main(int argc,
const char * argv[]) {
@autoreleasepool {

NSArray *array=[[NSArray
alloc]init];
array=@[@"abc",@"123",@"listen"];
NSLog(@"排序前是:%@",array);
NSArray *sortedArray=[array
sortedArrayUsingMyBlock:^NSComparisonResult(id obj1,
id obj2) {//定义数组
按照NSArray+Sort中for循环i和j的值所值的地址里的进行比较数组array里的值
数组里面包含调用方法执行之后返回的三种返回值 NSOrderedDescending(升序) NSOrderedSame
(相等) NSOrderedAscending(降序)
return [obj1
compare:obj2];//返回值是obj1和obj2比较之后的值有三种值
}];

NSLog(@"排序后:%@",sortedArray);

Student *st=[[Student
alloc]initWithName:@"tingyuxuan"
andAge:12];
Student *st1=[[Student
alloc]initWithName:@"gengbiao"
andAge:16];
Student *st2=[[Student
alloc]initWithName:@"xiaoxiao"
andAge:13];
Student *st3=[[Student
alloc]initWithName:@"fenmengwulei"
andAge:19];

NSArray *array2=@[st1,st2,st3,st];//定义一个数组
并且将定义好的STUDENT放进去
NSComparator com=^NSComparisonResult(id obj1 ,id
obj2){//调用NSComparator类型定义
并将STUDENT类型传进去
Student *stuobj1=(Student*)obj1;//将定义好的STUDENT按照顺序放入到stuobj1和stuobj2中
Student *stuobj2=(Student*)obj2;
if (stuobj1.age>stuobj2.age) {//让STUDENT类型的stuobj1和stuobj2中的年龄进行比较
比较过后按照对应的条件返回对应的值
return
NSOrderedDescending;//降序
}
else
if(stuobj1.age==stuobj2.age)
{
return
NSOrderedSame;
}
else
{
return
NSOrderedAscending;//升序
如果NSOrderedAscending和NSOrderedDescending反过来的话可以进行反向排序
}
};

NSArray *sortedStuArray=[array2
sortedArrayUsingMyBlock:com];//调用sortedArrayUsingMyBlock返回值是com中比较过后返回的值按照sortedArrayUsingMyBlock里面的方法定义返回
并将返回的值放入到sortedStuArray数组中打印
NSLog(@"排序后:%@",sortedStuArray);

}
return 0;
}

student.h

#import <Foundation/Foundation.h>

@interface Student :
NSObject
@property (nonatomic,strong)NSString*name;
@property(nonatomic)int age;

-(id)initWithName:(NSString*)name andAge:(int)age;
@end

student.m

#import "Student.h"

@implementation Student
-(id)initWithName:(NSString *)name andAge:(int)age
{
if (self=[super
init]) {
_name=name;
_age=age;
}
return
self;
}
-(NSString*)description
{
NSString *dect=[NSString
stringWithFormat:@"name=:%@ age=%d",_name,_age];
return dect;
}
@end
NSArray+Sort.h

#import <Foundation/Foundation.h>

@interface NSArray (Sort)
-(NSArray*)sortedArrayUsingMyBlock:(NSComparator)comparator;
@end
NSArray+Sort.m

#import "NSArray+Sort.h"

@implementation NSArray (Sort)
-(NSArray*)sortedArrayUsingMyBlock:(NSComparator)comparator
{
//1. 创建一个可变数组
NSMutableArray *array=[self
mutableCopy];

//2.冒泡排序
for (int i=0; i<[self
count]-1; i++) {//
for (int j=0; j<[self
count]-1-i;j++ ) {
if (comparator([array
objectAtIndex:j],[array objectAtIndex:j+1])==NSOrderedDescending) {//用当前传进来的参数
(代码块变量comparator)
判断此代码块 的返回值
如果是降序 则交换两个对象
升序则不动 方法执行之后返回的值进行比较
判断
[array exchangeObjectAtIndex:j
withObjectAtIndex:j+1];
}
}
}
return (NSArray*)array;
}
@end

打印结果是

排序前是:(
abc,
123,
listen
)
排序后:(
123,
abc,
listen
)
排序后:(
"name=:tingyuxuan age=12",
"name=:xiaoxiao age=13",
"name=:gengbiao age=16",
"name=:fenmengwulei age=19"
)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: