Objective-C 冒泡 选择 插入 快速排序

来源:互联网 发布:苹果7手机用不了网络 编辑:程序博客网 时间:2024/05/16 04:43
首先是header文件: 
Objective-c代码  收藏代码
  1. #import <Foundation/Foundation.h>  
  2.   
  3. @interface Sort : NSObject{  
  4.       
  5. }  
  6. //冒泡排序  
  7. -(void)bunbleSortWithArray:(NSArray *)aData;  
  8. //选择排序  
  9. -(void)selectSortWithArray:(NSArray *)aData;  
  10. //插入排序  
  11. -(void)insertSortWithArray:(NSArray *)aData;  
  12. //快速排序,对冒泡排序的一种改进  
  13. -(void)quickSortWithArray:(NSArray *)aData;  
  14. -(void)swapWithData:(NSMutableArray *)aData index1:(NSInteger)index1 index2:(NSInteger)index2;  
  15. @end  

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

然后写main函数测试: 
Objective-c代码  收藏代码
  1. #import <Foundation/Foundation.h>  
  2. #import "Sort.h"  
  3.   
  4. #define kSize 20  
  5. #define kMax 100  
  6.   
  7. int main (int argc, const char * argv[])  
  8. {  
  9.       
  10.     // insert code here...  
  11.     NSLog(@"Hello, World!");  
  12.       
  13.     NSMutableArray *data = [[NSMutableArray alloc] initWithCapacity:kSize];  
  14.       
  15.     for (int i =0;i<kSize;i++) {  
  16.         u_int32_t x = arc4random() % kMax;//0~kMax  
  17.         NSNumber *num = [[NSNumber alloc] initWithInt:x];  
  18.         [data addObject:num];  
  19.     }  
  20.       
  21.     NSLog(@"排序前的数据:%@",[data description]);  
  22.       
  23.     Sort *sort = [[Sort alloc] init];  
  24.     [sort bunbleSortWithArray:data];  
  25.     [sort selectSortWithArray:data];  
  26.     [sort insertSortWithArray:data];  
  27.     [sort quickSortWithArray:data];  
  28.     return 0;  
  29. }  
 转自http://jasonshieh.iteye.com/blog/2022553
0 0