Objective-C 基本演算法

lf644206005發表於2015-03-25

二分法

//binarySearch.h
#import <Foundation/Foundation.h>
@interface binarySearch : NSObject
-(NSInteger) binarySearchIndex:(NSNumber *)key arrayBySearch:(NSMutableArray *) array;
@end

//binarySearch.m
#import "binarySearch.h"
@implementation binarySearch
-(NSInteger) binarySearchIndex:(NSNumber *)key arrayBySearch:(NSMutableArray *) array{
    NSInteger min = 0;
    NSInteger max = (NSInteger)([array count]-1);
    while (min <= max) {
        NSInteger mid = (min+max)/2;
        if (key > [array objectAtIndex:mid]) {
            min = mid + 1;
        }else if (key < [array objectAtIndex:mid]){
            max = mid - 1;
        }else if(key == [array objectAtIndex:mid]){
            return mid;
        }
    }
    return -1;
}

@end
//實現
@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSMutableArray *test = [[NSMutableArray alloc] init];
    for (int i=0; i<20; i=i+2) {
        [test addObject:[NSNumber numberWithInteger:i]];
    }
    binarySearch *myBinarysearch = [[binarySearch alloc] init];
    NSInteger getInt = [myBinarysearch binarySearchIndex:[NSNumber numberWithInteger:100] arrayBySearch:test];
    NSLog(@"%ld", (long)getInt);   
}

氣泡排序

//bubble.h
#import <Foundation/Foundation.h>
@interface bubble : NSObject
-(NSMutableArray *)bubbleSort:(NSMutableArray *)array;
@end

//bubble.m
#import "bubble.h"
@implementation bubble
-(NSMutableArray *)bubbleSort:(NSMutableArray *)array{
    for (int j=0; j<array.count; j++) {
        for (int i=0; i<array.count-j-1; i++) {
            if (i == [array count]-1) {
                return array;
            }
            NSInteger pre = [[array objectAtIndex:i] intValue];
            NSInteger beh = [[array objectAtIndex:i+1] intValue];    
            if (pre >beh) {
                [array exchangeObjectAtIndex:i withObjectAtIndex:i+1];
            }
        }
    }
    return array;
}
@end

快速排序

//快速排序
-(void)quickSort:(NSMutableArray *)_dataSource startIndex:(NSInteger)start endIndex:(NSInteger)end{
    if (start<end) {
        NSInteger standardValue=[_dataSource[start] intValue];
        NSInteger left=start,right=end;
        while (start<end) {
            //從後往前找,如果後面的數值大於基準值,遞減
            while (start<end&&[_dataSource[end] intValue]>standardValue) {
                end--;
            }
            //小於基準值的時候,給陣列中索引為start賦值
            if (start<end) {
                _dataSource[start]=_dataSource[end];
                start=start+1;
            }
            //從前往後找,如果數值小於基準值,遞增
            while (start<end&&[_dataSource[start] intValue]<standardValue) {
                start++;
            }
            //大於基準值,給陣列中索引為end的資料賦值
            if (start<end) {
                _dataSource[end]=_dataSource[start];
                end=end-1;
            }
        }
        //退出的時候值startend相等
        _dataSource[start]=[NSString stringWithFormat:@"%ld",(long)standardValue];
        [self quickSort:_dataSource startIndex:left endIndex:end-1];//處理左邊
        [self quickSort:_dataSource startIndex:end+1 endIndex:right];//處理右邊
    }
}

相關文章