美文网首页
iOS 选择排序和堆排序

iOS 选择排序和堆排序

作者: 雪中夜归人 | 来源:发表于2019-10-12 10:14 被阅读0次

选择排序(Selection Sort)

  选择排序的核心思想:找到数组中最大值(或者最小),然后跟最后元素交换。(去除最后元素,重复操作)

- (void)sort
{
    NSInteger count = self.sortArray.count;
    for (NSInteger end = count - 1; end >= 0; end--) {
        NSInteger maxIndex = 0;
        for (NSInteger begain = 1; begain <= end; begain++) {
            if ([self compareObjcet:self.sortArray[maxIndex] anotherObject:self.sortArray[begain]] == YES) {
                maxIndex = begain;
            }
        }
        if (maxIndex != end) {
            [self swapObjectWithIndex:maxIndex anotherIndex:end];
        }
    }
}

  显然选择排序的核心就是寻找最大(或者最小值)。此上代码用的最简单的一种方式---遍历查找。

堆排序(Heap Sort)

  那么有一种结构能够更快的查找到最大最小值,那就是大顶堆或者小顶堆。那么这种方式的排序就是堆排序(Heap Sort),其实质上可以看成是选择排序的优化。

第一步:将数组建堆,而建堆的核心是数组中元素下沉(site down)

// 建堆操作
- (void)buildMaxHeap
{
    NSInteger arrayCount = self.sortArray.count;
    for(NSInteger i = arrayCount - 1; i > 0; i = i - 2){
        NSInteger currentIndex = (i + 1) / 2 - 1;
        [self siteDownWithIndex:currentIndex heapSize:arrayCount];
    }
}
//  指定下标的数据下沉
- (void)siteDownWithIndex:(NSInteger)index heapSize:(NSInteger)heapSize
{
    if (heapSize <= 1) {
        return;
    }
    NSInteger leftIndex = (index << 1) + 1;
    
    NSInteger rightIndex = (index << 1) + 2;
    
    id currentValue = self.sortArray[index];
    id leftValue = nil;
    id rightValue = nil;
    
    if (leftIndex >= heapSize) {
        return;
    }
    
    leftValue = self.sortArray[leftIndex];
    
    if (rightIndex < heapSize) {
        rightValue = self.sortArray[rightIndex];
        if ([self compareObjcet:currentValue anotherObject:leftValue] && [self compareObjcet:rightValue anotherObject:leftValue]) {
            [self swapObjectWithIndex:leftIndex anotherIndex:index];
            [self siteDownWithIndex:leftIndex heapSize:heapSize];
        }
        
        if ([self compareObjcet:currentValue anotherObject:rightValue] && [self compareObjcet:leftValue anotherObject:rightValue]) {
            [self swapObjectWithIndex:rightIndex anotherIndex:index];
            [self siteDownWithIndex:rightIndex heapSize:heapSize];
        }
        return;
    }
    if ([self compareObjcet:currentValue anotherObject:leftValue]) {
        [self swapObjectWithIndex:leftIndex anotherIndex:index];
    }
}

第二步:将堆顶元素与最后元素交换然后堆顶元素下沉,重新生成大顶堆

- (void)sort
{
    NSInteger arrayCount = self.sortArray.count;
    [self buildMaxHeap];
    
    for (NSInteger i = arrayCount - 1; i >= 1; i--) {
        [self swapObjectWithIndex:0 anotherIndex:i];
        [self siteDownWithIndex:0 heapSize:i];
    }
}

总结:选择排序和堆排序都可以看成是寻找最大值然后依次在数组中放好的过程。

相关文章

  • iOS算法总结-堆排序

    iOS算法总结-堆排序 iOS算法总结-堆排序

  • iOS 选择排序和堆排序

    选择排序(Selection Sort)   选择排序的核心思想:找到数组中最大值(或者最小),然后跟最后元素交换...

  • 选择排序法

    常用的选择排序方法有两种:直接选择排序和堆排序。直接排序简单直观,但性能略差;堆排序是一种较为高效的选择排序方法,...

  • 排序

    目的 方便查找 内排序 交换 冒泡排序 快速排序 选择 直接选择 堆排序 插入 -直接插入 堆排序 基数排序

  • 03-堆排序(Heap Sort)

    堆排序(Heap Sort) 结合上一讲的内容,发现选择排序可以使用堆排序来进行优化。所以堆排序可以认为是对选择排...

  • 排序

    一、选择排序 1.堆排序 定义:堆排序是利用堆这种数据结构而设计的一种排序算法,堆排序是一种选择排序可参考http...

  • 3.2-选择排序-堆排序

    参考链接 选择排序:堆排序(Heap Sort) 白话经典算法系列之七 堆与堆排序 堆排序与快速排序,归并排序一样...

  • 常见的排序算法-2.1 选择排序(堆排序)

    选择排序的优化方案(堆排序)

  • 排序法

    排序分 内部排序和外部排序 内部排序: 插入排序:{直接插入排序,希尔排序} 选择排序:{简单选择排序,堆排序} ...

  • iOS - 堆排序

    Demo_github 堆排序 堆排序(Heap Sort)是一种树形选择排序,是对直接选择排序的有效改进。 堆的...

网友评论

      本文标题:iOS 选择排序和堆排序

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