OC | 小九九:排序算法

2018-08-17  本文已影响79人  佳小豆

前些日子看到大佬写的排序算法温习,突然意识到自己早已经把这个抛到九霄云外了,所以,也想着来复习一下。
在开始记录排序算法之前,我们先来定义排序规则为从小到大排序,需要排序的数组如下:
NSMutableArray *numArr = [NSMutableArray arrayWithObjects:@(87),@(63.3),@(99.8),@(56),@(43.3), nil];

交换排序

1.冒泡排序

#pragma mark - 冒泡排序
+ (void)bubbleSort:(NSMutableArray *)array
{
    for (NSUInteger i = 0; i < array.count; i++)
    {
        for (NSUInteger j = 0; j < array.count-1-i; j++)
        {
            //相邻两个值进行比较
            if ([array[j] floatValue] > [array[j+1] floatValue])
            {
                [array exchangeObjectAtIndex:j withObjectAtIndex:j+1];
            }
        }
        NSLog(@"冒泡排序第%ld趟:%@",i+1,array);
    }
}

2.快速排序

#pragma mark - 快速排序
+ (void)quickSort:(NSMutableArray *)array leftIndex:(NSInteger)leftIndex rightIndex:(NSInteger)rightIndex
{
    if (leftIndex >= rightIndex)
    {
        return ;
    }
    
    NSInteger i = leftIndex;
    NSInteger j = rightIndex;
    
    CGFloat standardValue = [array[i] floatValue];
    
    while (i < j)
    {
        while (i < j && [array[j] floatValue] >= standardValue)
        {
            j--;
        }
        //如果比基准数小,则将查找到的小值调换到i的位置
        array[i] = array[j];
       
        while (i < j && [array[i] floatValue] <= standardValue)
        {
            i++;
        }
        //如果比基准数大,则将查找到的大值调换到j的位置
        array[j] = array[i];
    }

    array[i] = [NSNumber numberWithFloat:standardValue];
   
    //排序基准数左边的
    [self quickSort:array leftIndex:leftIndex rightIndex:i-1];
    //排序基准数右边的
    [self quickSort:array leftIndex:i+1 rightIndex:rightIndex];
}

插入排序

1.直接插入排序

#pragma mark - 直接插入排序
+ (void)insertSort:(NSMutableArray *)array
{
    NSInteger j;
    for (NSInteger i = 1; i < array.count; i++)
    {
        CGFloat tempValue = [array[i] floatValue];
        for (j = i-1; j >= 0 && [array[j] floatValue] > tempValue; j--)
        {
            [array exchangeObjectAtIndex:j+1 withObjectAtIndex:j];
        }
        [array replaceObjectAtIndex:j+1 withObject:[NSNumber numberWithFloat:tempValue]];
        NSLog(@"直接插入第%ld趟:%@",i,array);
    }
}

2.二分(折半)插入排序

#pragma mark - 二分(折半)插入排序
+ (void)halfInsertSort:(NSMutableArray *)array
{
    for (NSInteger i = 1 ; i < array.count ; i++)
    {
        CGFloat tempValue = [[array objectAtIndex:i] floatValue];
        NSInteger left = 0;
        NSInteger right = i - 1;
        
        while (left <= right)
        {
            CGFloat middle = (left + right) / 2;
            
            if(tempValue < [[array objectAtIndex:middle] floatValue])
            {
                right = middle - 1;
            }
            else
            {
                left = middle + 1;
            }
        }
        for(NSInteger j = i ; j > left; j--)
        {
            [array replaceObjectAtIndex:j withObject:[array objectAtIndex:j - 1]];
        }
        [array replaceObjectAtIndex:left withObject:[NSNumber numberWithFloat:tempValue]];
        NSLog(@"折半插入第%ld趟:%@",i,array);
    }
}

选择排序

1.直接选择排序

#pragma mark - 直接选择排序
+ (void)selectSort:(NSMutableArray *)array
{
    for (NSInteger i = 0; i < array.count; i++)
    {
        NSInteger minIndex = i;
        for (NSInteger j = i+1; j < array.count; j++)
        {
            if ([array[j] floatValue] < [array[minIndex] floatValue])
            {
                minIndex = j;
            }
        }
        if (minIndex != i)
        {
            [array exchangeObjectAtIndex:i withObjectAtIndex:minIndex];
        }
        NSLog(@"直接选择第%ld趟:%@",i+1,array);
    }
}
上一篇下一篇

猜你喜欢

热点阅读