iOS实现排序

来源:互联网 发布:预算软件 编辑:程序博客网 时间:2024/05/21 21:37

这几天用OC实现了下常见的几种排序算法

1.选择排序
表现最稳定的排序算法之一,因为无论什么数据进去都是O(n²)的时间复杂度…..所以用到它的时候,数据规模越小越好。唯一的好处可能就是不占用额外的内存空间了。理论上讲,选择排序可能也是平时排序一般人想

-(void)rankArray:(NSMutableArray *)array{    for (int i = 0; i < array.count; i ++) {        for (int j = i + 1; j < array.count; j ++) {            if ([array[i] integerValue] > [array[j] integerValue]) {                [array exchangeObjectAtIndex:i withObjectAtIndex:j];            }        }    }}

2.冒泡排序 较慢 冒泡排序运行需要 O(N^2)时间级别

冒泡排序是一种简单的排序算法。它重复地走访过要排序的数列,一次比较两个元素,如果它们的顺序错误就把它们交换过来。走访数列的工作是重复地进行直到没有再需要交换,也就是说该数列已经排序完成。这个算法的名字由来是因为越小的元素会经由交换慢慢“浮”到数列的顶端

-(void)bubbleSort0:(NSMutableArray *)array{    for (int i = 1; i < array.count - 1; i ++) {        for (int j = 0; j < array.count - i; j ++) {            if ([array[j] integerValue] > [array[j + 1] integerValue]) {                [array exchangeObjectAtIndex:j + 1 withObjectAtIndex:j];            }        }    }}

2.1改进冒泡算法

改进冒泡排序: 设置一标志性变量pos,用于记录每趟排序中最后一次进行交换的位置。由于pos位置之后的记录均已交换到位,故在进行下一趟排序时只要扫描到pos位置即可。
传统冒泡排序中每一趟排序操作只能找到一个最大值或最小值,我们考虑利用在每趟排序中进行正向和反向两遍冒泡的方法一次可以得到两个最终值(最大者和最小者) , 从而使排序趟数几乎减少了一半。

-(void)bubbleSort:(NSMutableArray *)array{    int low = 0;    int height = (int)array.count - 1;    while(low < height){        for (int i = low; i < height; i ++) {            if ([array[i] integerValue] > [array[i + 1] integerValue]) {                [array exchangeObjectAtIndex:i + 1 withObjectAtIndex:i];            }        }        low++;        for (int i = height; i < low; i --) {            if ([array[i -1] integerValue] > [array[i] integerValue]) {                [array exchangeObjectAtIndex:i -1 withObjectAtIndex:i];            }        }        height--;    }}

3.插入排序

插入排序的代码实现虽然没有冒泡排序和选择排序那么简单粗暴,但它的原理应该是最容易理解的了,因为只要打过扑克牌的人都应该能够秒懂。当然,如果你说你打扑克牌摸牌的时候从来不按牌的大小整理牌,那估计这辈子你对插入排序的算法都不会产生任何兴趣了…..

-(void)insertionSort:(NSMutableArray *)array{    for (int i = 1; i < array.count; i ++) {        int temp = [array[i] intValue];        int index = i;        while (index > 0 && [array[index -1] integerValue] >= temp) {            array[index] = array[index -1];            index --;        }        array[index] = [NSNumber numberWithInt:temp];    } }

3.1改进插入排序: 查找插入位置时使用二分查找的方式

关键用二级制找到要插入的位置

-(void)binaryInsertionSort:(NSMutableArray *)array{    for (int i = 1; i < array.count; i ++) {        int key = [array[i] intValue];        int left = 0;        int right = i - 1;        while (left <= right) {            int middle = (left + right)/2;            if ([array[middle] integerValue] < key) {                left = middle + 1;            }else{                right = middle - 1;            }        }        for (int j = i ; j > left; j--) {            array[j] = array[j - 1];        }        array[left] = [NSNumber numberWithInt:key];    }}

4.快速排序

快速排序的思想,去一个基准值,将份数据根据基准值分为两份,做递归知道分到最小,最差情况是选择排序

-(void)quickSort:(NSMutableArray *)array leftIndex:(int)leftIndex right:(int)rightIndex{    if (leftIndex >= rightIndex) {        return;    }    int i = leftIndex;    int j = rightIndex;    //基准值    NSInteger temp = [array[leftIndex] integerValue];    while (j > i) {        while (j > i &&  temp <= [array[j] integerValue]) {            j--;        }        array[i] = array[j];        while (j > i &&  temp >= [array[i] integerValue]) {            i++;        }        array[j] = array[i];    }    array[i] = [NSNumber numberWithInteger:temp];    [self quickSort:array leftIndex:0 right:i-1];    [self quickSort:array leftIndex:i + 1 right:rightIndex];}

5.堆排序

//将最大数找到,移到根节点-(void)heapSort:(NSMutableArray *)array index:(int)index length:(int)len{    int f = 2 * index + 1;    int l = 2 * index + 2;    if (f < len && [array[f] integerValue] > [array[index] integerValue] ) {        [array exchangeObjectAtIndex:index withObjectAtIndex:f];    }    if (l < len && [array[l] integerValue] > [array[index] integerValue]) {        [array exchangeObjectAtIndex:index withObjectAtIndex:l];    }}
-(void)heapSort:(NSMutableArray *)array{    int i = 0;    int heapSize = 0;    for (heapSize = (int)array.count; heapSize > 0 ; heapSize --) {        //最后一个非叶子节点        for (i = heapSize/2 - 1; i >= 0; --i) {            [self heapSort:array index:i length:heapSize];        }        //将最大数移动到栈顶        [array exchangeObjectAtIndex:heapSize - 1 withObjectAtIndex:0];    }}

代码:> https://github.com/iossun/iOS-

原创粉丝点击