/*******************************快速排序 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**********************************/

IOS- 快速排序,冒泡排序,直接插入排序和折半插入排序,希尔排序,堆排序,直接选择排序的更多相关文章

  1. 牛客网Java刷题知识点之插入排序(直接插入排序和希尔排序)、选择排序(直接选择排序和堆排序)、冒泡排序、快速排序、归并排序和基数排序(博主推荐)

    不多说,直接上干货! 插入排序包括直接插入排序.希尔排序. 1.直接插入排序: 如何写成代码: 首先设定插入次数,即循环次数,for(int i=1;i<length;i++),1个数的那次不用 ...

  2. 直接插入排序、折半插入排序、shell插入排序

    直接插入排序:   折半插入排序:   shell插入排序:  

  3. C语言排序算法之简单交换法排序,直接选择排序,冒泡排序

    C语言排序算法之简单交换法排序,直接选择排序,冒泡排序,最近考试要用到,网上也有很多例子,我觉得还是自己写的看得懂一些. 简单交换法排序 /*简单交换法排序 根据序列中两个记录键值的比较结果来对换这两 ...

  4. 直接插入排序、折半插入排序、Shell排序、冒泡排序,选择排序

    一.直接插入排序 稳定,时间复杂度:最好O(n).最差O(n^2).平均O(n^2).空间复杂度O(1) void InsertSort(int L[], int n) { int i, j,key; ...

  5. python 实现排序算法(三)-选择排序和冒泡排序

    #/usr/bin/env python #coding:utf-8 #@auther="livermorium" ''' 选择排序 从数据中选择最小值,排在位置首位 再从剩余未排 ...

  6. 对于近似有序序列(即除掉少数K个元素后是有序序列且K<<n),试分析直接插入排序,冒牌排序和简单选择排序的时间复杂度

    学弟问的一道数据结构的题,关于一些排序算法的时间复杂度. 针对近似有序序列, ①当使用直接插入排序时,其基本操作为数组中元素的移动.最好情况下,待排序列有序,无需移动,此时时间复杂度为O(n), 当为 ...

  7. Python实现八大排序(基数排序、归并排序、堆排序、简单选择排序、直接插入排序、希尔排序、快速排序、冒泡排序)

    目录 八大排序 基数排序 归并排序 堆排序 简单选择排序 直接插入排序 希尔排序 快速排序 冒泡排序 时间测试 八大排序 大概了解了一下八大排序,发现排序方法的难易程度相差很多,相应的,他们计算同一列 ...

  8. 常见排序算法总结:插入排序,希尔排序,冒泡排序,快速排序,简单选择排序以及java实现

    今天来总结一下常用的内部排序算法.内部排序算法们需要掌握的知识点大概有:算法的原理,算法的编码实现,算法的时空复杂度的计算和记忆,何时出现最差时间复杂度,以及是否稳定,何时不稳定. 首先来总结下常用内 ...

  9. php排序介绍_冒泡排序_选择排序法_插入排序法_快速排序法

    这里我们介绍一些常用的排序方法,排序是一个程序员的基本功,所谓排序就是对一组数据,按照某个顺序排列的过程. 充效率看 冒泡排序法<选择排序法<插入排序法 排序分两大类: 内部排序法 交换式 ...

随机推荐

  1. HTML Table导出为Excel的方法

    HTML Table导出为Excel的方法: 直接上源码 <html> <head> <meta http-equiv="Content-Type" ...

  2. JS仿Android Toast提示效果

    注:这个需要jquery文件来提示支持,所以需要先调用Jquery. <script type="text/javascript" src="js/jquery.j ...

  3. Linq查询操作语句学习

    对于一个集合,我们通常会用foreach或者for循环来判断查找里面的元素. 但这种方法通常会看起来比较复杂,我们可以使用linq. Linq允许编写C#代码以查询数据库相同的方式操作内存数据(写法类 ...

  4. I’ve seen the world,lit it up as my stage now

    I've seen the world,lit it up as my stage now 阅尽繁华 点亮红尘做舞台 Channeling angels in,the new age now 粉末登场 ...

  5. CSS浏览器兼容性与解析问题终极归纳

    1.怪异模式问题:漏写DTD声明,Firefox仍然会按照标准模式来解析网页,但在IE中会触发怪异模式.为避免怪异模式给我们带来不必要的麻烦,最好养成书写DTD声明的好习惯. 2.IE6双边距问题:在 ...

  6. DOM的相关优化

    为什么要进行DOM优化? DOM对象本身也是一个js对象,所以严格来说,并不是操作这个对象慢,而是说操作了这个对象后,会触发一些浏览器行为,比如布局(layout)和绘制(paint). 首先先说一些 ...

  7. 剑指Offer 左旋转字符串

    题目描述 汇编语言中有一种移位指令叫做循环左移(ROL),现在有个简单的任务,就是用字符串模拟这个指令的运算结果.对于一个给定的字符序列S,请你把其循环左移K位后的序列输出.例如,字符序列S=”abc ...

  8. cvGet2D的用法

    CvScalar s;s = cvGet2D(src, j,i);//获取src图像中坐标为(i,j)的像素点的值s.val[0] 代表src图像BGR中的B通道的值~int nXY = cvGet2 ...

  9. Linux环境变量的添加设置

    以前一直都记着的,后来做的事情多了就什么都忘的差不多了. 在Linux中,环境变量一般添加有这么几个地方: 1./etc/profile 这个目录设置的变量为整个系统的全局变量,所有的用户通用,一般不 ...

  10. 2015安徽省赛 I.梯田

    http://xcacm.hfut.edu.cn/problem.php?id=1213 set + 搜索 姐姐是用搜索+二分做的,效率要高很多 #include<iostream> #i ...