用oc写了几个排序算法,没有特殊的修改,比较容易理解,给大家参考一下。
/*
***选择排序
*/
- (NSArray*)choose:(NSArray *)arr{
if (arr.count < 2) {
return nil;
}
if (arr.count == 2) {
return nil;
}
NSMutableArray *muArr = [NSMutableArray arrayWithArray:arr];
for (int i = 0; i < muArr.count - 1 ; i ++) {
int temp = i + 1;
for (int j = i + 1; j < muArr.count - 1; j ++) {
if ([muArr[j + 1] intValue] <= [muArr[temp] intValue]) {
temp = j + 1;
}
}
if ([muArr[i] intValue] > [muArr[temp] intValue]) {
NSString *tempStr = muArr[i];
muArr[i] = muArr[temp];
muArr[temp] = tempStr;
}
}
return muArr;
}
/*
***插入排序
*/
- (NSArray*)insert:(NSArray*)arr{
NSMutableArray *muArr = [NSMutableArray arrayWithArray:arr];
for (int i = 0; i < arr.count; i ++) {
for (int j = i - 1; j >= 0; j --) {
if ([muArr[i] intValue] <= [muArr[j] intValue] && [muArr[i] intValue] >= [muArr[j - 1] intValue]) {
NSString *tempStr = muArr[i];
[muArr removeObjectAtIndex:i];
[muArr insertObject:tempStr atIndex:j];
break;
}
}
}
return muArr;
}
//快速排序
- (NSArray*)quick:(NSArray*)arr{
if (arr.count == 1) {
return arr;
}
if (arr.count == 2) {
if ([arr[0] intValue] > [arr[1] intValue]) {
return @[arr[1],arr[0]];
}
else{
return arr;
}
}
NSMutableArray *muArr1 = [NSMutableArray arrayWithArray:@[]];
NSMutableArray *muArr2 = [NSMutableArray arrayWithArray:@[]];
for (int i = 0; i < arr.count ; i ++) {
if ([arr[i] intValue] <= [arr[arr.count / 2] intValue]) {
[muArr1 addObject:arr[i]];
}
else {
[muArr2 addObject:arr[i]];
}
}
if (muArr1.count == arr.count) {
[muArr2 addObject:arr[arr.count / 2]];
[muArr1 removeObjectAtIndex:(arr.count / 2)];
}
else if (muArr2.count == arr.count){
[muArr1 addObject:arr[arr.count / 2]];
[muArr2 removeObjectAtIndex:(arr.count / 2)];
}
NSArray *arr1 = [self quick:muArr1];
NSArray *arr2 = [self quick:muArr2];
NSMutableArray *muarr = [NSMutableArray array];
for (id temp in arr1) {
[muarr addObject:temp];
}
for (id temp in arr2) {
[muarr addObject:temp];
}
return muarr;
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
NSArray *frontArr = @[@"G",@"D",@"A",@"F",@"E",@"M",@"H",@"Z"];
NSArray *midArr = @[@"A",@"D",@"E",@"F",@"G",@"H",@"M",@"Z"];
NSLog(@"%@",[self sortTree:frontArr and:midArr]);
}
//前序遍历:G DAFE MHZ
//中序遍历:ADEF G HMZ
// G
// D M
// A F H Z
// E
//后序遍历:AEFDHZMG
- (NSArray*)sortTree:(NSArray *)frontArr and:(NSArray*)midArr{
if (frontArr.count != midArr.count || frontArr.count == 0) {
return nil;
}
//拿到root节点。
NSString *rootPoint = frontArr[0];
//通过root节点和中序遍历结果,切割出中序遍历左右子树。(不含root节点)
NSMutableArray *midLeftArr = [NSMutableArray array];
NSMutableArray *midRightArr = [NSMutableArray array];
int aNum = (int)[midArr indexOfObject:rootPoint];
for (int i = 0; i < midArr.count ; i ++) {
if (i < aNum) {
[midLeftArr addObject:midArr[i]];
}
else if (i > aNum) {
[midRightArr addObject:midArr[i]];
}
}
//这里通过上面求出来左右子树的节点数,划分出前序遍历的左右子树。
NSArray *frontLeftArr = [frontArr subarrayWithRange:NSMakeRange(1, midLeftArr.count)];
NSArray *frontRightArr = [frontArr subarrayWithRange:NSMakeRange(1 + midLeftArr.count, midRightArr.count)];
//递归求值,(把左右子树分别看作单独的二叉树进行递归)
NSArray *lArr = [self sortTree:frontLeftArr and:midLeftArr];
NSArray *rArr = [self sortTree:frontRightArr and:midRightArr];
//我们知道了左子树和右子树还有节点,然后我们重新拼接一下就是我们需要的后序遍历了。
NSMutableArray *sortArr = [NSMutableArray array];
for (int i = 0; i < lArr.count; i ++) {
[sortArr addObject:lArr[i]];
}
for (int i = 0; i < rArr.count; i ++) {
[sortArr addObject:rArr[i]];
}
//别忘了root节点跟在最后面
[sortArr addObject:rootPoint];
//返回结果就是了
return sortArr;
}
网友评论