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