简单的算法---冒泡排序
2015-07-07 11:23
225 查看
刚开始学习算法,写了个小小的Demo,如有不好的地方欢迎大家多多指教!
NSMutableArray *array = [
NSMutableArray arrayWithObjects:@"12",@"12",@"99",@"76",@"76",
nil];
for (
int i = 4; i >=
0; i --) {
for (
int j = 0; j < i ; j ++ ) {
int a = [[ array
objectAtIndex: j ] intValue];
int b = [[array
objectAtIndex: (j + 1)]
intValue ] ;
int temp =
0;
if (a <= b) {
temp = b ;
b = a ;
a = temp ;
[ array replaceObjectAtIndex:j
withObject:[ NSNumber
numberWithInt:a]] ;
[ array replaceObjectAtIndex:(j +
1) withObject:[
NSNumber numberWithInt:b]] ;
}
}
}
for (int i =
0; i < array.count; i ++) {
NSLog(@"%@", [array
objectAtIndex:i]) ;
}
NSMutableArray *array = [
NSMutableArray arrayWithObjects:@"12",@"12",@"99",@"76",@"76",
nil];
for (
int i = 4; i >=
0; i --) {
for (
int j = 0; j < i ; j ++ ) {
int a = [[ array
objectAtIndex: j ] intValue];
int b = [[array
objectAtIndex: (j + 1)]
intValue ] ;
int temp =
0;
if (a <= b) {
temp = b ;
b = a ;
a = temp ;
[ array replaceObjectAtIndex:j
withObject:[ NSNumber
numberWithInt:a]] ;
[ array replaceObjectAtIndex:(j +
1) withObject:[
NSNumber numberWithInt:b]] ;
}
}
}
for (int i =
0; i < array.count; i ++) {
NSLog(@"%@", [array
objectAtIndex:i]) ;
}
相关文章推荐
- 提高matlab运行速度和节省空间的心得
- Serif和Sans-serif字体的区别
- css 使容器宽度适应内容宽
- asp.net mvc 如何在View中获取Url参数的值
- checkbox属性checked="checked"已有,但却不显示打勾的解决办法
- Mac 键盘快捷键
- Windows 10本周五正式版上线
- 关于“被继承的样式特性值为0”的理解
- [leetcode][list] Remove Duplicates from Sorted List II
- Apple Swift编程语言入门教程
- 正则表达式-验证
- jyphon 环境变量配置
- ADB server didn't ACK failed to start daemon
- 《C语言及程序设计》实践参考——二进制文件浏览器
- matlab提速技巧(自matlab帮助文件)
- jyphon 环境变量配置
- 创建,删除文件或文件夹及判断其是否存在
- 驱动-linux 中断处理
- 优雅的Android应用程序完全退出方案
- poj 1286