數據結構與算法 - OC 實現

【原創】http://www.cnblogs.com/luoguoqiang1985/算法

冒泡排序:經過N-1次對剩餘未排序元素中最大(小)元素的上浮來實現排序,上浮過程經過交換相鄰元素實現。數組

選擇排序:經過N-1次將剩餘未排序元素中最大(小)元素放置到數組尾部來實現排序。單元測試

插入排序:插入排序使用的是增量(incremental)方法;在排好子數組A[1..j-1]後,將A[j]插入,造成排好序的子數組A[1..j];測試

OC例子:spa

NSMutableArray+SortTools.hcode

 

//比較算法的塊定義
//若須要置換返回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.mblog

@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
相關文章
相關標籤/搜索