美文网首页
基本算法

基本算法

作者: 音吹 | 来源:发表于2016-10-28 10:08 被阅读3次
  • 1.冒泡算法
   NSMutableArray * allData = [NSMutableArray array];
    for (int i = 0; i < 10; i++) {
        [allData addObject:@(i)];
    }
    for (int i = 0; i < allData.count-1; i++) {
        for (int j = 0; j < allData.count-1-i; j++) {
            NSInteger a = [allData[j] integerValue];
            NSInteger b = [allData[j+1] integerValue];
            if (a < b) {
                allData[j] = @(b);
                allData[j+1] = @(a);
            }
        }
    }

  • 2.选择算法
  NSMutableArray * allData2 = [NSMutableArray array];
    for (int i = 0; i < 10; i++) {
        [allData2 addObject:@(i)];
    }
    for (int i = 0; i< allData2.count -1; i++) {

        for (int j = i +1; j < allData2.count; j++) {
            NSInteger a = [allData2[i] integerValue];
            NSInteger b = [allData2[j] integerValue];
            if (a < b) {
                allData2[i] = @(b);
                allData2[j] = @(a);
            }
        }
    }

*上面这两个算法耗时基本相同.

  • 插入算法
  NSMutableArray * allData3 = [NSMutableArray array];
    for (int i = 0; i < 1000; i++) {
        [allData3 addObject:@(i)];
    }
    for (int i = 0; i < allData3.count; i++) {
        for (int j = i; j >0&&(allData3[j]<allData3[j-1]); j--) {
            [allData3 exchangeObjectAtIndex:j withObjectAtIndex:j-1];
        }
    }

*耗时比上面两个小

  • 快速排序 StartIndex:0 EndIndex : arr.count-1
-(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 = (int)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];
    
}

相关文章

  • 基本算法

    冒泡算法 选择排序 插入排序 顺序查找 二分查找

  • 基本算法

    预热姿势: 什么是二叉查找树 (二叉排序树) 1.红黑树 规则: 插入删除节点时的方法: 2. B-树 (也叫B树...

  • 基本算法

    1.冒泡算法 2.选择算法 *上面这两个算法耗时基本相同. 插入算法 *耗时比上面两个小 快速排序 Start...

  • 基本算法

    冒泡排序、插入排序、选择排序、快速排序、二分查找(Objective-C实现)

  • 基本算法

    排序 冒泡排序 比较相邻的元素。如果第一个比第二个大,就交换他们两个。 对每一对相邻元素做同样的工作,从开始第一对...

  • 基本算法

    题目汇总 面试中常用到机试题[https://cloud.tencent.com/developer/articl...

  • 基本算法

    分治 分治分治,即分而治之。分治,就是把一个复杂的问题分成两个或更多的相同或相似的子问题,再把子问题分成更小的子问...

  • 基本KMeans和二分Kmeans的python实现

    基本Kmeans算法 二分KMeans算法

  • JVM垃圾回收(GC)整理总结学习

    摘要:基本回收算法 1. 引用计数(Reference Counting) 比较古老的回收算法。 基本回收算法 1...

  • 最“懒惰”的kNN分类算法

    1. K-近邻算法#### k-近邻算法(k Nearest Neighbor),是最基本的分类算法,其基本思想是...

网友评论

      本文标题:基本算法

      本文链接:https://www.haomeiwen.com/subject/vwasuttx.html