和在VC++6.0里相比 在OC里面实现 不算困难 可是我用惯了C/C++呢

快速排序,冒泡排序,直接插入排序和折半插入排序,希尔排序,堆排序,直接选择排序

/*******************************快速排序 start**********************************/
//随即取 当前取第一个,首先找到第一个的位置,然后分成left和right两组子集 ,分别对left和right继续执行分割(同上操作)

-(void)QuickSort:(NSMutableArray *)list StartIndex:(NSInteger)startIndex EndIndex:(NSInteger)endIndex{
    
    if(startIndex >= endIndex)return;
    
    NSNumber * temp = [list objectAtIndex:startIndex];
    NSInteger tempIndex = startIndex; //临时索引 处理交换位置(即下一个交换的对象的位置)
    
    for(int i = startIndex + 1 ; i <= endIndex ; i++){
        
        NSNumber *t = [list objectAtIndex:i];
        
        if([temp intValue] > [t intValue]){
            
            tempIndex = tempIndex + 1;
            
            [list exchangeObjectAtIndex:tempIndex withObjectAtIndex:i];
            
        }
        
    }
    
    [list exchangeObjectAtIndex:tempIndex withObjectAtIndex:startIndex];
    [self QuickSort:list StartIndex:startIndex EndIndex:tempIndex-1];
    [self QuickSort:list StartIndex:tempIndex+1 EndIndex:endIndex];

}

/*******************************快速排序 end**********************************/

/*******************************冒泡排序 start**********************************/
//取第一个 与其邻接的对比,若大则交换
-(void)BubbleSort:(NSMutableArray *)list{
    
    for (int j = 1; j<= [list count]; j++) {
            
        for(int i = 0 ;i < j ; i++){
            
            if(i == [list count]-1)return;
            
            NSInteger a1 = [[list objectAtIndex:i] intValue];
            NSInteger a2 = [[list objectAtIndex:i+1] intValue];
            
            if(a1 > a2){
                [list exchangeObjectAtIndex:i withObjectAtIndex:i+1];
            }
            
        }
        
    }
    
}
/*******************************冒泡排序 end**********************************/

/*******************************直接插入排序 start**********************************/
//从无序表中取出第一个元素,插入到有序表的合适位置,使有序表仍然有序
-(void)InsertSort:(NSMutableArray *)list{
    
    for(int i = 1 ; i < [list count] ; i++){
        
        
        int j = i;
        NSInteger temp= [[list objectAtIndex:i] intValue];
        
        while (j > 0 && temp < [[list objectAtIndex:j - 1]intValue]) {
            
            [list replaceObjectAtIndex:j withObject:[list objectAtIndex:(j-1)]];
            //list[j] = list[j-1];
            j--;
        
        }
        [list replaceObjectAtIndex:j withObject:[NSNumber numberWithInt:temp]];
        //list[j] = temp;
        
    }
    
}
/*******************************直接插入排序 end**********************************/

/*******************************折半插入排序 start**********************************/
//从无序表中取出第一个元素,利用折半查找插入到有序表的合适位置,使有序表仍然有序
-(void)BinaryInsertSort:(NSMutableArray *)list{
    
    //索引从1开始 默认让出第一元素为默认有序表 从第二个元素开始比较
    for(int i = 1 ; i < [list count] ; i++){
        
        //binary search start
        NSInteger temp= [[list objectAtIndex:i] intValue];
        
        int left = 0; 
        int right = i - 1;
        
        while (left <= right) {
            
            int middle = (left + right)/2;
            
            if(temp < [[list objectAtIndex:middle] intValue]){
                
                right = middle - 1;
                
            }else{
                
                left = middle + 1;
            
            }
            
        }
        //binary search end
        
        //移动3,5,6,[4] 4是当前目标对象 利用binarysearch 找到4应该在有续集{3,5,6}的位置,然后向后移动即{3,5,6,[4]}-->{3,[4],5,6}
        for(int j = i ; j > left; j--){
            
            [list replaceObjectAtIndex:j withObject:[list objectAtIndex:j-1]];
            
        }
        [list replaceObjectAtIndex:left withObject:[NSNumber numberWithInt:temp]];
        
    }
    
    
}
/*******************************折半插入排序 end**********************************/

/*******************************希尔排序 start**********************************/
//对直接插入排序优化,创造一个gap 来对表进行分割,对分割后的每个子集进行直接插入排序 知道gap==1结束
-(void)shellSort:(NSMutableArray *)list{

int gap = [list count] / 2;
    
    while (gap >= 1) {
        
        
        for(int i = gap ; i < [list count]; i++){
        
            NSInteger temp = [[list objectAtIndex:i] intValue];
            int j = i;
            
            while (j >= gap && temp < [[list objectAtIndex:(j - gap)] intValue]) {
                [list replaceObjectAtIndex:j withObject:[list objectAtIndex:j-gap]];
                j -= gap;
            }
            [list replaceObjectAtIndex:j withObject:[NSNumber numberWithInt:temp]];
            
            
        }
        
        gap = gap / 2;
    }

}
/*******************************希尔排序 end**********************************/

/*******************************堆排序 start**********************************/
//创建最大堆heap 最大/最小优先级队列
-(void)CreateBiggestHeap:(NSMutableArray *)list Count:(NSInteger)count{
    
    //int count = [list count];
    int lastParentIndex = (count - 2)/2;
    for(int i = lastParentIndex; i >= 0 ; i--){
        
        NSInteger parentIndex = i;
        NSInteger parentNode = [[list objectAtIndex:parentIndex] intValue];
        
        
        //获取左子结点为当前子结点
        int currentChildIndex = 2*i + 1;
        // 
        
        while (currentChildIndex <= count - 1) {
            
            NSInteger leftChildNode = [[list objectAtIndex:(currentChildIndex)] intValue];
            
            if((currentChildIndex + 1) <= count-1){//表示存在右子结点
                
                //读取右子结点
                int rightChildIndex =currentChildIndex + 1;
                NSInteger rightChildNode = [[list objectAtIndex:(rightChildIndex)] intValue];
                
                //如果右子结点为最大
                if(rightChildNode > leftChildNode && rightChildNode > parentNode){
                    [list exchangeObjectAtIndex:parentIndex withObjectAtIndex:rightChildIndex];
                    currentChildIndex = rightChildIndex;//右子结点为当前子结点 继续循环
                //左子结点最大
                }else if(leftChildNode > rightChildNode && leftChildNode > parentNode){
                    [list exchangeObjectAtIndex:parentIndex withObjectAtIndex:currentChildIndex];
                }
                
            }else{
                
                if(leftChildNode > parentNode){
                    [list exchangeObjectAtIndex:parentIndex withObjectAtIndex:currentChildIndex];
                    
                }
                
            }
            
            //更新父结点和下一个子结点
            parentIndex = currentChildIndex;
            currentChildIndex = 2*currentChildIndex + 1;
                        
        }

}
    
}
//每次执行最大堆(索引要前移动 即排除已经排好的最大堆头元算 交换到list尾部的这个元素)
-(void)HeapSort:(NSMutableArray *)list{
    
    for(int i = [list count] ; i > 0; i--){
        
        [self CreateBiggestHeap:list Count:i];
        
        //NSLog(@"%@",list);
        
        [list exchangeObjectAtIndex:(i-1) withObjectAtIndex:0];
        
    }
    
}

/*******************************堆排序 end**********************************/

/*******************************直接选择排序 start**********************************/
//在对象集中选出最小的 若不是第一个 则与第一个交换 在剩余的对象集中选出最小的 执行前面的步骤
-(void)SelectSort:(NSMutableArray *)list{
    
    for(int i = 0 ; i<[list count]; i++){
        
        int k = i;
        for(int j = i+1 ; j<[list count]; j++){
            
            NSInteger jvalue = [[list objectAtIndex:j] intValue];
            NSInteger kvalue = [[list objectAtIndex:k] intValue];
            
            if(jvalue < kvalue){
                k = j;
            }
            
        }
        if(k != i){
            [list exchangeObjectAtIndex:i withObjectAtIndex:k];
        }
        
    }
    
}

/*******************************直接选择排序 end**********************************/

转载于:https://www.cnblogs.com/someonelikeyou/p/3620520.html

OC 实现的几个排序算法相关推荐

  1. 排序算法c语言和oc实现的,几种常用的排序算法,OC实现

    1.冒泡排序 原理是:比较相邻的元素,如果第一个比第二个大,就交换他们两个.对每一个相邻元素作同样的工作,从开始第一对到结尾的最后一对,重复以上步骤,直到没有任何一对数字需要比较. n个元素比较n-1 ...

  2. 伍六七带你学算法 进阶篇-排序算法

    给定一个整数数组 nums,将该数组升序排列. 示例 1: 输入:[5,2,3,1] 输出:[1,2,3,5] 示例 2: 输入:[5,1,1,2,0,0] 输出:[0,0,1,1,2,5] 各排序算 ...

  3. C++排序算法实现(更新中)

    比较排序法:如冒泡排序.简单选择排序.合并排序.快速排序.其最优的时间复杂度为O(nlogn). 其他排序法:如桶排序.基数排序等.时间复杂度可以达到O(n).但试用范围有要求. 桶排序:排序的数组元 ...

  4. 十种经典排序算法精粹(c语言版本)

    下面给出这段时间我苦心研究验证过的十种经典排序算法的C语言版本,即下面的排序算法: 插入排序,shell排序,冒泡排序,快速排序,选择排序,堆排序,归并排序,桶排序,基数排序和计数排序.整理出来以作备 ...

  5. 十大排序算法 导图总结

    以下为我们经常用到的十大典型排序算法导图,很多设计以及优化的思想值得去参考学习 因为代码较多,所以都添加到对应的实现注释中了,相关代码可以从Mind-mapping获取xmind源文件 参考文档: 基 ...

  6. C++实现十大排序算法(冒泡,选择,插入,归并,快速,堆,希尔,桶,计数,基数)排序算法时间复杂度、空间复杂度、稳定性比较(面试经验总结)

    排序算法分类 内部排序算法又分为基于比较的排序算法和不基于比较的排序算法,其分类如下: 比较排序:   直接插入排序    希尔排序 (插入)  冒泡排序     快速排序  (交换) 直接选择排序  ...

  7. C++拾取——使用stl标准库实现排序算法及评测

    今天看了一篇文章,讲各种语言的优势和劣势.其中一个观点:haskell非常适合写算法,因为使用者不用去关心具体的计算机实现,而只要关注于操作语义.这让它在专心研究算法的人中非常受欢迎.所以很多时候,语 ...

  8. 常用排序算法的C++实现

    排序是将一组"无序"的记录序列调整为"有序"的记录序列. 假定在待排序的记录序列中,存在多个具有相同的关键字的记录,若经过排序,这些记录的相对次序保持不变,即在 ...

  9. python 排序算法 简书_Python---简析八大排序算法

    前言 1 .排序的概念 排序是计算机内经常进行的一种操作,其目的是将一组"无序"的记录序列调整为"有序"的记录序列. 排序分为内部排序和外部排序. 若整个排序过 ...

最新文章

  1. EchoesWorks —— 打造下一代技术Blog/Presentation 框架(招兵买马)
  2. 基于SSM实现在校学生考试系统
  3. 如何在vue里面正确的引用 jquery 和 第三方插件
  4. MySQL_前缀索引_建立
  5. SOA实现方式与模式
  6. redis的scan命令的源码分析,实现原理
  7. 使用一些可选的将字符串配置属性转换为其他类型
  8. 带有Spring,Hibernate,Akka,Twitter Bootstrap,Apache Tiles和jQuery的Maven Web项目Kickstarter代码库...
  9. kettle使用记录
  10. python课设答辩ppt_学生成绩管理系统答辩幻灯片.ppt
  11. JAVA羽毛球篮球运动场地预约管理系统毕业设计 开题报告
  12. mysql 分页 conut优化_mysql count函数与分页功能极限优化
  13. PR基础学习(四) 简单裁剪视频
  14. 「笔耕不辍」悲观锁和乐观锁的区别以及实现方式
  15. gulp压缩html
  16. 谁的盛世————读《饥饿的盛世》有感
  17. 通过iptables 禁止访问域名方法整合
  18. Unity3D摄像机远、近切面绘制
  19. 【机器学习】Unsupervised feature selection by regularized self-representation(RSR)
  20. 《在网页中实现手机验证码登陆代码》

热门文章

  1. php 分页类 bootstrap,ThinkPHP分页使用bootstrap样式
  2. extjs实现组织架构图_如何画好一张架构图?(内含知识图谱)
  3. 修改Tomcat窗口名称
  4. Zigbee学习计划暂停
  5. 谷歌提出深度CNN模型NIMA:帮你挑选清晰且有美感的图片
  6. 解决:mysql5.7 timestamp默认值0000-00-00 00:00:00 报错
  7. 微服务那么热,创业公司怎么选用实践?
  8. MySQL5.7的多源复制
  9. jmeter插件下载
  10. 服务器模型---总结