话不多说,学了这么多,写个快速排序先。

除了快排,以后有时间还要加堆排、归并等等。

今天学了有,类、协议、语法。

因为算法类,不止一个算法。所以新建一个Algorithm(算法)协议:

 #import <Foundation/Foundation.h>

 @protocol AlgorithmProtocol <NSObject>

 @optional
+(void)quickSortWithArray:(NSMutableArray*)array FirstIndex:(long)first EndIndex:(long)end CompareMethod:(bool(^)(id,id)) compare; @end

接下来,新建一个Algorithm(算法)类,遵循算法协议:

 #import <Foundation/Foundation.h>
#import "AlgorithmProtocol.h" @interface Algorithm : NSObject <AlgorithmProtocol> @end @implementation Algorithm +(void)quickSortWithArray:(NSMutableArray*)array FirstIndex:(long)firstIndex EndIndex:(long)endIndex CompareMethod:(bool(^)(id,id)) compare{
long (^partition)(NSMutableArray*,long,long) = ^(NSMutableArray *innerArray,long first,long end){
long i = first;
long j = end;
while (i<j) {
while (i<j && !compare(innerArray[i],innerArray[j])) {
j--;
}
if (i<j) {
id tmp = innerArray[i];
innerArray[i] = innerArray[j];
innerArray[j] = tmp;
i++;
}
while (i<j && !compare(innerArray[i],innerArray[j])) {
i++;
}
if (i<j) {
id tmp = innerArray[i];
innerArray[i] = innerArray[j];
innerArray[j] = tmp;
j--;
}
}
return i;
};
if (firstIndex<endIndex) {
long pivot = ;
pivot = partition(array,firstIndex,endIndex);
[self quickSortWithArray:array FirstIndex:firstIndex EndIndex:pivot- CompareMethod:compare];
[self quickSortWithArray:array FirstIndex:pivot+ EndIndex:endIndex CompareMethod:compare];
}
} @end

然后就是使用,main文件:

#import <Foundation/Foundation.h>
#import "Algorithm.h" int main(int argc, const char * argv[]) {
@autoreleasepool {
//测试数组
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject:@];
[array addObject:@];
[array addObject:@];
[array addObject:@];
[array addObject:@];
NSMutableArray *arrayCopy = [array copy]; [Algorithm quickSortWithArray:array FirstIndex: EndIndex:[array count] - CompareMethod:^bool(id obj1, id obj2) {
if (obj1>obj2){
return true;
}else{
return false;
}
}]; NSLog(@"\n排序前:%@\n排序后:%@",arrayCopy,array);
}
return ;
}

验证一下结果:

Objective-C学习笔记-第一天(3)的更多相关文章

  1. ActionBarSherlock学习笔记 第一篇——部署

    ActionBarSherlock学习笔记 第一篇--部署          ActionBarSherlock是JakeWharton编写的一个开源框架,使用这个框架,可以实现在所有的Android ...

  2. oracle学习笔记第一天

    oracle学习笔记第一天 --oracle学习的第一天 --一.几个基础的关键字   1.select select (挑选) 挑选出显示的--列--(可以多列,用“,”隔开,*表示所有列),为一条 ...

  3. Django学习笔记---第一天

    Django学习笔记 1.Django的安装 //如果不指定版本号,默认安装最新版 pip3 install django==1.11.8 关于Django的版本和python的版本依赖关系,请看下图 ...

  4. ASP.NET Core 学习笔记 第一篇 ASP.NET Core初探

    前言 因为工作原因博客断断续续更新,其实在很早以前就有想法做一套关于ASP.NET CORE整体学习度路线,整体来说国内的环境的.NET生态环境还是相对比较严峻的,但是干一行爱一行,还是希望更多人加入 ...

  5. GIT学习笔记——第一章

    git之vim编辑器退出命令 # 学习笔记 张文军微博主页  张文军码云主页   张文军新浪云主页  张文军博客主页 ## 刚学习git,好多东西没接触过,进入vim后不知道如何出来了,网上找了很多都 ...

  6. 《JavaScript权威指南》学习笔记 第一天。

    这是零零散散的笔记,作为自己看书打demo的笔记.不足为各位学习,留作自己复习知识点备用. 1.检测对象中某个属性存在不存在: <script> // in 运算符 //不管是对象的自有属 ...

  7. PRML学习笔记第一章

    [转] PRML笔记 - 1.1介绍 模式识别的目标 自动从数据中发现潜在规律,以利用这些规律做后续操作,如数据分类等. 模型选择和参数调节 类似的一族规律通常可以以一种模型的形式为表达,选择合适模型 ...

  8. 菜鸟Python学习笔记第一天:关于一些函数库的使用

    2017年1月3日 星期二 大一学习一门新的计算机语言真的很难,有时候连函数拼写出错查错都能查半天,没办法,谁让我英语太渣. 关于计算机语言的学习我想还是从C语言学习开始为好,Python有很多语言的 ...

  9. AndroidStudio学习笔记-第一个安卓程序

    要带一个本科生做一部分跟安卓有点关系的项目,于是趁着机会学习一下编写安卓程序. 第一篇材料来自谷歌官方,传送门:https://developer.android.com/training/basic ...

  10. 2011斯坦福大学iOS应用开发教程学习笔记(第一课)MVC.and.Introduction.to.Objective-C

    blog.csdn.net/totogo2010/article/details/8205810  目录(?)[-] 第一课名称 MVC and Introduction to Objective-C ...

随机推荐

  1. Stylish: http://bbs.csdn.net/topics/

    [id^=google_ads_], [id^=bd_ad_], #cpro_u2392825, [id^=BAIDU_SSP_], .bbs_top_ad, .csdn-toolbar, #topi ...

  2. hash命令

    什么是hash ? 在网上找了好久都没找到简截有力的说明.hash 我把它当成是集合,一个hash 就是一个集合,里面字段对应一个元素,元素不重复,字段都不一样. 简单hash 命令 1.hset 哈 ...

  3. javaScript实现修改输入框之后标红

    <html> <title>实现标红</title> <script type="text/javascript">  functi ...

  4. 【20160924】GOCVHelper 图像处理部分(3)

    //根据轮廓的圆的特性进行选择     vector<VP> selectShapeCircularity(Mat src,Mat& draw,vector<VP> c ...

  5. js继承之call,apply和prototype随谈

    在js中,call,apply和prototype都可以实现对象的继承,下面我们看一个例子: function FatherObj1() { this.sayhello = "I am jo ...

  6. 基础笔记3(一)(String StringBuilder StringBuffer 数组)

    ---恢复内容开始--- 1数组.有序的同类型的集合. 2.string :字符串类型:其实就是一个字符数组.添加了private final,所以string是一个不可以变的字符串. String. ...

  7. 用Python编写的第一个回测程序

    用Python编写的第一个回测程序 2016-08-06 def savfig(figureObj, fn_prefix1='backtest8', fn_prefix2='_1_'): import ...

  8. ElasticSearch学习问题记录——nested查询不到数据

    通过代码创建了索引名称为demoindex,索引类型为school,以下是索引类型的数据映射结构: { "state": "open", "setti ...

  9. JAVA 使用POI导出数据格式为Execl

    需要下载一个poi的jar包. 控制器 @Override public void getContractListExecl(Contract contract, BindingResult resu ...

  10. 运行Maven是报错:No goals have been specified for this build

    No goals have been specified for this build. You must specify a valid lifecycle phase or a goal in t ...