object-凯发app官方网站

凯发app官方网站-凯发k8官网下载客户端中心 | | 凯发app官方网站-凯发k8官网下载客户端中心
  • 博客访问: 339013
  • 博文数量: 78
  • 博客积分: 3380
  • 博客等级: 中校
  • 技术积分: 857
  • 用 户 组: 普通用户
  • 注册时间: 2010-06-16 19:39
文章分类

(78)

  • (0)
  • (3)
  • (1)
  • (7)
  • (3)
  • (0)
  • (0)
  • (1)
  • (6)
  • (5)
  • (26)
  • (1)
  • (0)
  • (6)
  • (18)
  • (1)
文章存档

(31)

(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;
 }
阅读(2278) | 评论(0) | 转发(0) |
0

上一篇:

下一篇:没有了

给主人留下些什么吧!~~
")); function link(t){ var href= $(t).attr('href'); href ="?url=" encodeuricomponent(location.href); $(t).attr('href',href); //setcookie("returnouturl", location.href, 60, "/"); }
网站地图