473,804 Members | 3,091 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Efficient way to sort array of points

Let's say I have Point[] and I want to sort it circle way (e.g.
2;2.4;1,8;2,8,5 ;4;8,2;7,1;4
The algo for this is:
if ((p[i].X<p[i+1].X & p[i].Y<Avg(p).Y & p[i+1].Y<Avg(p).Y) |
(p[i].X>p[i+1].X & p[i].Y>Avg(p).Y & p[i+1].Y>Avg(p).Y) )
Swap(p[i],p[i+1]);
where Avg(p).Y is the middle (average) of all Y values.
Other words:
First of all all point with Y above the Middle of Ys acsending and then
all point with Y under the Middle of Ys descending

Is there more effecient way to do this except buble sort for 2 axes?

TNX

--
Tamir Khason
You want dot.NET? Just ask:
"Please, www.dotnet.us "

Nov 16 '05 #1
6 8193
Use System.Array.So rt(p, new Class1()) where class1 implements IComparer and place your code in the compare routine.

--
Michael Culley
"Tamir Khason" <ta**********@t con-NOSPAM.co.il> wrote in message news:u1******** ******@TK2MSFTN GP12.phx.gbl...
Let's say I have Point[] and I want to sort it circle way (e.g.
2;2.4;1,8;2,8,5 ;4;8,2;7,1;4
The algo for this is:
if ((p[i].X<p[i+1].X & p[i].Y<Avg(p).Y & p[i+1].Y<Avg(p).Y) |
(p[i].X>p[i+1].X & p[i].Y>Avg(p).Y & p[i+1].Y>Avg(p).Y) )
Swap(p[i],p[i+1]);
where Avg(p).Y is the middle (average) of all Y values.
Other words:
First of all all point with Y above the Middle of Ys acsending and then
all point with Y under the Middle of Ys descending

Is there more effecient way to do this except buble sort for 2 axes?

TNX

--
Tamir Khason
You want dot.NET? Just ask:
"Please, www.dotnet.us "

Nov 16 '05 #2
TNX for response.
Did it at first try, but it works even slower then straight forward
algorithms. Maybe did something wrong?, but prety sure that did not
As far as I nuderstand IComparer did the same old Buble sort, so why it
slower?...

--
Tamir Khason
You want dot.NET? Just ask:
"Please, www.dotnet.us "
"Michael Culley" <mc*****@NOSPAM optushome.com.a u> wrote in message
news:u5******** *****@TK2MSFTNG P12.phx.gbl...
Use System.Array.So rt(p, new Class1()) where class1 implements IComparer and place your code in the compare routine.
--
Michael Culley
"Tamir Khason" <ta**********@t con-NOSPAM.co.il> wrote in message

news:u1******** ******@TK2MSFTN GP12.phx.gbl...
Let's say I have Point[] and I want to sort it circle way (e.g.
2;2.4;1,8;2,8,5 ;4;8,2;7,1;4
The algo for this is:
if ((p[i].X<p[i+1].X & p[i].Y<Avg(p).Y & p[i+1].Y<Avg(p).Y) |
(p[i].X>p[i+1].X & p[i].Y>Avg(p).Y & p[i+1].Y>Avg(p).Y) )
Swap(p[i],p[i+1]);
where Avg(p).Y is the middle (average) of all Y values.
Other words:
First of all all point with Y above the Middle of Ys acsending and then
all point with Y under the Middle of Ys descending

Is there more effecient way to do this except buble sort for 2 axes?

TNX

--
Tamir Khason
You want dot.NET? Just ask:
"Please, www.dotnet.us "


Nov 16 '05 #3
"Tamir Khason" <ta**********@t con-NOSPAM.co.il> wrote in message news:uf******** *****@TK2MSFTNG P12.phx.gbl...
TNX for response.
Did it at first try, but it works even slower then straight forward
algorithms. Maybe did something wrong?, but prety sure that did not
Try converting this vb6 code to C#. It should be very quick.

http://www.mikesdriveway.com/misc/sort.zip
As far as I understand IComparer did the same old Buble sort, so why it
slower?...


I would have thought it would have a better algorithm than bubble. How many times does it call the Compare routine compared to your
bubble sort?

--
Michael Culley
Nov 16 '05 #4
Works your algorithmus?
If I had an array of points to be sorted by distance of origin [0;0], I
would calculate the sum of the square of x and y and sort by this number

Point[] points=....;
System.Collecti on.SortedList sortedList=new
System.Collecti on.SortedList(p oints.Length);
for(int i=0;i<points.Le ngth;i++)
sortedList.Add( points[i].X*points[i].X+points[i].Y*points[i].Y,points[i]);
"Tamir Khason" <ta**********@t con-NOSPAM.co.il> schrieb im Newsbeitrag
news:u1******** ******@TK2MSFTN GP12.phx.gbl...
Let's say I have Point[] and I want to sort it circle way (e.g.
2;2.4;1,8;2,8,5 ;4;8,2;7,1;4
The algo for this is:
if ((p[i].X<p[i+1].X & p[i].Y<Avg(p).Y & p[i+1].Y<Avg(p).Y) |
(p[i].X>p[i+1].X & p[i].Y>Avg(p).Y & p[i+1].Y>Avg(p).Y) )
Swap(p[i],p[i+1]);
where Avg(p).Y is the middle (average) of all Y values.
Other words:
First of all all point with Y above the Middle of Ys acsending and then
all point with Y under the Middle of Ys descending

Is there more effecient way to do this except buble sort for 2 axes?

TNX

--
Tamir Khason
You want dot.NET? Just ask:
"Please, www.dotnet.us "

Nov 16 '05 #5
Dan
The documentation says that Array.Sort(myAr ray, IComparerObject ) uses a
quicksort:

"This method uses the QuickSort algorithm. This is an O(n ^2) operation,
where n is the number of elements to sort, with an average of ?(n log n)."
"Michael Culley" <mc*****@NOSPAM optushome.com.a u> wrote in message
news:eZ******** ********@TK2MSF TNGP10.phx.gbl. ..
"Tamir Khason" <ta**********@t con-NOSPAM.co.il> wrote in message news:uf******** *****@TK2MSFTNG P12.phx.gbl...
TNX for response.
Did it at first try, but it works even slower then straight forward
algorithms. Maybe did something wrong?, but prety sure that did not


Try converting this vb6 code to C#. It should be very quick.

http://www.mikesdriveway.com/misc/sort.zip
As far as I understand IComparer did the same old Buble sort, so why it
slower?...


I would have thought it would have a better algorithm than bubble. How

many times does it call the Compare routine compared to your bubble sort?

--
Michael Culley

Nov 16 '05 #6
"Dan" <da*@dontspamme .com> wrote in message news:eE******** ******@tk2msftn gp13.phx.gbl...
The documentation says that Array.Sort(myAr ray, IComparerObject ) uses a
quicksort:

"This method uses the QuickSort algorithm. This is an O(n ^2) operation,
where n is the number of elements to sort, with an average of ?(n log n)."


I tested the ArrayList sort and compared it to my* sort. I thought mine was fast but the arraylist sort was significantly faster
(3.6 times faster). The bubble sort took huge amounts of time, I stopped it before it finished. If the op is getting poor results it
must be because their algorithm is slow. It could also be slow because of incorrect data being returned , eg if you tell it A > B
and B > A it is bound to get confused.

Results for sort of 100,000 items (Time (ms), Comparison Count):
My Sort 2895, 4,066,741
Arraylist sort 771, 2,223,288
Bubble Sort 2,500,000, 5,000,000,000
The time for the bubble sort was estimated cause I got sick of waiting :-)

*It's not really my sort, I pinched it from a website somewhere, all I did was package it into a reusable class. :-)

--
Michael Culley
Nov 16 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
10959
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.
40
4338
by: Elijah Bailey | last post by:
I want to sort a set of records using STL's sort() function, but dont see an easy way to do it. I have a char *data; which has size mn bytes where m is size of the record and n is the number of records. Both these numbers are known
4
2817
by: PCHOME | last post by:
Hi! I have questions about qsort( ). Is anyone be willing to help? I use the following struct: struct Struct_A{ double value; ... } *AA, **pAA;
3
1927
by: Daniel Weinand | last post by:
hello ng, i have a problem and a imho an insufficient method of solution. strings should be sorted by specific text pattern and dispayed in groups. the strings are stored in a db and have the following layout: 1.0.0.0 1.1.0.0 1.1.1.0 1.1.2.0
11
3625
by: hoopsho | last post by:
Hi Everyone, I am trying to write a program that does a few things very fast and with efficient use of memory... a) I need to parse a space-delimited file that is really large, upwards fo a million lines. b) I need to store the contents into a unique hash. c) I need to then sort the data on a specific field. d) I need to pull out certain fields and report them to the user.
21
3227
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...
8
4624
by: Protoman | last post by:
Why doesn't my double bubble sort algorithm sort; it just spits the numbers out, int the exact same order that I inputted them. Code: void swap(int& i,int& j) { i^=j; j^=i; i^=j;
8
3193
by: SimeonArgus | last post by:
I need to sort a list of points, so I've considered using an IComparable implementation. Should be easy, right? But I need to know two things in the CompareTo function, not one. Test1: I need to know the distance in space from the "current point" to the point sent in. This is what the current IComparable interfeace gives me. Test2 : I also need to know the distance in space from the "previous point" to the point being sent in. HERE is...
1
1559
by: kimt | last post by:
Hello, I am currently writing an application that involves many (> 1000) points on a (x,y) plane. I am using a struct to contain the position information, and I have the structs contained in a STL vector. Given a target coordinate (x,y)_t, I would like to be able to cycle through the vector of points in order to obtain the closest point. What would be the most efficient way to implement this? I have considered keeping the vector sorted...
0
9704
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
10558
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
10318
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...
0
10069
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9130
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
6844
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
5503
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3802
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.