排序算法c语言和oc实现的,排序算法(OC实现)

本文介绍使用Objective-C实现的选择排序、插入排序及快速排序三种基本排序算法,并通过具体实例展示了这些算法的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转载自:51cto

本文采用objective-c实现常见的排序算法:选择排序,插入排序,快速排序。

//

//  Sort.h

//  Algorithm

//

//  Created by 张

汉国 on 11-9-30.

//  Copyright

2011年 __MyCompanyName__. All rights reserved.

//

#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

//

//  Sort.m

//  Algorithm

//

//  Created by 张

汉国 on 11-9-30.

//  Copyright

2011年 __MyCompanyName__. All rights reserved.

//

#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 =

http://www.cnblogs.com/halzhang/archive/2011/10/07/[[NSMutableArray

alloc]initWithArray:aData];

for (int i=0; i

int m

=i;

for (int j

=i+1; j

if ([data objectAtIndex:j]

< [data objectAtIndex:m]) {

m = j;

}

}

if (m != i)

{

[self swapWithData:data

index1:m index2:i];

}

}

NSLog(@"选择排序后的结果:%@",[data description]);

[data release];

}

-(void)insertSortWithArray:(NSArray

*)aData{

NSMutableArray *data =

http://www.cnblogs.com/halzhang/archive/2011/10/07/[[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(@"插入排序后的结果:%@",[data description]);

[data release];

}

-(void)quickSortWithArray:(NSArray

*)aData{

NSMutableArray *data =

http://www.cnblogs.com/halzhang/archive/2011/10/07/[[NSMutableArray

alloc] initWithArray:aData];

[self quickSortWithArray:data left:0 right:[aData

count]-1];

NSLog(@"快速排序后的结果:%@",[data 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

测试例子:

//

//  main.m

//  Algorithm

//

//  Created by 张

汉国 on 11-9-30.

//  Copyright

2011年 __MyCompanyName__. All rights reserved.

//

#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 =

http://www.cnblogs.com/halzhang/archive/2011/10/07/[[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(@"排序前的数据:%@",[data description]);

Sort *sort = [[Sort alloc] init];

[sort selectSortWithArray:data];

[sort insertSortWithArray:data];

[sort quickSortWithArray:data];

[sort release];

[data release];

[pool drain];

return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值