標籤:
今天是星期天,想睡到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常用演算法