和在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**********************************/

OC 实现的几个排序算法的更多相关文章

  1. 数据结构与算法之PHP排序算法(快速排序)

    一.基本思想 快速排序又称划分交换排序,是对冒泡排序的一种改进,亦是分而治之思想在排序算法上的典型应用. 它的基本思想是:通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部 ...

  2. 个性化排序算法实践(五)——DCN算法

    wide&deep在个性化排序算法中是影响力比较大的工作了.wide部分是手动特征交叉(负责memorization),deep部分利用mlp来实现高阶特征交叉(负责generalizatio ...

  3. 个性化排序算法实践(三)——deepFM算法

    FM通过对于每一位特征的隐变量内积来提取特征组合,最后的结果也不错,虽然理论上FM可以对高阶特征组合进行建模,但实际上因为计算复杂度原因,一般都只用到了二阶特征组合.对于高阶特征组合来说,我们很自然想 ...

  4. JavaScript实现常用的排序算法

    ▓▓▓▓▓▓ 大致介绍 由于最近要考试复习,所以学习js的时间少了 -_-||,考试完还会继续的努力学习,这次用原生的JavaScript实现以前学习的常用的排序算法,有冒泡排序.快速排序.直接插入排 ...

  5. 排序算法----基数排序(RadixSort(L))单链表智能版本

    转载http://blog.csdn.net/Shayabean_/article/details/44885917博客 先说说基数排序的思想: 基数排序是非比较型的排序算法,其原理是将整数按位数切割 ...

  6. 常见排序算法(附java代码)

    常见排序算法与java实现 一.选择排序(SelectSort) 基本原理:对于给定的一组记录,经过第一轮比较后得到最小的记录,然后将该记录与第一个记录的位置进行交换:接着对不包括第一个记录以外的其他 ...

  7. 几大排序算法的Java实现

    很多的面试题都问到了排序算法,中间的算法和思想比较重要,这边我选择了5种常用排序算法并用Java进行了实现.自己写一个模板已防以后面试用到.大家可以看过算法之后,自己去实现一下. 1.冒泡排序:大数向 ...

  8. 排序算法----基数排序(RadixSort(L,max))单链表版本

    转载http://blog.csdn.net/Shayabean_/article/details/44885917博客 先说说基数排序的思想: 基数排序是非比较型的排序算法,其原理是将整数按位数切割 ...

  9. 排序算法汇总(C/C++实现)

    前言:     本人自接触算法近2年以来,在不断学习中越多地发觉各种算法中的美妙.之所以在这方面过多的投入,主要还是基于自身对高级程序设计的热爱,对数学的沉迷.回想一下,先后也曾参加过ACM大大小小的 ...

随机推荐

  1. js中Number数字数值运算后值不对

    问题: 37.5*5.5=206.08 (JS算出来是这样的一个结果,我四舍五入取两位小数) 我先怀疑是四舍五入的问题,就直接用JS算了一个结果为:206.08499999999998 怎么会这样,两 ...

  2. hdu 5424 Rikka with Graph II (BestCoder Round #53 (div.2))(哈密顿通路判断)

    http://acm.hdu.edu.cn/showproblem.php?pid=5424 哈密顿通路:联通的图,访问每个顶点的路径且只访问一次 n个点n条边 n个顶点有n - 1条边,最后一条边的 ...

  3. PC端 $_SERVER 说明

    $_SERVER['PHP_SELF'] #当前正在执行脚本的文件名,与 document root相关. $_SERVER['argv'] #传递给该脚本的参数. $_SERVER['argc'] ...

  4. 关于 三星 I9100 (水货)

    前天陪好友去买水货9100,总结了一点经验,觉得挺有用的,今天整理一下写出来...有 需要的可以看看..原创整理.. 一,当然是检查外观(检查USB接口有没有磨损,检查摄像头是否有灰尘,检查屏幕是不是 ...

  5. 【Python实战02】共享Python代码到PyPI社区

    之前学习了Python的列表,以及编写了一个函数来进行列表的输出,这次我们就继续来学习如何把我们已经编写好的代码共享到PyPI社区,这里以上篇文章中编写的print_lol函数为例. 函数转换为模块 ...

  6. 【AS3 Coder】任务七:初涉PureMVC——天气预报功能实现

    转自:http://www.iamsevent.com/post/36.html AS3 Coder]任务七:初涉PureMVC——天气预报功能实现 使用框架:AS3任务描述:了解PureMVC框架使 ...

  7. Oracle数据库定时任务配置和日志执行情况查询

    基础配置: /***************************************************************** * * 移动抵扣券快到期推送提醒 * 首次执行 : 2 ...

  8. 2.里氏替换原则(Liskov Substitution Principle)

    1.定义 里氏替换原则的定义有两种,据说是由麻省理工的一位姓里的女士所提出,因此以其名进行命名. 定义1:如果对一个类型为T1的对象o1,都有类型为T2的对象o2,使得以T1所定义的程序P中在o1全都 ...

  9. Eclipse配置PyDev插件

    安装python解释器 安装PyDev: 首先需要去Eclipse官网下载:http://www.eclipse.org/,Eclipse需要JDK支持,如果Eclipse无法正常运行,请到Java官 ...

  10. C++ map注意事项

    1.在map中,由key查找value时,首先要判断map中是否包含key. 2.如果不检查,直接返回map[key],可能会出现意想不到的行为.如果map包含key,没有问题,如果map不包含key ...