码迷,mamicode.com
首页 > 编程语言 > 详细

总结Objective-c常用算法

时间:2015-07-12 22:55:25      阅读:224      评论:0      收藏:0      [点我收藏+]

标签:

      今天是星期天,想睡到10点起床,结果认为自己太奢侈了,不能这么做,于是把闹钟设置成了6:30;结果终于9:36醒了,起床,无缘无故迟了,好吧,就算太累了吧,周天就原谅自己一回。终于到了中午,辗转反侧,用objective-c去实现一个计算器程序,调试不成四则运算计算器算法,总是差那么一点点,却还是差那么一点点,运行不起来,终于决定出去办点事然后回去教室问同学……….,后来发现,这些算法非常重要,就算摔倒了爬起来也要记得,切记,切记,由四则运算算法得到的启示,然后查找资料总结的objective-c常用算法如下:

    本次主要是总结排序算法,其他算法有待总结

#import <Foundation/Foundation.h>
10 
11 @interface Sort : NSObject{
12 
13 }
14 
15 //选择排序
16 -(void)selectSortWithArray:(NSArray *)aData;
17 //插入排序
18 -(void)insertSortWithArray:(NSArray *)aData;
19 //快速排序
20 -(void)quickSortWithArray:(NSArray *)aData;
21 
22 -(void)swapWithData:(NSMutableArray *)aData index1:(NSInteger)index1index2:(NSInteger)index2;
23 
24 
25 @end

 技术分享

 9 #import "Sort.h"
10 
11 @interface Sort()
12 -(void)quickSortWithArray:(NSArray *)aData left:(NSInteger)leftright:(NSInteger)right;
13 @end
14 
15 @implementation Sort
16 
17 - (id)init
18 {
19     self = [super init];
20     if (self) {
21         // Initialization code here.
22     }
23     
24     return self;
25 }
26 
27 -(void)selectSortWithArray:(NSArray *)aData{
28     NSMutableArray *data =[[NSMutableArray alloc]initWithArray:aData];
29     for (int i=0; i<[data count]-1;i++) {
30         int m =i;
31         for (int j =i+1; j<[datacount]; j++) {
32             if ([data objectAtIndex:j]< [data objectAtIndex:m]) {
33                 m = j;
34             }
35         }
36         if (m != i) {
37             [self swapWithData:dataindex1:m index2:i];
38         }
39     }
40     NSLog(@"选择排序后的结果:%@",[data description]);
41     [data release];
42 }
43 
44 -(void)insertSortWithArray:(NSArray *)aData{
45     NSMutableArray *data =[[NSMutableArray alloc]initWithArray:aData];
46     for (int i = 1; i < [datacount]; i++) {
47         id tmp = [dataobjectAtIndex:i];
48         int j = i-1;
49         while (j != -1 &&[data objectAtIndex:j] > tmp) {
50             [datareplaceObjectAtIndex:j+1 withObject:[data objectAtIndex:j]];
51             j--;
52         }
53         [data replaceObjectAtIndex:j+1withObject:tmp];
54     }
55     NSLog(@"插入排序后的结果:%@",[data description]);
56     [data release];
57 }
58 
59 -(void)quickSortWithArray:(NSArray *)aData{
60     NSMutableArray *data = [[NSMutableArrayalloc] initWithArray:aData];
61     [self quickSortWithArray:dataleft:0 right:[aData count]-1];
62     NSLog(@"快速排序后的结果:%@",[data description]);
63     [data release];
64     
65 }
66 
67 -(void)quickSortWithArray:(NSMutableArray *)aData left:(NSInteger)leftright:(NSInteger)right{
68     if (right > left) {
69         NSInteger i = left;
70         NSInteger j = right + 1;
71         while (true) {
72             while (i+1 < [aDatacount] && [aData objectAtIndex:++i] < [aData objectAtIndex:left]) ;
73             while (j-1 > -1&& [aData objectAtIndex:--j] > [aData objectAtIndex:left]) ;
74             if (i >= j) {
75                 break;
76             }
77             [self swapWithData:aDataindex1:i index2:j];
78         }
79         [self swapWithData:aDataindex1:left index2:j];
80         [self quickSortWithArray:aDataleft:left right:j-1];
81         [self quickSortWithArray:aDataleft:j+1 right:right];
82     }
83 }
84 
85 
86 -(void)dealloc{
87     [super dealloc];
88 }
89 
90 -(void)swapWithData:(NSMutableArray *)aData index1:(NSInteger)index1index2:(NSInteger)index2{
91     NSNumber *tmp = [aDataobjectAtIndex:index1];
92     [aData replaceObjectAtIndex:index1withObject:[aData objectAtIndex:index2]];
93     [aData replaceObjectAtIndex:index2withObject:tmp];
94 }
95 
96 @end

 技术分享

下面来一个例子测试一下:

 


 9 #import<Foundation/Foundation.h>
10 #import "Sort.h"
11 
12 #define kSize 20
13 #define kMax 100
14 
15 int main (int argc, const char * argv[])
16 {
17     
18     NSAutoreleasePool * pool =[[NSAutoreleasePool alloc] init];
19     
20     // insert code here...
21     NSLog(@"Hello, World!");
22     
23     NSMutableArray *data =[[NSMutableArray alloc] initWithCapacity:kSize];
24     
25     for (int i =0;i<kSize;i++) {
26         u_int32_t x = arc4random() %kMax;//0~kMax
27         NSNumber *num = [[NSNumberalloc] initWithInt:x];
28         [data addObject:num];
29         [num release];
30     }
31     
32     NSLog(@"排序前的数据:%@",[datadescription]);
33     
34     Sort *sort = [[Sort alloc] init];
35     [sort selectSortWithArray:data];
36     [sort insertSortWithArray:data];
37     [sort quickSortWithArray:data];
38     [sort release];
39     [data release];
40     [pool drain];
41     return 0;
42 }

总结Objective-c常用算法

标签:

原文地址:http://www.cnblogs.com/ZhiRongqun/p/4641563.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!