IOS学习 NSMutableArray中元素排序
2016-02-29 14:09
627 查看
首先(效率同于冒泡):
int comparecCount =
0;
NSMutableArray *array = [[NSMutableArrayalloc]initWithObjects:@"5",@"9",@"2",@"4",@"8",@"1",@"7",@"3",nil];
for (inti = [array count] -1; i >
0; i--)
{
for (int j =0; j< i; j++)
{
comparecCount++;
if([[array objectAtIndex:i] integerValue] < [[arrayobjectAtIndex:j]integerValue])
{
[array exchangeObjectAtIndex:iwithObjectAtIndex:j];
}
}
}
输出比较的次数为:28
有序,只想把新来的元素插在哪里即可:
for (inti =0; i < [array
count]; i++)
{
for (int j =0; j< i; j++)
{
comparecCount++;
if ([[array objectAtIndex:i] integerValue]< [[array objectAtIndex:j]integerValue])
{
idtemp = [arrayobjectAtIndex:i];
[array removeObject:temp];
[array insertObject:tempatIndex:j];
break;
}
}
}
输出比较的次数为:17
备注:要多说一些细节,大家往NSMutableArray中插入一个元素(即使已经存在于数组中)insertObject总数量会变多。
UIView的subview的array中addsubview一个已存在只是将subview放到最上层,并不会有两个相同对象subview出现。
replaceObjectAtIndex:0 withObject:Object Object代替第0个对象,如果Object已经是Array中的一员,Array中就会有两个Object。
int comparecCount =
0;
NSMutableArray *array = [[NSMutableArrayalloc]initWithObjects:@"5",@"9",@"2",@"4",@"8",@"1",@"7",@"3",nil];
for (inti = [array count] -1; i >
0; i--)
{
for (int j =0; j< i; j++)
{
comparecCount++;
if([[array objectAtIndex:i] integerValue] < [[arrayobjectAtIndex:j]integerValue])
{
[array exchangeObjectAtIndex:iwithObjectAtIndex:j];
}
}
}
输出比较的次数为:28
有序,只想把新来的元素插在哪里即可:
for (inti =0; i < [array
count]; i++)
{
for (int j =0; j< i; j++)
{
comparecCount++;
if ([[array objectAtIndex:i] integerValue]< [[array objectAtIndex:j]integerValue])
{
idtemp = [arrayobjectAtIndex:i];
[array removeObject:temp];
[array insertObject:tempatIndex:j];
break;
}
}
}
输出比较的次数为:17
备注:要多说一些细节,大家往NSMutableArray中插入一个元素(即使已经存在于数组中)insertObject总数量会变多。
UIView的subview的array中addsubview一个已存在只是将subview放到最上层,并不会有两个相同对象subview出现。
replaceObjectAtIndex:0 withObject:Object Object代替第0个对象,如果Object已经是Array中的一员,Array中就会有两个Object。
相关文章推荐
- iOS自定义推送音效
- iOS: 从ipa提取图片资源文件及从iOS设备提取ipa文件的方法
- iOS开发证书变成“此证书的签发者无效”解决方法
- iOS开发--控件
- iOS开发-自定义控件的方式及注意(一)
- iOS闭包循环引用讲解
- iOS开发中如何合理地制造BUG
- iOS开发技巧系列---打造强大的BaseModel
- iOS中创建.gitignore文件
- iOS动画图层 — iOS模糊效果
- ios怎么让图片只有上半边圆角?
- iOS自带的拨号和短信功能
- iOS开发--即时通讯常用第三方库
- iOS8 之后对 SearchController的使用
- iOS国际化(多国语言设置)
- iOS开发之键盘
- iOS EXC_BAD_ACCESS的本质详解以及僵尸模式调试原理
- iOS导航及分享归纳与整理
- iOS开发--xcode快捷键
- iOS打电话