Objective-C实现常用的4种排序算法

来源:互联网 发布:感染病毒源码 编辑:程序博客网 时间:2024/05/18 01:06

OC实现的4种排序又来了!

4种排序分别是:快速排序、冒泡排序、选择排序、插入排序,其他的我就不写了,因为OC里的数组中不能存放基本数据类型,如int不能存放,只能放对象,所以所有的数据我用了NSNumber类型,一开始我直接用>、=、<来比较结果排序后还是乱七八糟,后来想起来不能这么比较,对象的比较,可以用compare方法,结果与NSComparisonResult这个枚举类型的数据比较大小就可以了。或者取NSNumber 的intValue,在用>、=、<进行比较,第一个方法中有些两种方式的语句,后来的类似就不写了。

1、快速排序

#pragma - mark 快速排序+ (void)quickSort:(NSMutableArray *)array low:(int)low high:(int)high{    if(array == nil || array.count == 0){        return;    }    if (low >= high) {        return;    }        //取中值    int middle = low + (high - low)/2;    NSNumber *prmt = array[middle];    int i = low;    int j = high;        //开始排序,使得left<prmt 同时right>prmt    while (i <= j) {//        while ([array[i] compare:prmt] == NSOrderedAscending) {  该行与下一行作用相同        while ([array[i] intValue] < [prmt intValue]) {            i++;        }//        while ([array[j] compare:prmt] == NSOrderedDescending) { 该行与下一行作用相同        while ([array[j] intValue] > [prmt intValue]) {            j--;        }                if(i <= j){            [array exchangeObjectAtIndex:i withObjectAtIndex:j];            i++;            j--;        }                printf("排序中:");        [self printArray:array];    }        if (low < j) {        [self quickSort:array low:low high:j];    }    if (high > i) {        [self quickSort:array low:i high:high];    }}
快速排序的过程如下:

排序前:9 2 10 7 3 7 4 排序中:4 2 10 7 3 7 9 排序中:4 2 7 7 3 10 9 排序中:4 2 7 3 7 10 9 排序中:2 4 7 3 7 10 9 排序中:2 4 3 7 7 10 9 排序中:2 3 4 7 7 10 9 排序中:2 3 4 7 7 9 10 排序中:2 3 4 7 7 9 10 排序后:2 3 4 7 7 9 10
2、冒泡排序

#pragma - mark 冒泡排序+ (void)buddleSort:(NSMutableArray *)array{    if(array == nil || array.count == 0){        return;    }        for (int i = 1; i < array.count; i++) {        for (int j = 0; j < array.count - i; j++) {            if ([array[j] compare:array[j+1]] == NSOrderedDescending) {                [array exchangeObjectAtIndex:j withObjectAtIndex:j+1];            }                        printf("排序中:");            [self printArray:array];        }    }}
冒泡排序的过程如下:

排序前:9 2 10 7 3 7 4 排序中:2 9 10 7 3 7 4 排序中:2 9 10 7 3 7 4 排序中:2 9 7 10 3 7 4 排序中:2 9 7 3 10 7 4 排序中:2 9 7 3 7 10 4 排序中:2 9 7 3 7 4 10 排序中:2 9 7 3 7 4 10 排序中:2 7 9 3 7 4 10 排序中:2 7 3 9 7 4 10 排序中:2 7 3 7 9 4 10 排序中:2 7 3 7 4 9 10 排序中:2 7 3 7 4 9 10 排序中:2 3 7 7 4 9 10 排序中:2 3 7 7 4 9 10 排序中:2 3 7 4 7 9 10 排序中:2 3 7 4 7 9 10 排序中:2 3 7 4 7 9 10 排序中:2 3 4 7 7 9 10 排序中:2 3 4 7 7 9 10 排序中:2 3 4 7 7 9 10 排序中:2 3 4 7 7 9 10 排序后:2 3 4 7 7 9 10
3、选择排序

+ (void)selectSort:(NSMutableArray *)array{    if(array == nil || array.count == 0){        return;    }        int min_index;    for (int i = 0; i < array.count; i++) {        min_index = i;        for (int j = i + 1; j<array.count; j++) {            if ([array[j] compare:array[min_index]] == NSOrderedAscending) {                [array exchangeObjectAtIndex:j withObjectAtIndex:min_index];            }                        printf("排序中:");            [self printArray:array];        }    }}
选择排序的过程如下:

排序前:9 2 10 7 3 7 4 排序中:2 9 10 7 3 7 4 排序中:2 9 10 7 3 7 4 排序中:2 9 10 7 3 7 4 排序中:2 9 10 7 3 7 4 排序中:2 9 10 7 3 7 4 排序中:2 9 10 7 3 7 4 排序中:2 9 10 7 3 7 4 排序中:2 7 10 9 3 7 4 排序中:2 3 10 9 7 7 4 排序中:2 3 10 9 7 7 4 排序中:2 3 10 9 7 7 4 排序中:2 3 9 10 7 7 4 排序中:2 3 7 10 9 7 4 排序中:2 3 7 10 9 7 4 排序中:2 3 4 10 9 7 7 排序中:2 3 4 9 10 7 7 排序中:2 3 4 7 10 9 7 排序中:2 3 4 7 10 9 7 排序中:2 3 4 7 9 10 7 排序中:2 3 4 7 7 10 9 排序中:2 3 4 7 7 9 10 排序后:2 3 4 7 7 9 10
4、插入排序

#pragma - mark 插入排序+ (void)inserSort:(NSMutableArray *)array{    if(array == nil || array.count == 0){        return;    }        for (int i = 0; i < array.count; i++) {        NSNumber *temp = array[i];        int j = i-1;                while (j >= 0 && [array[j] compare:temp] == NSOrderedDescending) {            [array replaceObjectAtIndex:j+1 withObject:array[j]];            j--;                        printf("排序中:");            [self printArray:array];        }                [array replaceObjectAtIndex:j+1 withObject:temp];    }}
插入排序的过程如下:

排序前:9 2 10 7 3 7 4 排序中:9 9 10 7 3 7 4 排序中:2 9 10 10 3 7 4 排序中:2 9 9 10 3 7 4 排序中:2 7 9 10 10 7 4 排序中:2 7 9 9 10 7 4 排序中:2 7 7 9 10 7 4 排序中:2 3 7 9 10 10 4 排序中:2 3 7 9 9 10 4 排序中:2 3 7 7 9 10 10 排序中:2 3 7 7 9 9 10 排序中:2 3 7 7 7 9 10 排序中:2 3 7 7 7 9 10 排序后:2 3 4 7 7 9 10 
另外,类的代码也附上吧!

////  SortUtil.h//  SortUtil////  Created by Mac on 14-4-17.//  Copyright (c) 2014年 KnightKing. All rights reserved.//#import <Foundation/Foundation.h>@interface SortUtil : NSObject//快速排序+ (void)quickSort:(NSMutableArray *)array low:(int)low high:(int)high;//冒泡排序+ (void)buddleSort:(NSMutableArray *)array;//选择排序+ (void)selectSort:(NSMutableArray *)array;//插入排序+ (void)inserSort:(NSMutableArray *)array;//打印数组+ (void)printArray:(NSArray *)array;@end

////  SortUtil.m//  SortUtil////  Created by Mac on 14-4-17.//  Copyright (c) 2014年 KnightKing. All rights reserved.//#import "SortUtil.h"@implementation SortUtil#pragma - mark 快速排序+ (void)quickSort:(NSMutableArray *)array low:(int)low high:(int)high{    if(array == nil || array.count == 0){        return;    }    if (low >= high) {        return;    }        //取中值    int middle = low + (high - low)/2;    NSNumber *prmt = array[middle];    int i = low;    int j = high;        //开始排序,使得left<prmt 同时right>prmt    while (i <= j) {//        while ([array[i] compare:prmt] == NSOrderedAscending) {  该行与下一行作用相同        while ([array[i] intValue] < [prmt intValue]) {            i++;        }//        while ([array[j] compare:prmt] == NSOrderedDescending) { 该行与下一行作用相同        while ([array[j] intValue] > [prmt intValue]) {            j--;        }                if(i <= j){            [array exchangeObjectAtIndex:i withObjectAtIndex:j];            i++;            j--;        }                printf("排序中:");        [self printArray:array];    }        if (low < j) {        [self quickSort:array low:low high:j];    }    if (high > i) {        [self quickSort:array low:i high:high];    }}#pragma - mark 冒泡排序+ (void)buddleSort:(NSMutableArray *)array{    if(array == nil || array.count == 0){        return;    }        for (int i = 1; i < array.count; i++) {        for (int j = 0; j < array.count - i; j++) {            if ([array[j] compare:array[j+1]] == NSOrderedDescending) {                [array exchangeObjectAtIndex:j withObjectAtIndex:j+1];            }                        printf("排序中:");            [self printArray:array];        }    }}#pragma - mark 选择排序+ (void)selectSort:(NSMutableArray *)array{    if(array == nil || array.count == 0){        return;    }        int min_index;    for (int i = 0; i < array.count; i++) {        min_index = i;        for (int j = i + 1; j<array.count; j++) {            if ([array[j] compare:array[min_index]] == NSOrderedAscending) {                [array exchangeObjectAtIndex:j withObjectAtIndex:min_index];            }                        printf("排序中:");            [self printArray:array];        }    }}#pragma - mark 插入排序+ (void)inserSort:(NSMutableArray *)array{    if(array == nil || array.count == 0){        return;    }        for (int i = 0; i < array.count; i++) {        NSNumber *temp = array[i];        int j = i-1;                while (j >= 0 && [array[j] compare:temp] == NSOrderedDescending) {            [array replaceObjectAtIndex:j+1 withObject:array[j]];            j--;                        printf("排序中:");            [self printArray:array];        }                [array replaceObjectAtIndex:j+1 withObject:temp];    }}+ (void)printArray:(NSArray *)array{    for(NSNumber *number in array) {        printf("%d ",[number intValue]);    }        printf("\n");}@end

调用我就写在了app启动的方法里:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];    // Override point for customization after application launch.    self.window.backgroundColor = [UIColor whiteColor];    [self.window makeKeyAndVisible];        NSMutableArray *array = [NSMutableArray arrayWithObjects:@9,@2,@10,@7,@3,@7,@4,nil];        printf("排序前:");    [SortUtil printArray:array];    //快速排序//    [SortUtil quickSort:array low:0 high:6];    //冒泡排序//    [SortUtil buddleSort:array];    //选择排序//    [SortUtil selectSort:array];    //插入排序    [SortUtil inserSort:array];        printf("排序后:");    [SortUtil printArray:array];        return YES;}


0 0
原创粉丝点击