选择排序 //插入排序 //快速排序
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(@"选择排序后的结果:%@",[data 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(@"插入排序后的结果:%@",[data description]);
[data release];
}
-(void)quickSortWithArray:(NSArray *)aData{
NSMutableArray *data = [[NSMutableArray alloc] initWithArray:aData];
[self quickSortWithArray:data left:0 right:[aData count]-1];
NSLog(@"快速排序后的结果:%@",[data description]);
[data release];
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。