算法

作者: onlyyourself | 来源:发表于2017-03-07 20:19 被阅读7次

//冒泡排序从大到小

  • (void)orderBubbleSort:(NSMutableArray *)mArray {
    for (int i = 0; i < mArray.count - 1; i ++) {
    for (int j = 0; j < mArray.count - i - 1; j ++) {
    if ([mArray[j] intValue] > [mArray[j + 1] intValue]) {
    [mArray exchangeObjectAtIndex:j withObjectAtIndex:j + 1];
    }
    }
    }
    NSLog(@"从小到大%@",mArray);
    }

//冒泡排序从小到大

  • (void)reverseBubbleSort:(NSMutableArray *)mArray {
    for (int i = 0; i < mArray.count - 1; i ++) {
    for (int j = 0; j < mArray.count - i - 1; j ++) {
    if ([mArray[j] intValue] < [mArray[j + 1] intValue]) {
    [mArray exchangeObjectAtIndex:j withObjectAtIndex:j + 1];
    }
    }
    }
    NSLog(@"从大到大小%@",mArray);
    }

//快速排序

  • (void)quickSort:(NSMutableArray *)mArray leftIndex:(int )left rightIndex:(int )right
    {
    if (left < right) {
    int temp = [self getMiddleIndex:mArray leftIndex:left rightIndex:right];
    [self quickSort:mArray leftIndex:left rightIndex:temp - 1];
    [self quickSort:mArray leftIndex:temp + 1 rightIndex:right];
    }
    NSLog(@"%@",mArray);
    }

  • (int )getMiddleIndex:(NSMutableArray *)mArray leftIndex:(int )left rightIndex:(int )right {
    int tempValue = [mArray[left] intValue];
    while (left < right) {
    while ((left < right) && (tempValue <= [mArray[right] intValue])) {
    right --;
    }
    if (left < right) {
    mArray[left] = mArray[right];
    }
    while (left < right && ([mArray[left] intValue] <= tempValue)) {
    left ++;
    }
    if (left < right) {
    mArray[right] = mArray[left];
    }
    }
    mArray[left] = [NSNumber numberWithInt:tempValue];
    return left;
    }

  • (void)HTTquickSort:(NSMutableArray *)quickArray leftIndex:(NSInteger)left rightIndex:(NSInteger )right
    {
    NSInteger i = left;
    NSInteger j = right;
    NSString *t;
    if (left>right) {
    NSLog(@"%@",quickArray);
    return;
    }
    NSString *temp = quickArray[left];
    while (i != j) {
    while (([quickArray[j] integerValue]>[temp integerValue] || [quickArray[j] integerValue] == [temp integerValue]) && i<j)
    j--;
    while (([quickArray[i] integerValue] < [temp integerValue] || [quickArray[i] integerValue] == [temp integerValue]) && i<j)
    i++;
    if (i<j) {
    t = quickArray[i];
    quickArray[i] = quickArray[j];
    quickArray[j] = t;

      }
    

    }
    quickArray[left] = quickArray[i];
    quickArray[i] = temp;

    [self HTTquickSort:quickArray leftIndex:left rightIndex:i-1];
    [self HTTquickSort:quickArray leftIndex:i+1 rightIndex:right];

}

//选择排序

  • (void)selectSort:(NSMutableArray *)mArray {
    for (int i = 0; i < mArray.count; i ++) {
    int minIndex = i;
    for (int j = i + 1; j < mArray.count; j ++) {
    if ([mArray[minIndex] intValue] > [mArray[j] intValue]) {
    minIndex = j;
    }
    }
    if(minIndex != i) {
    //如果不是无序区的最小值位置不是默认的第一个数据,则交换。
    [mArray exchangeObjectAtIndex:i withObjectAtIndex:minIndex];
    }
    }
    }

//直接插入排序

  • (void)insertSort:(NSMutableArray*)array {
    for (int i = 0;i < [array count] - 1;i ++) {
    if([[array objectAtIndex:i + 1]intValue] < [[array objectAtIndex:i] intValue]) {
    NSNumber *temp=[array objectAtIndex:i+1];
    for (int j = i + 1; j > 0 && [[array objectAtIndex:j - 1] intValue] > [temp intValue]; j --){
    [array exchangeObjectAtIndex:j - 1 withObjectAtIndex:j];
    }
    }
    }
    }

//二分插入排序

  • (void)binaryInsertSort:(NSMutableArray *)mArray {
    //索引从1开始 默认让出第一元素为默认有序表 从第二个元素开始比较
    for(int i = 1 ; i < [mArray count] ; i++){
    //二分查找
    int temp= [[mArray objectAtIndex:i] intValue];
    int left = 0;
    int right = i - 1;
    while (left <= right) {
    int middle = (left + right)/2;
    if(temp < [[mArray objectAtIndex:middle] intValue]){
    right = middle - 1;
    } else {
    left = middle + 1;
    }
    }
    //排序
    for(int j = i ; j > left; j --){
    [mArray replaceObjectAtIndex:j withObject:[mArray objectAtIndex:j - 1]];
    }
    [mArray replaceObjectAtIndex:left withObject:[NSNumber numberWithInt:temp]];
    }
    }

//希尔排序
-(void)shellSort:(NSMutableArray *)mArray {
int gap = (int)[mArray count] / 2;
while (gap >= 1) {
for(int i = gap ; i < [mArray count]; i ++){
int temp = [[mArray objectAtIndex:i] intValue];
int j = i;
while (j >= gap && temp < [[mArray objectAtIndex:(j - gap)] intValue]) {
[mArray replaceObjectAtIndex:j withObject:[mArray objectAtIndex:j-gap]];
j -= gap;
}
[mArray replaceObjectAtIndex:j withObject:[NSNumber numberWithInt:temp]];
}
gap = gap / 2;
}
}

//堆排序(目前不支持负数)

  • (void)heapSort:(NSMutableArray *)mArray isAsc:(BOOL)isAsc {
    for (NSInteger i = mArray.count / 2 - 1; i >= 0; -- i) {
    [self siftWithLow:i high:mArray.count asc:isAsc array:mArray];
    }
    for (NSInteger i = mArray.count - 1; i >= 1; -- i) {
    id temp = mArray[0];
    mArray[0] = mArray[i];
    mArray[i] = temp;
    [self siftWithLow:0 high:i asc:isAsc array:mArray];
    }
    }

  • (void)siftWithLow:(NSInteger)low high:(NSInteger)high asc:(BOOL)isAsc array:(NSMutableArray *)mArray {
    NSInteger left = 2 * low + 1;
    NSInteger right = left + 1;
    NSInteger lastIndex = low;
    //左子节点大的情况
    if (left < high && ((mArray[left] > mArray[lastIndex] && isAsc) || (mArray[left] < mArray[lastIndex] && !isAsc))) lastIndex = left;
    //右子节点大的情况
    if (right < high && ((mArray[right] > mArray[lastIndex] && isAsc) || (mArray[right] < mArray[lastIndex] && !isAsc))) lastIndex = right;
    //节点改变
    if (lastIndex != low) {
    //较大的节点值将交换到其所在节点的父节点
    id temp = mArray[low];
    mArray[low] = mArray[lastIndex];
    mArray[lastIndex] = temp;
    //递归遍历
    [self siftWithLow:lastIndex high:high asc:isAsc array:mArray];
    }
    }
    @end

相关文章

  • 匈牙利算法

    算法思想 算法流程 算法步骤 算法实现 python 算法应用

  • web开发需要知道的几个算法

    算法分类 快速排序算法 深度优先算法 广度优先算法 堆排序算法 归并排序算法

  • 机器学习算法

    机器学习的算法分监督算法和无监督 算法。监督算法包括回归算法,神经网络,SVM;无监督算法包括聚类算法,降维算法。...

  • 字符串匹配

    BF 算法和 RK 算法BM 算法和 KMP 算法

  • 垃圾回收算法有几种类型? 他们对应的优缺点又是什么?

    常见的垃圾回收算法有: 标记-清除算法、复制算法、标记-整理算法、分代收集算法 标记-清除算法 标记—清除算法包括...

  • 头条-手撕代码

    [toc] 图算法 以及最短路径算法 树算法 手写LRU 排序算法 链表算法

  • 关于一些算法

    我们平常说的算法按照使用方向加密算法,排序算法,搜索算法,优化算法,音视频处理算法,图片处理算法 1.加密解密算法...

  • 给我巨大影响的技术书籍

    算法《算法概论》《算法设计与分析基础》 Anany Levitin《算法引论》Udi Manber《算法导论》《什...

  • 缓存相关

    cache淘汰算法:LIRS 算法 缓存那些事 Redis缓存淘汰算法,LRU算法,LRU算法讲解

  • LZW压缩算法

    参考链接:超级简单的数据压缩算法—LZW算法压缩算法——lzw算法实现LZW算法 LZW 压缩算法正确图解

网友评论

      本文标题:算法

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