MCPcopy Create free account
hub / github.com/creatale/node-dv / sarraySort

Function sarraySort

deps/leptonica/src/sarray2.c:94–134  ·  view source on GitHub ↗

----------------------------------------------------------------------* * Sort * *----------------------------------------------------------------------*/ ! * \brief sarraySort() * * \param[in] saout output sarray; can be NULL or equal to sain * \param[in] sain input sarray * \param[in] sortorder L_SORT_INCREASING or

Source from the content-addressed store, hash-verified

92 * </pre>
93 */
94SARRAY *
95sarraySort(SARRAY *saout,
96 SARRAY *sain,
97 l_int32 sortorder)
98{
99char **array;
100char *tmp;
101l_int32 n, i, j, gap;
102
103 PROCNAME("sarraySort");
104
105 if (!sain)
106 return (SARRAY *)ERROR_PTR("sain not defined", procName, NULL);
107
108 /* Make saout if necessary; otherwise do in-place */
109 if (!saout)
110 saout = sarrayCopy(sain);
111 else if (sain != saout)
112 return (SARRAY *)ERROR_PTR("invalid: not in-place", procName, NULL);
113 array = saout->array; /* operate directly on the array */
114 n = sarrayGetCount(saout);
115
116 /* Shell sort */
117 for (gap = n/2; gap > 0; gap = gap / 2) {
118 for (i = gap; i < n; i++) {
119 for (j = i - gap; j >= 0; j -= gap) {
120 if ((sortorder == L_SORT_INCREASING &&
121 stringCompareLexical(array[j], array[j + gap])) ||
122 (sortorder == L_SORT_DECREASING &&
123 stringCompareLexical(array[j + gap], array[j])))
124 {
125 tmp = array[j];
126 array[j] = array[j + gap];
127 array[j + gap] = tmp;
128 }
129 }
130 }
131 }
132
133 return saout;
134}
135
136
137/*!

Callers 1

Calls 3

sarrayCopyFunction · 0.85
sarrayGetCountFunction · 0.85
stringCompareLexicalFunction · 0.85

Tested by

no test coverage detected