RWalib C Array Library User Guide > Sort and Unique > alibSort
  

alibSort
Sorts a vector or a matrix by its rows. Sorting is stable and is lexicographic by default, but columns can be reprioritized, and any column can be ignored or treated in descending order. The return parameter is the array of indices which defines the sorted order. The sorted version of the input array can be obtained from this index array and alibGetSubset. The PV-WAVE API for this routine is the SORTN Function.
Prototypes
void alibSortb( wvlong m, wvlong n, wvlong p, wvlong *c, UCHAR *d, wvlong *i)
void alibSorts( wvlong m, wvlong n, wvlong p, wvlong *c, short *d, wvlong *i)
void alibSorti( wvlong m, wvlong n, wvlong p, wvlong *c, int *d, wvlong *i)
void alibSortl( wvlong m, wvlong n, wvlong p, wvlong *c, wvlong *d, wvlong *i)
void alibSortf( wvlong m, wvlong n, wvlong p, wvlong *c, float *d, wvlong *i)
void alibSortd( wvlong m, wvlong n, wvlong p, wvlong *c, double *d, wvlong *i)
Parameters
m — (Input) The number of rows in the matrix (or column vector).
n — (Input) The number of columns in the matrix (n=1 for a column vector).
p — (Input) The number of columns used to sort the rows. If p is negative, |p| columns are used, and the first column is prioritized in descending order.
*c — (Input) If c=NULL then sorting is based on the first |p| columns. Otherwise, c is a |p|-element array of indices specifying the columns used to sort the rows. The columns are prioritized in order of appearance in c, and a negative index –j specifies that column j is prioritized in descending order.
*d — (Input) The (m,n) matrix or column vector to be sorted.
*i — (Input/Output) The m-element destination array. On return, array i contains the indices defining the sorted order in the array d.
Example
Here we give several examples for alibSort, and in the last example we show how the returned indices are used to obtain the sorted version of the data.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "alib.h"
void main() {
   /* input arrays used for all the examples */
   wvlong c0[2]={1,0}, c1[2]={-1,0}, j[2]={0,1}, dm[2]={12,2};
   UCHAR  b[24]={2,0,1,2,0,1,2,1,1,0,0,2,2,0,1,2,0,1,2,1,1,0,0,2};
   /* output arrays used for all the examples */
   UCHAR  bs[24];
   wvlong bi[12], *e[2]={bi,j};
   printf( "\n\n show 6-element vector b" );
      alibinit( NULL, NULL, NULL, NULL );
      alibPrintArrayb( 1, 1, 1, 6, b, NULL );
   printf( "\n\n show the indices describing ascending order in b" );
      alibSortb( 6, 1, 1, NULL, b, bi );
      alibPrintArrayl( 1, 1, 1, 6, bi, "%4lld" );
   printf( "\n\n show the indices describing descending order in b" );
      alibSortb( 6, 1, -1, NULL, b, bi );
      alibPrintArrayl( 1, 1, 1, 6, bi, "%4lld" );
   printf( "\n\n show (12,2) matrix b" );
      alibPrintArrayb( 1, 1, 12, 2, b, NULL );
   printf( "\n\n show the indices describing lexical order of rows in b" );
      alibSortb( 12, 2, 2, NULL, b, bi );
      alibPrintArrayl( 1, 1, 1, 12, bi, "%4lld" );
   printf( "\n\n show indices describing ascending order in column-0 of b" );
      alibSortb( 12, 2, 1, NULL, b, bi );
      alibPrintArrayl( 1, 1, 1, 12, bi, "%4lld" );
   printf( "\n\n show indices describing descending order in column-0 of b");
      alibSortb( 12, 2, -1, NULL, b, bi );
      alibPrintArrayl( 1, 1, 1, 12, bi, "%4lld" );
   printf( "\n\n show indices describing descending order in column-0, \n" );
   printf( " followed by ascending order in column-1" );
      alibSortb( 12, 2, -2, NULL, b, bi );
      alibPrintArrayl( 1, 1, 1, 12, bi, "%4lld" );
   printf( "\n\n show indices describing ascending order in column-1, \n" );
   printf( " followed by ascending order in column-0" );
      alibSortb( 12, 2, 2, c0, b, bi );
      alibPrintArrayl( 1, 1, 1, 12, bi, "%4lld" );
   printf( "\n\n show indices describing ascending order in column-1" );
      alibSortb( 12, 2, 1, c0, b, bi );
      alibPrintArrayl( 1, 1, 1, 12, bi, "%4lld" );
   printf( "\n\n show indices describing descending order in column-1, \n" );
   printf( " followed by descending order in column-0" );
      alibSortb( 12, 2, -2, c1, b, bi );
      alibPrintArrayl( 1, 1, 1, 12, bi, "%4lld" );
   printf( "\n\n now rearrange b in this order" );
      alibGetSubsetb( 2, dm, dm, e, b, bs );
      alibPrintArrayb( 1, 1, 12, 2, bs, NULL );
}
 
Output:
 
 show 6-element vector b
 
   2   0   1   2   0   1
 
 show the indices describing ascending order in b
 
   1   4   2   5   0   3
 
 show the indices describing descending order in b
 
   0   3   2   5   1   4
 
 show (12,2) matrix b
 
   2   0
   1   2
   0   1
   2   1
   1   0
   0   2
   2   0
   1   2
   0   1
   2   1
   1   0
   0   2
 
 show the indices describing lexical order of rows in b
 
   2   8   5  11   4  10   1   7   0   6   3   9
 
 show indices describing ascending order in column-0 of b
 
   2   5   8  11   1   4   7  10   0   3   6   9
 
 show indices describing descending order in column-0 of b
 
   0   3   6   9   1   4   7  10   2   5   8  11
 
 show indices describing descending order in column-0, 
 followed by ascending order in column-1
 
   0   6   3   9   4  10   1   7   2   8   5  11
 
 show indices describing ascending order in column-1, 
 followed by ascending order in column-0
 
   4  10   0   6   2   8   3   9   5  11   1   7
 
 show indices describing ascending order in column-1
 
   0   4   6  10   2   3   8   9   1   5   7  11
 
 show indices describing descending order in column-1, 
 followed by descending order in column-0
 
   1   7   5  11   3   9   2   8   0   6   4  10
 
 now rearrange b in this order
 
   1   2
   1   2
   0   2
   0   2
   2   1
   2   1
   0   1
   0   1
   2   0
   2   0
   1   0
   1   0

Version 2017.0
Copyright © 2017, Rogue Wave Software, Inc. All Rights Reserved.