Need an efficient sorting algorithm

Started by
2 comments, last by EvilNando 15 years, 5 months ago
I need to sort a 2d-array of Points (struct { int X; int Y; }; ) I dont want to implement a bubble sort because the array can get as big as 999x999 any recomendations? Thanks!
Advertisement
So the first problem I see is that points in 2D don't have a well defined ordering to begin with. How do you want the points ordered? Sorted by X then Y, sorted by Y then X, sorted by the sum of X and Y, with respect to some space filling curve... etc.? Once you distill your comparison operator, then you can use any classical efficient sorting algorithm (something thats O(n lg n)). For example, merge sort is a fast, classical comparison based sorting algorithm that would be pretty easy to implement. You could find most of the necessary resources for mergesort anywhere on the web (the wikipedia page is a good place to start).

Cheers,
Rob
Your standard library will come with a sort routine. For example, qsort() in C and std::sort() in C++.
Thank you very much for the comments

at the end I got smart enough (thanks to mr. cofee) to see that I actually didn't need to sort anything, just a copy in inverse order will do

but thanks a lot

This topic is closed to new replies.

Advertisement