Objective-C实现常用的4种排序算法

OC实现的4种排序又来了!

4种排序分别是:快速排序、冒泡排序、选择排序、插入排序,其他的我就不写了,因为OC里的数组中不能存放基本数据类型,如int不能存放,只能放对象,所以所有的数据我用了NSNumber类型,一开始我直接用>、=、<来比较结果排序后还是乱七八糟,后来想起来不能这么比较,对象的比较,可以用compare方法,结果与NSComparisonResult这个枚举类型的数据比较大小就可以了。或者取NSNumber 的intValue,在用>、=、<进行比较,第一个方法中有些两种方式的语句,后来的类似就不写了。

1、快速排序

#pragma - mark 快速排序
+ (void)quickSort:(NSMutableArray *)array low:(int)low high:(int)high
{
    if(array == nil || array.count == 0){
        return;
    }
    if (low >= high) {
        return;
    }
    
    //取中值
    int middle = low + (high - low)/2;
    NSNumber *prmt = array[middle];
    int i = low;
    int j = high;
    
    //开始排序,使得left<prmt 同时right>prmt
    while (i <= j) {
//        while ([array[i] compare:prmt] == NSOrderedAscending) {  该行与下一行作用相同
        while ([array[i] intValue] < [prmt intValue]) {
            i++;
        }
//        while ([array[j] compare:prmt] == NSOrderedDescending) { 该行与下一行作用相同
        while ([array[j] intValue] > [prmt intValue]) {
            j--;
        }
        
        if(i <= j){
            [array exchangeObjectAtIndex:i withObjectAtIndex:j];
            i++;
            j--;
        }
        
        printf("排序中:");
        [self printArray:array];
    }
    
    if (low < j) {
        [self quickSort:array low:low high:j];
    }
    if (high > i) {
        [self quickSort:array low:i high:high];
    }
}
快速排序的过程如下:

排序前:9 2 10 7 3 7 4 
排序中:4 2 10 7 3 7 9 
排序中:4 2 7 7 3 10 9 
排序中:4 2 7 3 7 10 9 
排序中:2 4 7 3 7 10 9 
排序中:2 4 3 7 7 10 9 
排序中:2 3 4 7 7 10 9 
排序中:2 3 4 7 7 9 10 
排序中:2 3 4 7 7 9 10 
排序后:2 3 4 7 7 9 10
2、冒泡排序

#pragma - mark 冒泡排序
+ (void)buddleSort:(NSMutableArray *)array
{
    if(array == nil || array.count == 0){
        return;
    }
    
    for (int i = 1; i < array.count; i++) {
        for (int j = 0; j < array.count - i; j++) {
            if ([array[j] compare:array[j+1]] == NSOrderedDescending) {
                [array exchangeObjectAtIndex:j withObjectAtIndex:j+1];
            }
            
            printf("排序中:");
            [self printArray:array];
        }
    }

}
冒泡排序的过程如下:

排序前:9 2 10 7 3 7 4 
排序中:2 9 10 7 3 7 4 
排序中:2 9 10 7 3 7 4 
排序中:2 9 7 10 3 7 4 
排序中:2 9 7 3 10 7 4 
排序中:2 9 7 3 7 10 4 
排序中:2 9 7 3 7 4 10 
排序中:2 9 7 3 7 4 10 
排序中:2 7 9 3 7 4 10 
排序中:2 7 3 9 7 4 10 
排序中:2 7 3 7 9 4 10 
排序中:2 7 3 7 4 9 10 
排序中:2 7 3 7 4 9 10 
排序中:2 3 7 7 4 9 10 
排序中:2 3 7 7 4 9 10 
排序中:2 3 7 4 7 9 10 
排序中:2 3 7 4 7 9 10 
排序中:2 3 7 4 7 9 10 
排序中:2 3 4 7 7 9 10 
排序中:2 3 4 7 7 9 10 
排序中:2 3 4 7 7 9 10 
排序中:2 3 4 7 7 9 10 
排序后:2 3 4 7 7 9 10
3、选择排序

+ (void)selectSort:(NSMutableArray *)array
{
    if(array == nil || array.count == 0){
        return;
    }
    
    int min_index;
    for (int i = 0; i < array.count; i++) {
        min_index = i;
        for (int j = i + 1; j<array.count; j++) {
            if ([array[j] compare:array[min_index]] == NSOrderedAscending) {
                [array exchangeObjectAtIndex:j withObjectAtIndex:min_index];
            }
            
            printf("排序中:");
            [self printArray:array];
        }
    }
}
选择排序的过程如下:

排序前:9 2 10 7 3 7 4 
排序中:2 9 10 7 3 7 4 
排序中:2 9 10 7 3 7 4 
排序中:2 9 10 7 3 7 4 
排序中:2 9 10 7 3 7 4 
排序中:2 9 10 7 3 7 4 
排序中:2 9 10 7 3 7 4 
排序中:2 9 10 7 3 7 4 
排序中:2 7 10 9 3 7 4 
排序中:2 3 10 9 7 7 4 
排序中:2 3 10 9 7 7 4 
排序中:2 3 10 9 7 7 4 
排序中:2 3 9 10 7 7 4 
排序中:2 3 7 10 9 7 4 
排序中:2 3 7 10 9 7 4 
排序中:2 3 4 10 9 7 7 
排序中:2 3 4 9 10 7 7 
排序中:2 3 4 7 10 9 7 
排序中:2 3 4 7 10 9 7 
排序中:2 3 4 7 9 10 7 
排序中:2 3 4 7 7 10 9 
排序中:2 3 4 7 7 9 10 
排序后:2 3 4 7 7 9 10
4、插入排序

#pragma - mark 插入排序
+ (void)inserSort:(NSMutableArray *)array
{
    if(array == nil || array.count == 0){
        return;
    }
    
    for (int i = 0; i < array.count; i++) {
        NSNumber *temp = array[i];
        int j = i-1;
        
        while (j >= 0 && [array[j] compare:temp] == NSOrderedDescending) {
            [array replaceObjectAtIndex:j+1 withObject:array[j]];
            j--;
            
            printf("排序中:");
            [self printArray:array];
        }
        
        [array replaceObjectAtIndex:j+1 withObject:temp];
    }
}
插入排序的过程如下:

排序前:9 2 10 7 3 7 4 
排序中:9 9 10 7 3 7 4 
排序中:2 9 10 10 3 7 4 
排序中:2 9 9 10 3 7 4 
排序中:2 7 9 10 10 7 4 
排序中:2 7 9 9 10 7 4 
排序中:2 7 7 9 10 7 4 
排序中:2 3 7 9 10 10 4 
排序中:2 3 7 9 9 10 4 
排序中:2 3 7 7 9 10 10 
排序中:2 3 7 7 9 9 10 
排序中:2 3 7 7 7 9 10 
排序中:2 3 7 7 7 9 10 
排序后:2 3 4 7 7 9 10 
另外,类的代码也附上吧!

//
//  SortUtil.h
//  SortUtil
//
//  Created by Mac on 14-4-17.
//  Copyright (c) 2014年 KnightKing. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface SortUtil : NSObject

//快速排序
+ (void)quickSort:(NSMutableArray *)array low:(int)low high:(int)high;

//冒泡排序
+ (void)buddleSort:(NSMutableArray *)array;

//选择排序
+ (void)selectSort:(NSMutableArray *)array;

//插入排序
+ (void)inserSort:(NSMutableArray *)array;

//打印数组
+ (void)printArray:(NSArray *)array;

@end

//
//  SortUtil.m
//  SortUtil
//
//  Created by Mac on 14-4-17.
//  Copyright (c) 2014年 KnightKing. All rights reserved.
//

#import "SortUtil.h"

@implementation SortUtil

#pragma - mark 快速排序
+ (void)quickSort:(NSMutableArray *)array low:(int)low high:(int)high
{
    if(array == nil || array.count == 0){
        return;
    }
    if (low >= high) {
        return;
    }
    
    //取中值
    int middle = low + (high - low)/2;
    NSNumber *prmt = array[middle];
    int i = low;
    int j = high;
    
    //开始排序,使得left<prmt 同时right>prmt
    while (i <= j) {
//        while ([array[i] compare:prmt] == NSOrderedAscending) {  该行与下一行作用相同
        while ([array[i] intValue] < [prmt intValue]) {
            i++;
        }
//        while ([array[j] compare:prmt] == NSOrderedDescending) { 该行与下一行作用相同
        while ([array[j] intValue] > [prmt intValue]) {
            j--;
        }
        
        if(i <= j){
            [array exchangeObjectAtIndex:i withObjectAtIndex:j];
            i++;
            j--;
        }
        
        printf("排序中:");
        [self printArray:array];
    }
    
    if (low < j) {
        [self quickSort:array low:low high:j];
    }
    if (high > i) {
        [self quickSort:array low:i high:high];
    }
}

#pragma - mark 冒泡排序
+ (void)buddleSort:(NSMutableArray *)array
{
    if(array == nil || array.count == 0){
        return;
    }
    
    for (int i = 1; i < array.count; i++) {
        for (int j = 0; j < array.count - i; j++) {
            if ([array[j] compare:array[j+1]] == NSOrderedDescending) {
                [array exchangeObjectAtIndex:j withObjectAtIndex:j+1];
            }
            
            printf("排序中:");
            [self printArray:array];
        }
    }

}

#pragma - mark 选择排序
+ (void)selectSort:(NSMutableArray *)array
{
    if(array == nil || array.count == 0){
        return;
    }
    
    int min_index;
    for (int i = 0; i < array.count; i++) {
        min_index = i;
        for (int j = i + 1; j<array.count; j++) {
            if ([array[j] compare:array[min_index]] == NSOrderedAscending) {
                [array exchangeObjectAtIndex:j withObjectAtIndex:min_index];
            }
            
            printf("排序中:");
            [self printArray:array];
        }
    }
}

#pragma - mark 插入排序
+ (void)inserSort:(NSMutableArray *)array
{
    if(array == nil || array.count == 0){
        return;
    }
    
    for (int i = 0; i < array.count; i++) {
        NSNumber *temp = array[i];
        int j = i-1;
        
        while (j >= 0 && [array[j] compare:temp] == NSOrderedDescending) {
            [array replaceObjectAtIndex:j+1 withObject:array[j]];
            j--;
            
            printf("排序中:");
            [self printArray:array];
        }
        
        [array replaceObjectAtIndex:j+1 withObject:temp];
    }
}

+ (void)printArray:(NSArray *)array
{
    for(NSNumber *number in array) {
        printf("%d ",[number intValue]);
    }
    
    printf("\n");
}

@end

调用我就写在了app启动的方法里:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    NSMutableArray *array = [NSMutableArray arrayWithObjects:@9,@2,@10,@7,@3,@7,@4,nil];
    
    printf("排序前:");
    [SortUtil printArray:array];
    //快速排序
//    [SortUtil quickSort:array low:0 high:6];
    //冒泡排序
//    [SortUtil buddleSort:array];
    //选择排序
//    [SortUtil selectSort:array];
    //插入排序
    [SortUtil inserSort:array];
    
    printf("排序后:");
    [SortUtil printArray:array];
    
    return YES;
}


  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值