-(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(@"选择排序后的结果:%@",[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];
}
选择排序 //插入排序 //快速排序
原文:http://blog.csdn.net/longshiqing14/article/details/45170483