Getting Top 10 Highest Numbers From Array?

感情迁移 提交于 2019-12-02 07:17:18

Easiest way, is to sort the array in Descending order, and then grab the first 10 indexes

Or if they are inside dictionaries, iterate the dictionary allValues, grab all the arrays, add all the elements inside a common array, and sort that

It seems as if the data structure you want to end up with is an array of objects, where each object is functionally similar to an "index path" except that it's composed of a string (key) and a value (offset).

Assuming that the actual search for highest numbers isn't in question, then I'd suggest creating one of these objects whenever you find a candidate number so that, once the top ten are found, the objects can be used as back-pointers to the numbers' source locations.

fabrice truillot de chambrier

Sounds like some sort of homework :)

So you have this:

NSMutableDictionary* source = [@{
    @"1" : @[ @10, @20, @100 … ],
    @"2" : @[ @8, @42, @17 … ]
} mutableCopy];

So lets start by creating another arrangement:

NSMutableArray* numbers = [NSMutableArray new];
for (NSArray* array in source.allValues)
{
    for (NSNumber* number in array)
    {
        [numbers addObject: @{ @"number" : number, @"parent" : array }];
    }
}

This is what we get:

@[
    @{ @"number" : @10, @"parent" : <array> },
    @{ @"number" : @20, @"parent" : <array> },
    …
]

Now we can sort and find the numbers you wanted.

[numbers sortUsingComparator: ^( id lhs, id rhs ){
    return [((NSDictionary*) rhs)[@"number"] compare: ((NSDictionary*) lhs)[@"number"]];
}];
NSArray* topNumbers = [numbers subarrayWithRange: NSMakeRange( 0, 10 )];

Here we are. topNumbers contains the numbers you needed along the source array.

This is quite a naive way to do it. It can be optimized both in CPU time and memory usage by a fair amount. But hey, keep it simple is not a bad thing.

Not addressed: what if the tenth and eleventh numbers are equal? (adressed here: Pick Out Specific Number from Array?) range checks. not tested, not even compiled. ;)

Walk through the arrays creating an object/structure for each element, consisting of the numeric "key" value and the "path" (array indices) to the element. Sort the objects/structures so created. (This is referred to as a "tag sort".)

The other approach, if you only need the top N values (where N << total number of entries) is to create an array of N elements, consisting of the above key and path info. Scan through all the arrays and compare each array element to the smallest key of the N currently stored. If you find an element larger than the smallest stored, replace the smallest stored and sort the N elements to select a new smallest stored.

You have to short your array in descending order using 'C' logic. Here i'm going to give an example according to your condition....

// adding 20 elements in an array, suppose this is your original array (array1).
NSMutableArray *array1 = [[NSMutableArray alloc]init];

for(int i=0;i<20;i++)
{
    NSString *str = [NSString stringWithFormat:@"%d",(i*4)];
    [array1 addObject:str];
}

//make a copy of your original array 
NSMutableArray *array2 = [[NSMutableArray alloc]initWithArray:array1];


// this is the array which will get your sorting list
NSMutableArray *array3 = [[NSMutableArray alloc]init];

//declare an integer for compare as a maximum number and it to 0 initially
int max = 0;


// this is the logic to sort an array
for(int i=0;i<20;i++)
{
    for(int j=0;j<[array2 count];j++)
    {
        int f = [[array2 objectAtIndex:j] intValue];
        if(max<f)
        {
            max = f;
        }
    }

    NSString *str = [[NSNumber numberWithInt:max]stringValue];

    //max has a maximum value then add it to array3 and remove from array2 
    //for a new shorting

    [array3 addObject:str];
    [array2 removeObject:str];

    // set 0 to max again
    max = 0;
}

    //now after all procedure print the **array3**
    // and you will get all the objects in descending order,
    //you can take top **10** variables from **array3**

    NSLog(@"your sorting array %@", **array3**);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!