Need to sort 3 arrays by one key array
I am trying to get 3 arrays sorted by one key array in objective c for the iphone, here is a example to help out...
Array 1 Array 2 Array 3 Array 4
1 15 21 7
3 12 8 9
6 7 8 0
2 3 4 8
When sorted i want this to look like
Array 1 Array 2 Array 3 Array 4
1 15 21 7
2 3 4 8
3 12 8 9
6 7 8 0
So array 2,3,4 are moving with Array 1 when sorted.
Currently i am using a bubble sort to do this but it lags so bad that it crashes by app. The code i am using to do this is
int flag = 0;
int i = 0;
int temp = 0;
do
{
flag=1;
for(i = 0; i < distancenumber; i++)
{
if(distance[i] > distance[i+1])
{
temp = distance[i];
distance[i]=distance[i + 1];
distance[i + 1]=temp;
temp = FlowerarrayNumber[i];
FlowerarrayNumber[i] = FlowerarrayNumber[i+1];
FlowerarrayNumber[i + 1] = temp;
temp = BeearrayNumber[i];
BeearrayNumber[i] = BeearrayNumber[i + 1];
BeearrayNumber[i + 1] = temp;
flag=0;
}
}
}while (flag==0);
where distance number is the amount of elements in all of the 开发者_开发知识库arrays, distance is array 1 or my key array.
and the other 2 are getting sorted.
If anyone can help me get a merge sort(or something faster, it is running on a iPhone so it needs to be quick and light) to do this that would be great i cannot figure out how the recursion works in this method and so having a hard time to get the code to work. Any help would be greatly appreciated
Can't you simply structure your array to have A array that each item holds a array ?
Then simply sort your array based on the first item of the array it holds, or have a simple struct that holds an item and also the array.
I'm just thinking out loud here, but if all of your arrays correspond with each other (that is, BeearrayNumber[x]
corresponds with FlowerarrayNumber[x]
, which corresponds with distance[x]
), then you could consider using an array of structures rather than independent arrays. For example:
typedef struct
{
int flowerNumber;
int beeNumber;
float distance;
} BeeFlowerData;
#define MAX_BEE_FLOWER_DATA (100)
BeeFlowerData allBeeFlowers[MAX_BEE_FLOWER_DATA];
Then, you can sort using POSIX qsort
:
int BeeFlowerComparator(const void *l, const void *r)
{
const BeeFlowerData *left = l;
const BeeFlowerData *right = r;
if (left->distance > right->distance)
return 1;
else if (left->distance < right->distance)
return -1;
else
return 0;
}
// somewhere in your class:
- (void) sort
{
qsort (allBeeFlowers, MAX_BEE_FLOWER_DATA, sizeof(BeeFlowerData), BeeFlowerComparator);
}
I can't believe no one has suggested wrapping them in an object yet. It's fairly trivial:
//MyObject.h
@interface MyObject : NSObject {
int a;
int b;
int c;
int d;
}
@property int a;
@property int b;
@property int c;
@property int d;
@end
//MyObject.m
@implementation MyObject
@synthesize a, b, c, d;
@end
//Elsewhere:
MyObject * m = [[MyObject alloc] init];
[m setA:1];
[m setB:15];
[m setC:21];
[m setD:7];
[myMutableArray addObject:m];
[m release];
//... do that for the rest of the sets of numbers
NSSortDescriptor * sortByA = [NSSortDescriptor sortDescriptorWithKey:@"a" ascending:YES];
[myMutableArray sortUsingDescriptors:[NSArray arrayWithObject:sortByA]];
When you do that, you'll have one array, but the objects in that array will be sorted by their "a" value in ascending order.
This is not a objective c specific question. This is an algorithmic question.
- First sort the first array.
- Loop through the first array and find the index for each number.
- Then retrieve the value in second array corresponding to the index in step 2.
- construct a new array that holds the results in step 3.
- Repeat step 2,3,4 for the other arrays as well.
精彩评论