美文网首页
oc 排序(冒泡 选择 插入 希尔……)

oc 排序(冒泡 选择 插入 希尔……)

作者: 程序疯 | 来源:发表于2017-03-23 21:59 被阅读0次

    - (void)viewDidLoad {

    [superviewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    //冒泡

    //[self storyBuddleByAray:[self creatStoryAryAndStart:0 andEnd:60000 andLength:50000] andDes:0];

    //选择

    //[self selectStroyByArray:[self creatStoryAryAndStart:0 andEnd:1000000 andLength:100000] andDes:0];

    //插入排序

    [selfinsterStoryByArray:[selfcreatStoryAryAndStart:0andEnd:1000000andLength:100000]andDesc:0];

    //希尔排序

    [selfhillStoryArray:[selfcreatStoryAryAndStart:0andEnd:1000000andLength:100000]andSetp:6andDesc:0];

    }

    //选择排序

    - (void)selectStroyByArray:(NSMutableArray*)array andDes:(int)desc

    {

    if(array ==nil|| array.count==0)return;

    NSLog(@"选择");

    [selfprintCurrectTimeAndStartOrEndtimer:0];

    for(inti =0; i < array.count; i++) {

    intcurrectIndex = i;

    for(intj = i +1; j < array.count; j++)

    {

    if([array[j]compare:array[currectIndex]] ==NSOrderedDescending)

    {

    [arrayexchangeObjectAtIndex:jwithObjectAtIndex:currectIndex];

    }

    }

    }

    [selfprintCurrectTimeAndStartOrEndtimer:1];

    }

    //冒泡排序

    - (void)storyBuddleByAray:(NSMutableArray*)array andDes:(int)desc

    {

    if(array.count==0|| array ==nil)return;

    [selfprintCurrectTimeAndStartOrEndtimer:0];

    for(inti =1; i< array.count; i++)

    {

    for(intj =0; j < array.count- i ; j++)

    {

    if([array[j]compare:array[j+1]] ==NSOrderedAscending)

    {

    [arrayexchangeObjectAtIndex:j+1withObjectAtIndex:j];

    }

    }

    }

    [selfprintCurrectTimeAndStartOrEndtimer:1];

    [selfprintStoryArray:arrayandFixIndex:0];

    }

    //插入排序

    - (void)insterStoryByArray:(NSMutableArray*)array andDesc:(int)NSSortDescriptor

    {

    if(array.count==0|| array ==nil)return;

    NSLog(@"插入排序开始");

    NSDate*startDate = [NSDatedate];

    for(inti =0; i < array.count; i++)

    {

    for(intj = i ; j >0; j--)

    {

    NSString*fixStr = [NSStringstringWithFormat:@"%@",array[j]];

    NSString*pixStr = [NSStringstringWithFormat:@"%@",array[j-1]];

    if(fixStr.integerValue<= pixStr.integerValue)break;//判断当前结束的条件

    elseif(fixStr.integerValue> pixStr.integerValue)

    {

    [arrayexchangeObjectAtIndex:jwithObjectAtIndex:(j-1)];

    }

    }

    }

    NSDate*endDate = [NSDatedate];

    [selfjudeTimeScendByStartTime:startDateandEndTime:endDate];

    }

    //希尔排序

    - (void)hillStoryArray:(NSMutableArray*)list andSetp:(int)currectSetp andDesc:(int)desc

    {

    if(0== list.count||nil== list)return;

    NSLog(@"希尔排序");

    NSDate*date=[NSDatedate];

    intgap = (int)[listcount] / currectSetp;

    while(gap >=1) {

    for(inti = gap ; i < [listcount]; i++){

    NSIntegertemp = [[listobjectAtIndex:i]intValue];

    intj = i;

    while(j >= gap && temp < [[listobjectAtIndex:(j - gap)]intValue]) {

    //替换而不是互换值

    [listreplaceObjectAtIndex:jwithObject:[listobjectAtIndex:j-gap]];

    j -= gap;

    }

    //替换而不是互换值

    [listreplaceObjectAtIndex:jwithObject:@(temp)];

    }

    gap = gap / currectSetp;

    }

    NSDate*endDate = [NSDatedate];

    [selfjudeTimeScendByStartTime:dateandEndTime:endDate];

    }

    /**

    生成数组

    @param startInt开始位置

    @param endInt结束位置

    @param aryCount长度

    @return返回数组

    */

    - (NSMutableArray*)creatStoryAryAndStart:(int)startInt andEnd:(int)endInt andLength:(int)aryCount

    {

    NSMutableArray*array = [NSMutableArrayarrayWithCapacity:0];

    for(inti =0; i < aryCount; i++)

    {

    intindex =arc4random() % (endInt - startInt +1) + startInt;

    NSString*objectStr = [NSStringstringWithFormat:@"%d",index];

    [arrayaddObject:objectStr];

    }

    returnarray;

    }

    - (void)printCurrectTimeAndStartOrEndtimer:(int)str

    {

    NSDate*date=[NSDatedate];//获取当前时间

    NSDateFormatter*format1=[[NSDateFormatteralloc]init];

    [format1setDateFormat:@"yyyy/MM/dd HH:mm:ss"];

    NSString*str1=[format1stringFromDate:date];

    NSString*fixStr = str ?@"结束":@"开始";

    NSLog(@"-%@时间-%@-----",fixStr,str1);

    }

    - (void)judeTimeScendByStartTime:(NSDate*)startDate andEndTime:(NSDate*)endDate

    {

    UInt64msecond1 = [startDatetimeIntervalSince1970];

    UInt64msecond2 = [endDatetimeIntervalSince1970];

    NSLog(@"%llu", msecond1 - msecond2);

    }

    - (void)printStoryArray:(NSMutableArray*)array andFixIndex:(int)fixInt

    {

    NSString*fixStr = fixInt ==0?@"排序完成":@"排序中";

    NSString*compStr = [arraycomponentsJoinedByString:@" "];

    NSLog(@"%@ : %@",fixStr,compStr);

    }

    - (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

    }

    相关文章

      网友评论

          本文标题:oc 排序(冒泡 选择 插入 希尔……)

          本文链接:https://www.haomeiwen.com/subject/duzowttx.html