How to resolve the algorithm Sort disjoint sublist step by step in the Objective-C programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Sort disjoint sublist step by step in the Objective-C programming language
Table of Contents
Problem Statement
Given a list of values and a set of integer indices into that value list, the task is to sort the values at the given indices, while preserving the values at indices outside the set of those to be sorted. Make your example work with the following list of values and set of indices: Where the correct result would be: In case of one-based indexing, rather than the zero-based indexing above, you would use the indices {7, 2, 8} instead. The indices are described as a set rather than a list but any collection-type of those indices without duplication may be used as long as the example is insensitive to the order of indices given.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Sort disjoint sublist step by step in the Objective-C programming language
Source code in the objective-c programming language
#import
@interface DisjointSublistView : NSMutableArray {
NSMutableArray *array;
int *indexes;
int num_indexes;
}
- (instancetype)initWithArray:(NSMutableArray *)a andIndexes:(NSIndexSet *)ind;
@end
@implementation DisjointSublistView
- (instancetype)initWithArray:(NSMutableArray *)a andIndexes:(NSIndexSet *)ind {
if ((self = [super init])) {
array = a;
num_indexes = [ind count];
indexes = malloc(num_indexes * sizeof(int));
for (NSUInteger i = [ind firstIndex], j = 0; i != NSNotFound; i = [ind indexGreaterThanIndex:i], j++)
indexes[j] = i;
}
return self;
}
- (void)dealloc {
free(indexes);
}
- (NSUInteger)count { return num_indexes; }
- (id)objectAtIndex:(NSUInteger)i { return array[indexes[i]]; }
- (void)replaceObjectAtIndex:(NSUInteger)i withObject:(id)x { array[indexes[i]] = x; }
@end
@interface NSMutableArray (SortDisjoint)
- (void)sortDisjointSublist:(NSIndexSet *)indexes usingSelector:(SEL)comparator;
@end
@implementation NSMutableArray (SortDisjoint)
- (void)sortDisjointSublist:(NSIndexSet *)indexes usingSelector:(SEL)comparator {
DisjointSublistView *d = [[DisjointSublistView alloc] initWithArray:self andIndexes:indexes];
[d sortUsingSelector:comparator];
}
@end
int main(int argc, const char *argv[]) {
@autoreleasepool {
NSMutableArray *a = [@[@7, @6, @5, @4, @3, @2, @1, @0] mutableCopy];
NSMutableIndexSet *ind = [NSMutableIndexSet indexSet];
[ind addIndex:6]; [ind addIndex:1]; [ind addIndex:7];
[a sortDisjointSublist:ind usingSelector:@selector(compare:)];
NSLog(@"%@", a);
}
return 0;
}
You may also check:How to resolve the algorithm Pi step by step in the Python programming language
You may also check:How to resolve the algorithm Dynamic variable names step by step in the APL programming language
You may also check:How to resolve the algorithm Longest string challenge step by step in the PureBasic programming language
You may also check:How to resolve the algorithm McNuggets problem step by step in the REXX programming language
You may also check:How to resolve the algorithm Phrase reversals step by step in the Quackery programming language