数据结构与算法 - OC 实现

【原创】http://www.cnblogs.com/luoguoqiang1985/

冒泡排序:通过N-1次对剩余未排序元素中最大(小)元素的上浮来实现排序,上浮过程通过交换相邻元素实现。

选择排序:通过N-1次将剩余未排序元素中最大(小)元素放置到数组尾部来实现排序。

插入排序:插入排序使用的是增量(incremental)方法;在排好子数组A[1..j-1]后,将A[j]插入,形成排好序的子数组A[1..j];

OC例子:

NSMutableArray+SortTools.h

 

//比较算法的块定义
//若需要置换返回YES,否则返回NO
typedef BOOL (^compareElement)(NSObject * el1, NSObject *el2);

@interface NSMutableArray (SortTools)
/*
 * 通过N-1次对剩余未排序元素中最大或最小元素的上浮来实现排序。
 * 上浮通过交换相邻元素实现
 */
- (void) sortByBubble:(compareElement) cmpBlock;
/*
 *通过N-1次将剩余未排序元素中最大(小)元素放置到数组尾部来实现排序。
 */
- (void) sortByChoose:(compareElement) cmpBlock;
/*
 *插入排序使用的是增量(incremental)方法;在排好子数组A[1..j-1]后,将A[j]插入,形成排好序的子数组A[1..j];
 */
- (void) sortByInsert:(compareElement) cmpBlock;
/*
 * 内容是否一样
 */
- (BOOL) isTheSame:(NSArray *)otherArray
 usingCompareBlock:(compareElement) cmpBlock;
 
@end

NSMutableArray+SortTools.m

@implementation NSMutableArray (SortTools)
/*
 *通过N-1次对剩余未排序元素中最大或最小元素的上浮来实现排序。
 *上浮通过交换相邻元素实现
 */
- (void) sortByBubble:(compareElement) cmpBlock
{
    NSObject *temp = nil;
    for(int i = 0; i < self.count - 1; i++){
        for(int j = 0; j < self.count - 1 - i; j++){
            if (cmpBlock([self objectAtIndex:j], [self objectAtIndex:j+1])) {
                temp = [self objectAtIndex:j];
                [self replaceObjectAtIndex:j
                                withObject:[self objectAtIndex:j+1]];
                [self replaceObjectAtIndex:j+1 withObject:temp];
            }
        }
    }
    temp = nil;
}

- (void) sortByChoose:(compareElement) cmpBlock{
    NSObject *temp = nil;
    NSInteger maxIndex = 0;
    for (int i = 0; i < self.count - 1; i++) {
        maxIndex = 0;
        for (int j = 0; j < self.count - 1 - i; j++) {
            if (cmpBlock([self objectAtIndex:maxIndex], [self objectAtIndex:j])) {
                maxIndex = j;
            }
        }
        temp = [self objectAtIndex:self.count - 1 - i];
        [self replaceObjectAtIndex:self.count - 1 - i
                        withObject:[self objectAtIndex:maxIndex]];
        [self replaceObjectAtIndex:maxIndex withObject:temp];
    }
    temp = nil;
}
/*
 *插入排序使用的是增量(incremental)方法;在排好子数组A[1..j-1]后,将A[j]插入,形成排好序的子数组A[1..j];
 */
- (void) sortByInsert:(compareElement) cmpBlock{
    NSObject *temp = nil;
    for (int i = 1; i < self.count; i++) {
        temp = [self objectAtIndex:i];
        int j = 0;
        for (j = i; j > 0 && cmpBlock(temp, [self objectAtIndex:j-1]) ; j--) {
            [self replaceObjectAtIndex:j withObject:[self objectAtIndex:j-1]];
        }
        [self replaceObjectAtIndex:j withObject:temp];
    }
}

/*
 * 内容是否一样
 */
- (BOOL) isTheSame:(NSArray *)otherArray
 usingCompareBlock:(compareElement) cmpBlock{
    
    BOOL isSame = YES;
    
    if (self.count != otherArray.count) {
        isSame = NO;
    } else {
        for (int i = 0; i < self.count; i++) {
            if ([self objectAtIndex:i] == nil) {
                continue;
            }
            
            if (!cmpBlock([self objectAtIndex:i], [otherArray objectAtIndex:i])) {
                isSame = NO;
                break;
            }
        }
    }
    
    return isSame;
}


@end

单元测试案例:

@synthesize testBubbleBefore, testBubbleAfter, testChooseAfter, testChooseBefore;

- (void)setUp
{
    [super setUp];

    // Set-up code here.
    self.testBubbleBefore = [NSMutableArray arrayWithObjects:@45, @2, @63,@11, nil];
    
    self.testBubbleAfter = [NSMutableArray arrayWithObjects:@2, @11, @45,@63, nil];
    
    self.testChooseBefore = [NSMutableArray arrayWithObjects:@45, @2, @63,@11, nil];
    
    self.testChooseAfter = [NSMutableArray arrayWithObjects:@2, @11, @45,@63, nil];
    
    self.testInsertB = [NSMutableArray arrayWithObjects:@45, @2, @63,@11, nil];
    
    self.testInsertA = [NSMutableArray arrayWithObjects:@2, @11, @45,@63, nil];
}

- (void)tearDown
{
    // Tear-down code here.
    
    [self.testBubbleBefore removeAllObjects];
    [self.testBubbleAfter removeAllObjects];
    [self.testChooseBefore removeAllObjects];
    [self.testChooseAfter removeAllObjects];
    [self.testInsertB removeAllObjects];
    [self.testInsertA removeAllObjects];
    
    [super tearDown];
}

/*
 *冒泡测试
 */
- (void)testBubble
{
    
    
    [self.testBubbleBefore sortByBubble:^BOOL(NSObject *el1, NSObject *el2) {
        __weak NSNumber *n1 = (NSNumber *)el1;
        __weak NSNumber *n2 = (NSNumber *)el2;
        return n1.intValue > n2.intValue;
    }];
    
    BOOL isSame = [self.testBubbleBefore isTheSame:self.testBubbleAfter usingCompareBlock:^BOOL(NSObject *el1, NSObject *el2) {
        __weak NSNumber *n1 = (NSNumber *)el1;
        __weak NSNumber *n2 = (NSNumber *)el2;
        return n1.intValue == n2.intValue;
    }];
    STAssertTrue(isSame, @"testBefore is not the same as testAfter!");
    
}

/*
 *选择排序测试
 */
- (void)testChoose
{
    
    
    [self.testChooseBefore sortByChoose:^BOOL(NSObject *el2, NSObject *el1) {
        __weak NSNumber *n2 = (NSNumber *)el2;
        __weak NSNumber *n1 = (NSNumber *)el1;
        return n1.intValue > n2.intValue;
    }];
    
    BOOL isSame = [self.testChooseBefore isTheSame:self.testChooseAfter usingCompareBlock:^BOOL(NSObject *el1, NSObject *el2) {
        __weak NSNumber *n1 = (NSNumber *)el1;
        __weak NSNumber *n2 = (NSNumber *)el2;
        return n1.intValue == n2.intValue;
    }];
    STAssertTrue(isSame, @"testBefore is not the same as testAfter!");
    
}

/*
 *插入排序测试
 */
- (void)testInsert
{
    
    
    [self.testInsertB sortByInsert:^BOOL(NSObject *el2, NSObject *el1) {
        __weak NSNumber *n1 = (NSNumber *)el1;
        __weak NSNumber *n2 = (NSNumber *)el2;
        return n1.intValue > n2.intValue;
    }];
    
    BOOL isSame = [self.testInsertB isTheSame:self.testInsertA usingCompareBlock:^BOOL(NSObject *el1, NSObject *el2) {
        __weak NSNumber *n1 = (NSNumber *)el1;
        __weak NSNumber *n2 = (NSNumber *)el2;
        return n1.intValue == n2.intValue;
    }];
    STAssertTrue(isSame, @"testBefore is not the same as testAfter!");
    
}

@end

 

posted @ 2014-01-06 08:24  路途遥远  阅读(3601)  评论(0编辑  收藏  举报