如何在Objective C中使用for循环对NSNumbers的可变数组进行排序?

时间:2014-06-27 09:45:11

标签: ios objective-c sorting loops

我尝试了这个,但我没有成功。任何人都可以解决这个问题吗?

 NSNumber *num1= [NSNumber numberWithInt:5];
    NSNumber *num2= [NSNumber numberWithInt:3];
    NSNumber *num3= [NSNumber numberWithInt:7];
    NSNumber *num4= [NSNumber numberWithInt:9];
    NSNumber *num5= [NSNumber numberWithInt:2];



    NSMutableArray *array =[NSMutableArray arrayWithObjects:num1,num2,num3,num4,num5, nil];

    int numLength = [array count];

    int tempValue;

    for (int i = 0; i < numLength; i++)
    {
        for(int j = 0; j < numLength; j++)
        {
            NSNumber *temp=[array objectAtIndex:i];
            int first =[temp integerValue];

            NSNumber *temp1=[array objectAtIndex:j+1];
            int second =[temp1 integerValue];

            if(first > second)

            {
                tempValue = second;

                [array replaceObjectAtIndex:j+1 withObject:temp];
                [array replaceObjectAtIndex:i withObject:temp1];

            }
        }
    }

    NSLog(@"Array is %@", array);

此代码位于ViewDidLoad ...

由于

4 个答案:

答案 0 :(得分:4)

试试这个,确实很有效:

NSMutableArray *_array = [NSMutableArray array];
for (int i = 0; i < 5; i++) { [_array addObject:@(arc4random()%10)]; }

NSLog(@"random: %@", _array);

for (int i = 0; i < _array.count; i++) {
    for(int j = i; j < _array.count; j++) {
        if (i != j) {
            NSInteger _first = [[_array objectAtIndex:i] integerValue];
            NSInteger _second = [[_array objectAtIndex:j] integerValue];
            if (_second < _first) {
                [_array exchangeObjectAtIndex:i withObjectAtIndex:j];
            }
        }
    }
}

NSLog(@"sorted : %@", _array);

注意:这种对数组元素进行排序的方法非常低效,建议仅用于教育目的。

答案 1 :(得分:0)

使用此代码

NSNumber *num1= [NSNumber numberWithInt:5];
NSNumber *num2= [NSNumber numberWithInt:3];
NSNumber *num3= [NSNumber numberWithInt:7];
NSNumber *num4= [NSNumber numberWithInt:9];
NSNumber *num5= [NSNumber numberWithInt:2];

NSMutableArray *array =[[NSMutableArray alloc] initWithObjects:num1,num2,num3,num4,num5,nil];

int numLength = [array count];
for (int i = 0; i < numLength-1; i++)
{
    for(int j = i+1; j < numLength; j++)
    {
        NSNumber *temp=[array objectAtIndex:i];
        int first =[temp integerValue];

        NSNumber *temp1=[array objectAtIndex:j];
        int second =[temp1 integerValue];

        if(first > second)
        {
            [array replaceObjectAtIndex:j withObject:temp];
            [array replaceObjectAtIndex:i withObject:temp1];
        }
    }
}

答案 2 :(得分:0)

试试这段代码......

NSNumber *num1= [NSNumber numberWithInt:5];
    NSNumber *num2= [NSNumber numberWithInt:3];
    NSNumber *num3= [NSNumber numberWithInt:7];
    NSNumber *num4= [NSNumber numberWithInt:9];
    NSNumber *num5= [NSNumber numberWithInt:2];

    NSMutableArray *array =[[NSMutableArray alloc] initWithObjects:num1,num2,num3,num4,num5,nil];
    int tempValue;

    int numLength = [array count];
    for (int i = 0; i < numLength-1; i++)
    {
        int j=0;
        for(; j < numLength-1; j++)
        {
            NSNumber *temp=[array objectAtIndex:i];
            int first =[temp integerValue];

            NSNumber *temp1=[array objectAtIndex:j];
            int second =[temp1 integerValue];

            if(first < second)
            {
                 tempValue = second;
                [array replaceObjectAtIndex:j withObject:temp];
                [array replaceObjectAtIndex:i withObject:temp1];
            }
        }
    }

答案 3 :(得分:0)

这就是着名的冒泡排序:

static void bubble_sort()
{
    NSMutableArray *array = [@[@5,@3,@7,@9,@2] mutableCopy];

    BOOL haveSwappedSomething;
    do
    {
        haveSwappedSomething = NO;
        for(int i=0; i<([array count]-1); i++) {
            NSNumber *num1 = array[i];
            NSNumber *num2 = array[i+1];
            if([num1 intValue] > [num2 intValue]) {
                array[i] = num2;
                array[i+1] = num1;
                haveSwappedSomething = YES;
            }
        }
    }
    while(haveSwappedSomething);

    NSLog(@"Array is %@", array);
}

时间复杂度:O(n^2)(最差情况和平均情况)(=&#34;蹩脚&#34;)

空间复杂度:O(1)(最糟糕的情况)(因为它就像快速排序的某些变体一样就位)(=&#34;优秀&#34;)

当然,最好使用quicksort,因为O(n log n) / O(1)关于时间/空间。