0

I have multiple arrays, the values in the arrays need to stay together column by column. I want to quicksort then binary search the first array then display all of the values from the other arrays according to the search on the first array. But if I sort the first array then the values from the other arrays are no longer in the correct place.

(Example, not real data) The unsorted but correct position of the data looks like this.

              array1{5,2,3,1,4}
              array2{6,9,1,7,8}
              array3{2,4,1,5,4}

when the first array is sorted it should look like this.

 array1{1,2,3,4,5}
 array2{7,9,1,8,6}
 array3{5,4,1,4,2}

then if 2 is searched it should display like this.

 array1{2}
 array2{9}
 array3{4}

My arrays are in double[].

1

2 Answers 2

-1

First thing; linked arrays like this suggests you might find it easier if you modelled the problem differently. For example, if the arrays are x, y, and x coordinates, then you might be better off using a class like this;

public class Point3d { public int x; public int y; public int z; }

Then sort the array of points by x;

Array.Sort(listOfPoints, (d1,d2) => d1.CompareTo(d2));

In general, lining things up like this suggests you've actually got records on the vertical, and your code may benefit from treating it like that.

Sign up to request clarification or add additional context in comments.

1 Comment

@FirstStep it seems his/her intention is fast access to records, not actually to sort and search linked arrays. This is a solution to that problem, and the code in C# is trivial. The sort I've already handled; Array.BinarySearch() can handle the second part trivially. I should mention that...
-1
int index = 0;
// save original positions
var lookup = array.ToDictionary(x => x, x => index++);

// sort array
Array.Sort(array1);
foreach (var item in array)
{
    int originalIndex = lookup[item];
    ... array2[originalIndex]
}

Actually, having this dictionary, you don't need to sort the array. It can be even faster because building a dictionary is O(N) and accessing an element is O(1) (both amortized).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.