473,898 Members | 3,971 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Anyone know how to sort 3 arrays?

8 New Member
Hi everyone. I have 3 arrays of doubles that represent 3D coordinates. The arrays are of 8000 points so I put here a little example.

x = { 0.3 , 0.6 , 0.2} y = {2.0 , 0.3 , 0.2} z = {1.0 , 2.0 , 3.0}

So x(0),y(0),z(0) is a 3D point x(1),y(1),z(1) is another 3D point and so on. What I would like to do is to sort the 3D points in space from the closest to (0,0,0), so the 3 arrays are simultaneously sorted but maintaining the fact that x(i),y(i),z(i) is a 3D point. I've seen that it can be made for 2 arrays using array.sort(x,y) where x is sorted and y is recolocated following x index.
Can this be done for the 3 arrays?
(Hope I've explained myself :P ) Many thanks.
Apr 2 '07 #1
18 3400
vijaydiwakar
579 Contributor
Hi everyone. I have 3 arrays of doubles that represent 3D coordinates. The arrays are of 8000 points so I put here a little example.

x = { 0.3 , 0.6 , 0.2} y = {2.0 , 0.3 , 0.2} z = {1.0 , 2.0 , 3.0}

So x(0),y(0),z(0) is a 3D point x(1),y(1),z(1) is another 3D point and so on. What I would like to do is to sort the 3D points in space from the closest to (0,0,0), so the 3 arrays are simultaneously sorted but maintaining the fact that x(i),y(i),z(i) is a 3D point. I've seen that it can be made for 2 arrays using array.sort(x,y) where x is sorted and y is recolocated following x index.
Can this be done for the 3 arrays?
(Hope I've explained myself :P ) Many thanks.
I don't think so
Apr 2 '07 #2
godofredo
8 New Member
Well I've done it. Just a bit of thinking and some auxiliar arrays, albeit lot of memory usage, here's the code:

Expand|Select|Wrap|Line Numbers
  1.         Dim x() As Double = {3.0, 2.0, 1.0}
  2.         Dim y() As Double = {4.0, 5.0, 6.0}
  3.         Dim z() As Double = {1.0, 3.0, 2.0}
  4.         Dim auxx() As Double
  5.         Dim auxy() As Double
  6.         Dim auxz() As Double
  7.  
  8.         ReDim auxx(UBound(x))
  9.         x.CopyTo(auxx, 0)
  10.         Array.Sort(x, y)
  11.         Array.Sort(auxx, z)
  12.  
  13.         ReDim auxy(UBound(y))
  14.         y.CopyTo(auxy, 0)
  15.         Array.Sort(y, x)
  16.         Array.Sort(auxy, z)
  17.  
  18.         ReDim auxz(UBound(z))
  19.         z.CopyTo(auxz, 0)
  20.         Array.Sort(z, x)
  21.         Array.Sort(auxz, y)
And the output is:

x={3.0, 1.0, 2.0} y={4.0, 6.0, 5.0} z={1.0, 2.0, 3.0}

Which if you make some calcs the module of {x(i), y(i), z(i)} is sorted by its proximity to {0, 0, 0}

Hope this weird question helps someone.
Apr 2 '07 #3
Killer42
8,435 Recognized Expert Expert
Seems as though the simplest thing would be to combine the arrays into a single array of user-defined type. In other words, something like...

Expand|Select|Wrap|Line Numbers
  1. Type CoordTyp
  2.   X As Double
  3.   Y As Double
  4.   Z As Double
  5.   Distance As Double
  6. End Type
  7.  
  8. Dim CoOrd(1 to 8000) As CoordTyp
Then, once your array is populated with the X/Y/Z values, I'd suggest you fill in the Distance values, then sort the CoOrd array based on that field.
Apr 8 '07 #4
godofredo
8 New Member
Seems as though the simplest thing would be to combine the arrays into a single array of user-defined type. In other words, something like...

Expand|Select|Wrap|Line Numbers
  1. Type CoordTyp
  2.   X As Double
  3.   Y As Double
  4.   Z As Double
  5.   Distance As Double
  6. End Type
  7.  
  8. Dim CoOrd(1 to 8000) As CoordTyp
Then, once your array is populated with the X/Y/Z values, I'd suggest you fill in the Distance values, then sort the CoOrd array based on that field.
Yeah that seems a much more elegant way.
I don't know what kind of algorithm does the sort function use in order to evaluate the efficiency of both codes. Your proposal only has 1 sort call but needs to do (x^2 + y^2 + z^2) ^ 0.5 for all points to calculate distances. I'll have to check both codes but at first sight (not knowing what the sort function actually does) your code seems much faster ;). Many thanks.
Apr 9 '07 #5
Killer42
8,435 Recognized Expert Expert
Yeah that seems a much more elegant way.
I don't know what kind of algorithm does the sort function use in order to evaluate the efficiency of both codes. Your proposal only has 1 sort call but needs to do (x^2 + y^2 + z^2) ^ 0.5 for all points to calculate distances. I'll have to check both codes but at first sight (not knowing what the sort function actually does) your code seems much faster ;). Many thanks.
I'll be interested to hear the result. But surely you need to calculate the distance no matter what the method used, since you want the points sorted by it.

I'm afraid I didn't really take the time to understand your code, but got the impression that it would lose the relationship between the three arrays. I take it this is not the case?
Apr 9 '07 #6
godofredo
8 New Member
I'm afraid I didn't really take the time to understand your code, but got the impression that it would lose the relationship between the three arrays. I take it this is not the case?
Well, first of all: many thanks. I've been working on this and making some tries and you're probably right about losing the relationship between arrays. There is something I'm missing and I don't quite understand at all.

This is what I tried to do with my code,and what I understand with array.sort (surely I'm wrong so any help is wellcomed)
If you sort 2 arrays one acting as key, then what I thought is that the key array is sorted and the 2cond array is corresponded to that key array, i.e.
x={3,2,1} y={ a,b,c}
array.sort(x,y)
' output: x={1,2,3} ; y={c,b,a}
Is that correct?

If this is ok. then if you save a copy of the key array before the first sort and reuse that copy in a second sort the 3 arrays should maintain their relationship,i. e

x={3,2,1} y={a,b,c} z={what, is, wrong}
x.copyto(aux,0)
' output: x={3,2,1} aux={3,2,1}
array.sort(x,y)
' output: x={1,2,3} ; y={c,b,a}
array.sort(aux, z)
' output: x={1,2,3} ; z={wrong, is, what}

This actually worked on the example I posted previously but doesn't with larger arrays so there's something wrong I do not actually see.
Apr 11 '07 #7
Tig201
103 New Member
I'm not sure about the sort method you have but if you want to keep the 3D points together why don’t you use one array
Expand|Select|Wrap|Line Numbers
  1. Dim mPoint3d(6, 2) As Double
This is only 7 points but should get the idea across. Access the individual points with the first number and the X,Y,Z axis by the second number. Example: to get the X axis of the 1st point mPoint3d(0,0) or the Y Axis of the 3rd point
mPoint3d(2,1)
Apr 11 '07 #8
Killer42
8,435 Recognized Expert Expert
Well, first of all: many thanks. I've been working on this and making some tries and you're probably right about losing the relationship between arrays. There is something I'm missing and I don't quite understand at all.

This is what I tried to do with my code,and what I understand with array.sort (surely I'm wrong so any help is wellcomed)
If you sort 2 arrays one acting as key, then what I thought is that the key array is sorted and the 2cond array is corresponded to that key array...
But surely that's the problem, right there. You are keeping two arrays synchronised. That means they are now completely out of step with the third. In other words, you are moving around your X and Y coordinates, so they no longer correspond to the Z's.

This is why I suggested using a structure that keeps them together. May or may not make a difference to the efficiency, but will certainly be simpler to work with. On the other hand, I'm not familar with the array.sort routine you're using - is it a feature of VB.Net? Can it handle user-defined types like this?
Apr 11 '07 #9
Killer42
8,435 Recognized Expert Expert
...Example: to get the X axis of the 1st point mPoint3d(0,0) or the Y Axis of the 3rd point mPoint3d(2,1)
And if you want the numbers to make any sense, start your array at 1 instead of the stupid 0.

Computers, running just about any language, seem to love starting lists, arrays and so on at zero, but this is quite counter-intuitive to the human mind. I always recommend starting at 1 so it makes sense to the most important part of the process - you.
Apr 11 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

13
10967
by: Gram | last post by:
Hello, Can anyone help out with a bubble sort for strings using ASP? I have a text file of words id like to sort and count. Thanks in advance for any help. Gram.
6
3540
by: Xiaozhu | last post by:
say, if you had parallel arrays containing the sales person id, the month, and the sales figures (for that person in that month), sorting on sales figure and preserve the order of figures within the data about the same person: Original Data: id month sales +-----+ +-----+ +-----+ | 432 | | 8 | | 89 | +-----+ +-----+ +-----+ | 123 | | 8 | | 116 |
36
5472
by: HERC777 | last post by:
just paste the below into notepad, call it iqsort.html double click it and your IE browser will run it. (neat! a universal programming language we can all run!) it falls out from the sort procedure. I got javascript quicksort running, it's quick but it gets stack overflow after about 5000 elements. Am I passing the array as a parameter alright? I think javascript default is to pass pointers, that can be global.
1
2141
by: strotee | last post by:
#include <iostream> #include <ctime> using namespace std; // function declarations void swap(int *a, int *b); void sort(int arr, int beg, int end); int main() {
3
2799
by: cpuracr8 | last post by:
i've been looking through the topics here and i can't quite find one that helps me. i'm trying to sort a struct that contains three arrays using the sort() function. the three arrays are parallel and need to be grouped together when it is sorted. for instance, if i sort the int array by ascending order, i need the name and grade associated with that int array to also move with it. i hope what i said is not too confusing. what am i doing...
6
14060
by: Ali Chambers | last post by:
Hi, I have a two arrays that I wish to sort. One is the index array (full of floating point values). The other is a string array: ARRAY 1 ARRAY 2 ------- -------- -1.2 textA 12 textB 23.5 textC
21
3240
by: yeti349 | last post by:
Hi, I'm using the following code to retrieve data from an xml file and populate a javascript array. The data is then displayed in html table form. I would like to then be able to sort by each column. Once the array elements are split, what is the best way to sort them? Thank you. //populate data object with data from xml file. //Data is a comma delimited list of values var jsData = new Array(); jsData = {lib: "#field...
22
6308
by: AB | last post by:
Hello All, I'm trying to replicate a general purpose sort function (think qsort) void sort(void *arr, const int num, size_t size, int (*cmp)(void *a, void *b)) { int i = 0 ; int j = 0 ;
10
6281
by: ikarus | last post by:
Hello C++ Gurus! I'm comparing sorting algorithm for study goals. I've compared STL std::sort and hand-coded introsort on millions (tens of millions) of integers array sorting. It was tested on random, sorted, and sorted in reverse arrays. In all tests on such a huge arrays std::sort is faster by ~1/4 then my version. So it seems that it some call optimization. My 'hand coded' version is based on D. Musser's suggestions and on some...
0
9993
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9842
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11265
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10859
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10954
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9662
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7191
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4297
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.