- Scilab Online Help
- Elementary Functions
- Discrete mathematics
- Floating point
- Integer representation
- Trigonometry
- abs
- amell
- and
- &
- binomial
- bitand
- bitor
- bloc2exp
- bloc2ss
- cat
- cell2mat
- cellstr
- char
- cumprod
- cumsum
- delip
- diag
- diff
- dsearch
- exp
- eye
- flipdim
- gsort
- imag
- imult
- ind2sub
- intersect
- inttrap
- isdef
- isempty
- isequal
- isequalbitwise
- isreal
- isvector
- kron
- lex_sort
- linspace
- log
- log10
- log1p
- log2
- logm
- logspace
- lstsize
- max
- meshgrid
- min
- modulo
- ndgrid
- ndims
- nextpow2
- norm
- ones
- or
- |
- pen2ea
- permute
- pertrans
- prod
- rand
- real
- resize_matrix
- setdiff
- sign
- signm
- size
- solve
- sqrt
- sqrtm
- squarewave
- ssrand
- sub2ind
- sum
- sysconv
- sysdiag
- syslin
- toeplitz
- trfmod
- trianfml
- tril
- trisolve
- triu
- typeof
- union
- unique
- vectorfind
- zeros
Please note that the recommended version of Scilab is 2025.0.0. This page might be outdated.
See the recommended documentation of this function
gsort
sorting by quick sort agorithm
Calling Sequence
[B [,k]]=gsort(A) [B [,k]]=gsort(A,option) [B [,k]]=gsort(A,option,direction)
Arguments
- A
a real,an integer or a character string vector/matrix or a sparse vector.
- option
a character string. It gives the type of sort to perform:
'r' : each column of
A
is sorted'c': each row of
A
is sorted'g': all elements of
A
are sorted. It is the default value.'lr': lexicographic sort of the rows of
A
'lc': lexicographic sort of the columns of
A
- direction
a character string. It gives the ordering direction:
'i'
stand for increasing and'd'
for decreasing order (default).- B
an array with same type and dimensions as
A
.- k
a real array with integer values and same dimensions as
A
. Contains the origin indices.
Description
gsort
implements a "quick sort" algorithm for
various data types.
B=gsort(A,'g')
,B=gsort(A,'g','d')
andB=gsort(A)
sort the elements of the arrayA
, seen asA(:)
in a decreasing order.B=gsort(A,'g','i')
sort the elements of the arrayA
in the increasing order.B=gsort(A,'lr')
sorts the rows ofA
in lexical decreasing order.B
is obtained by a permutation of the rows of matrixA
in such a way that the rows ofB
verifyB(i,:)>=B(j,:)
ifi<j
.B=gsort(A,'lr','i')
works similarily for increasing lexical order.B=gsort(A,'lc')
sorts the columns ofA
in lexical decreasing order.B
is obtained by a permutation of the columns of matrixA
in such a way that the columns ofB
verifyB(:,i)>=B(:,j)
ifi<j
.B=gsort(A,'lc','i')
works similarily for increasing lexical order.
If required the second return argument k
contains
the indices of the sorted values in A
. If
[B,k]=gsort(A,'g')
one has B==A(k)
.
The algorithme preserve the relative order of
records with equal values.
When v
is complex, the elements are sorted by
magnitude, i.e., abs(v
) . Only 'g'
as second argument is managed with complex.
With complex numbers, gsort
can be overloaded
see macro: SCI/modules/elementary_functions/macros/%_gsort.sci
if v
have %nan
or
%inf
as elements. gsort places these at the beginning
with 'i'
or at the end with 'd'
argument.
Examples
alr=[1,2,2; 1,2,1; 1,1,2; 1,1,1]; [alr1,k]=gsort(alr,'lr','i') [alr1,k]=gsort(alr,'lc','i') v=int32(alr) gsort(v) gsort(v,'lr','i') gsort(v,'lc','i') v=['Scilab' '2.6' 'Scilab' '2.7' 'xcos' '2.7' 'Scilab' '3.1' 'xcos' '3.1' 'xcos' '4.0' 'Scilab' '4.0'] gsort(v,'lr','i') gsort(v,'lc','i')
Bibliography
Quick sort algorithm from Bentley & McIlroy's "Engineering a Sort Function". Software---Practice and Experience, 23(11):1249-1265
<< flipdim | Elementary Functions | imag >> |