Objective-C 基本算法

二分法

//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];//处理右边
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: 好的,关于二叉树查找算法的实现,我可以为您提供一些帮助。 以下是一个Objective-C的二叉树查找算法的示例代码: ``` // 定义二叉树节点 @interface TreeNode : NSObject @property(nonatomic, assign) int value; @property(nonatomic, strong) TreeNode *leftChild; @property(nonatomic, strong) TreeNode *rightChild; @end @implementation TreeNode @end // 定义二叉树查找算法 @interface BinarySearchTree : NSObject @property(nonatomic, strong) TreeNode *root; - (void)insertNodeWithValue:(int)value; - (BOOL)searchNodeWithValue:(int)value; @end @implementation BinarySearchTree - (void)insertNodeWithValue:(int)value { TreeNode *newNode = [[TreeNode alloc] init]; newNode.value = value; if (self.root == nil) { self.root = newNode; } else { TreeNode *currentNode = self.root; while (YES) { if (value < currentNode.value) { if (currentNode.leftChild == nil) { currentNode.leftChild = newNode; break; } else { currentNode = currentNode.leftChild; } } else if (value > currentNode.value) { if (currentNode.rightChild == nil) { currentNode.rightChild = newNode; break; } else { currentNode = currentNode.rightChild; } } else { break; } } } } - (BOOL)searchNodeWithValue:(int)value { TreeNode *currentNode = self.root; while (currentNode != nil) { if (value < currentNode.value) { currentNode = currentNode.leftChild; } else if (value > currentNode.value) { currentNode = currentNode.rightChild; } else { return YES; } } return NO; } @end ``` 这个示例实现了一个二叉查找树,可以用来插入节点和查找节点。在这个示例中,我们定义了一个`TreeNode`类作为二叉树的节点,并在`BinarySearchTree`类中实现了插入节点和查找节点的方法。在插入节点时,我们按照二叉查找树的规则将节点插入到树中,而在查找节点时,则按照二叉查找树的规则进行遍历查找。 希望这个示例对您有所帮助! ### 回答2: 二叉树是一种重要的数据结构,在Objective-C中实现二叉树查找算法可以通过以下步骤完成: 首先,定义二叉树的节点类,可以包含一个值属性和两个指针属性(即左子节点和右子节点),代码如下: ``` // TreeNode.h 文件 #import <Foundation/Foundation.h> @interface TreeNode : NSObject @property (nonatomic, assign) NSInteger value; @property (nonatomic, strong) TreeNode *leftChild; @property (nonatomic, strong) TreeNode *rightChild; @end // TreeNode.m 文件 #import "TreeNode.h" @implementation TreeNode @end ``` 然后,实现二叉树的查找方法。这里使用递归算法,根据要查找的值和当前节点的值进行比较,如果相等则返回当前节点,否则根据大小关系判断在左子树还是右子树继续查找,直到找到值为止。代码如下: ``` // BinaryTreeSearch.m 文件 #import "BinaryTreeSearch.h" @implementation BinaryTreeSearch + (TreeNode *)searchValue:(NSInteger)value inTree:(TreeNode *)root { if (value == root.value) { return root; // 查找到了节点,返回该节点 } else if (value < root.value) { if (root.leftChild) { return [self searchValue:value inTree:root.leftChild]; // 在左子树中继续查找 } else { return nil; // 没有左子树,查找结束 } } else { if (root.rightChild) { return [self searchValue:value inTree:root.rightChild]; // 在右子树中继续查找 } else { return nil; // 没有右子树,查找结束 } } } @end ``` 以上是使用Objective-C实现二叉树查找算法基本步骤。在使用时,可以根据需要创建一个二叉树的根节点对象,并逐个添加节点,然后使用`[BinaryTreeSearch searchValue:inTree:]`方法进行查找。 ### 回答3: 以下是使用Objective-C语言写的一个二叉树查找算法: ```objective-c #import <Foundation/Foundation.h> @interface TreeNode : NSObject @property (nonatomic, assign) NSInteger value; @property (nonatomic, strong) TreeNode *left; @property (nonatomic, strong) TreeNode *right; @end @implementation TreeNode @end @interface BinaryTree : NSObject @property (nonatomic, strong) TreeNode *root; - (void)insertNodeWithValue:(NSInteger)value; - (TreeNode *)searchNodeWithValue:(NSInteger)value; @end @implementation BinaryTree - (void)insertNodeWithValue:(NSInteger)value { TreeNode *newNode = [[TreeNode alloc] init]; newNode.value = value; if (self.root == nil) { self.root = newNode; } else { TreeNode *current = self.root; while (YES) { if (value < current.value) { if (current.left != nil) { current = current.left; } else { current.left = newNode; break; } } else { if (current.right != nil) { current = current.right; } else { current.right = newNode; break; } } } } } - (TreeNode *)searchNodeWithValue:(NSInteger)value { TreeNode *current = self.root; while (current != nil && current.value != value) { if (value < current.value) { current = current.left; } else { current = current.right; } } return current; } @end int main(int argc, const char * argv[]) { @autoreleasepool { BinaryTree *binaryTree = [[BinaryTree alloc] init]; [binaryTree insertNodeWithValue:10]; [binaryTree insertNodeWithValue:5]; [binaryTree insertNodeWithValue:15]; [binaryTree insertNodeWithValue:3]; [binaryTree insertNodeWithValue:8]; TreeNode *foundNode = [binaryTree searchNodeWithValue:8]; if (foundNode != nil) { NSLog(@"找到了值为8的节点"); } else { NSLog(@"未找到值为8的节点"); } } return 0; } ``` 这是一个简单的二叉树查找算法的实现。它使用`TreeNode`类表示树节点,每个节点都包含一个值、左子节点和右子节点。`BinaryTree`类代表二叉树,包含插入节点和查找节点的方法。 在主函数中,我们创建了一个二叉树对象,并插入了一些节点。然后,我们使用`searchNodeWithValue`方法来查找值为8的节点,并根据返回的结果打印相应的消息。 希望这个答案能对您有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值