Chinaunix首页 | 论坛 | 博客
  • 博客访问: 360385
  • 博文数量: 78
  • 博客积分: 3380
  • 博客等级: 中校
  • 技术积分: 857
  • 用 户 组: 普通用户
  • 注册时间: 2010-06-16 19:39
文章分类

全部博文(78)

文章存档

2011年(31)

2010年(47)

分类: BSD

2011-11-11 08:41:01

#import
 @interface Sort : NSObject{
 
 }
 //选择排序
 -(void)selectSortWithArray:(NSArray *)aData;
 //插入排序
 -(void)insertSortWithArray:(NSArray *)aData;
 //快速排序
 -(void)quickSortWithArray:(NSArray *)aData;
 -(void)swapWithData:(NSMutableArray *)aData index1:(NSInteger)index1 index2:(NSInteger)index2;
 
 
 @end
 #import "Sort.h"
 
 @interface Sort()
 -(void)quickSortWithArray:(NSArray *)aData left:(NSInteger)left right:(NSInteger)right;
 @end
 
 @implementation Sort
 
 - (id)init
 {
     self = [super init];
     if (self) {
         // Initialization code here.
     }
    
     return self;
 }
 
 -(void)selectSortWithArray:(NSArray *)aData{
     NSMutableArray *data = [[NSMutableArray alloc]initWithArray:aData];
     for (int i=0; i<[data count]-1; i++) {
         int m =i;
         for (int j =i+1; j<[data count]; j++) {
             if ([data objectAtIndex:j] < [data objectAtIndex:m]) {
                 m = j;
             }
         }
         if (m != i) {
             [self swapWithData:data index1:m index2:i];
         }
     }
     NSLog(@"选择排序后的结果: description]);
     [data release];
 }
 
 -(void)insertSortWithArray:(NSArray *)aData{
     NSMutableArray *data = [[NSMutableArray alloc]initWithArray:aData];
     for (int i = 1; i < [data count]; i++) {
         id tmp = [data objectAtIndex:i];
         int j = i-1;
         while (j != -1 && [data objectAtIndex:j] > tmp) {
             [data replaceObjectAtIndex:j+1 withObject:[data objectAtIndex:j]];
             j--;
         }
         [data replaceObjectAtIndex:j+1 withObject:tmp];
     }
     NSLog(@"插入排序后的结果: description]);
     [data release];
 }
 
 -(void)quickSortWithArray:(NSArray *)aData{
     NSMutableArray *data = [[NSMutableArray alloc] initWithArray:aData];
     [self quickSortWithArray:data left:0 right:[aData count]-1];
     NSLog(@"快速排序后的结果: description]);
     [data release];
    
 }
 
 -(void)quickSortWithArray:(NSMutableArray *)aData left:(NSInteger)left right:(NSInteger)right
 {
     if (right > left)
     {
         NSInteger i = left;
         NSInteger j = right + 1;
         while (true)
         {
             while (i+1 < [aData count] && [aData objectAtIndex:++i] < [aData objectAtIndex:left]) ;
             while (j-1 > -1 && [aData objectAtIndex:--j] > [aData objectAtIndex:left]) ;
             if (i >= j) {
                 break;
             }
             [self swapWithData:aData index1:i index2:j];
         }
         [self swapWithData:aData index1:left index2:j];
         [self quickSortWithArray:aData left:left right:j-1];
         [self quickSortWithArray:aData left:j+1 right:right];
     }
 }
 
 
 -(void)dealloc{
     [super dealloc];
 }
 -(void)swapWithData:(NSMutableArray *)aData index1:(NSInteger)index1 index2:(NSInteger)index2{
     NSNumber *tmp = [aData objectAtIndex:index1];
     [aData replaceObjectAtIndex:index1 withObject:[aData objectAtIndex:index2]];
     [aData replaceObjectAtIndex:index2 withObject:tmp];
 }
 
 @end
测试例子:
 #import
 #import "Sort.h"
 
 #define kSize 20
 #define kMax 100
 
 int main (int argc, const char * argv[])
 {
    
     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    
     // insert code here...
     NSLog(@"Hello, World!");
    
     NSMutableArray *data = [[NSMutableArray alloc] initWithCapacity:kSize];
    
     for (int i =0;i         u_int32_t x = arc4random() % kMax;//0~kMax
         NSNumber *num = [[NSNumber alloc] initWithInt:x];
         [data addObject:num];
         [num release];
     }
    
     NSLog(@"排序前的数据: description]);
    
     Sort *sort = [[Sort alloc] init];
     [sort selectSortWithArray:data];
     [sort insertSortWithArray:data];
     [sort quickSortWithArray:data];
     [sort release];
     [data release];
     [pool drain];
    return 0;
 }
阅读(2447) | 评论(0) | 转发(0) |
0

上一篇:检查网络是否存在

下一篇:没有了

给主人留下些什么吧!~~