iOS希尔排序算法

来源:互联网 发布:阿里云服务器外网ip 编辑:程序博客网 时间:2024/06/06 02:35
#import <Foundation/Foundation.h>@interface ShellSort : NSObject/** 希尔排序 @param list 被排序的数组 */+ (void)shellSort:(NSMutableArray *)list;@end
#import "ShellSort.h"@implementation ShellSort+ (void)shellSort:(NSMutableArray *)list{    int gap = [list count]/2.0;    while (gap>=1) {        for(int i =0;i<[list count];i++){            NSInteger temp = [[list objectAtIndex:i] integerValue];            int j =i;            while (j>=gap && temp <[[list objectAtIndex:(j-gap)] integerValue]) {                [list replaceObjectAtIndex:j withObject:[list objectAtIndex:j-gap]];                j-=gap;            }            [list replaceObjectAtIndex:j withObject:[NSNumber numberWithInteger:temp]];                    }    }        }@end


0 0
原创粉丝点击