Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting a point array efficiently in C?

I need to sort a point array (a point is a struct with two float types - one for x and one for y) in a special fashion.

The points have to be sorted so when they are traversed, they form a zig-zag pattern starting at the top leftmost point, moving to the top rightmost point, then down to the second leftmost point, to the second rightmost point and so on.

Illustration

I need this to be able to convert arbitrary polygons to triangle strip arrays which I can then draw using GLes. What would be the most efficient way of sorting those points, by either using pointers (ie. passing and rearranging the pointers to the point structures) or by copying and moving the data in the structures directly?

like image 231
Kristina Brooks Avatar asked Aug 31 '12 15:08

Kristina Brooks


People also ask

What is the most efficient way of sorting an array?

Quicksort. Quicksort is generally thought of as the most efficient 'general' sorting algorithm, where nothing is known about the inputs to the array, and it's more efficient than insertion sort on large lists.

Which is the most efficient sorting algorithm in C?

Quicksort. Quicksort is one of the most efficient sorting algorithms, and this makes of it one of the most used as well. The first thing to do is to select a pivot number, this number will separate the data, on its left are the numbers smaller than it and the greater numbers on the right.

How do you sort an array in increasing order?

Example: Sort an Array in Java in Ascending Order Then, you should use the Arrays. sort() method to sort it. That's how you can sort an array in Java in ascending order using the Arrays. sort() method.

Which sort is best for nearly sorted arrays?

​Many sorting algorithms are available, but the one which is best suited for the almost sorted array is the insertion sort.

How to sort an array using pointers in C programming?

Basic C programming, Array, Functions, Pointers, Function Pointers Logic to sort an array using pointers Below is the step by step descriptive logic to sort an array using pointer. Input size and elements in array. Store them in some variable say sizeand arr.

How to sort the given array in ascending order in C?

This c program will sort the given array in ascending order by passing the array pointer to functions and the while loop for sorting.

How efficient is the Array sort algorithm?

The algorithm is not efficient when compared to other modern sorting algorithms but its easy to understand and use. Program to sort an array using pointers /** * C program to sort an array using pointers.

What is selection sort in C++?

The selection sort may be defined as another algorithm for sorting the list in which the array is bifurcated into two arrays where the first array is supposed to be empty while the second array consists of the unsorted list of values.


1 Answers

I'd use qsort() with a custom compare() function that as @stefan noted, sorts descending by y then alternates (max/min) for x.

like image 100
Dan Pichelman Avatar answered Sep 23 '22 19:09

Dan Pichelman