LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Using C+ Function "QSORT" in LV

Hi all,

 

I'm converting some C+ code to LabVIEW. 

 

I want to use the C+ "QSORT" function as I cannot find an equivalent in LV.  Is there one?

 

Can I use the Code Interface Node?  I don't have a C compiler.  I don't know how to write a C file.

 

Here is a description of the QSORT function taken from the Using External Code in LV manual (p276):

 

QSort

void QSort(arrayp, n, elmtSize, compareProcP());

Purpose

Sorts an array of an arbitrary data type using the QuickSort algorithm. In addition to passing

the array you want to sort to this routine, you also pass a comparison procedure that this sort

routine then uses to compare elements in the array.

The comparison routine should return a number less than zero if
a is less than b, zero if a is

equal to b, and a number greater than zero if a is greater than b.

You should declare the comparison routine to have the following parameters and return type.

int32 compareProcP(UPtr a, UPtr b);

Parameters

Name Type Description

arrayp Uptr Pointer to an array of data.

n int32 Number of elements in the array you want to

sort.

elmtSize int32 Size in bytes of an array element.

compareProcP CompareProcPtr Comparison routine you want QSort to useto compare array elements. QSort passes

this routine the addresses of two elements

that it needs to compare.

0 Kudos
Message 1 of 5
(4,239 Views)

hi battler,

 why dont you make use of sort vi in array pallette...

 

 

thanks and regards,

srikrishnaNF

Regards,
Srikrishna


0 Kudos
Message 2 of 5
(4,218 Views)

There is no generic quick-sort in LabVIEW - you would have to code your own implementation. 😞

 

@srikrishnaNF

The advantage of an equivalent to that Qsort routine is that it is possible to customize the order logic - in the Sort 1D Array function, you are restricted to sorting by ascending number or alphabetical, and if you are sorting an array of clusters, only the 1st element in the cluster is used for the sort. 

0 Kudos
Message 3 of 5
(4,200 Views)

I found a couple of QUICK SORT LV implementations.

 

They appear to work for simple numeric arrays but I cannot get them to work for my application.  I'm trying to implement the Graham's (Hull) Algorithm (see here).

 

Attached are a couple of the implementations.  I got them from here and another place.

 

I'm trying to sort cluster array.  My Compare function compares 3 (not 2) array elements.  It appears to sort some of my array correctly but may be terminating early.  The attached implementations call only 2 instances of QSORT recursively.. do I need more?..

Message Edited by battler. on 05-22-2010 09:24 PM
Download All
0 Kudos
Message 4 of 5
(4,193 Views)

Custom sorting is very easy in LabVIEW.  Don't make it more difficult than it needs to be.

 

To sort by a group of keys, you need to build a new array with the keys at the beginning.  For example, for each old cluster element, build a new cluster that has key1, key2, key3, and old cluster.  Use 1-D sort array to sort the new cluster array, then loop and extract the original cluster from the new cluster.  Another approach is to just store the index of the original array, then use the sorted index list to create the new array.

 

1-D sort array will always sort clusters by the first element.  If the first elements match, it sorts by the second element, etc.  It will sort by all the existing elements if necessary.  Thus, it will sort by the multiple keys in your new cluster.

 

Bruce

Bruce Ammons
Ammons Engineering
Message 5 of 5
(4,141 Views)