----------------------------------------------------------------------* * 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
| 92 | * </pre> |
| 93 | */ |
| 94 | SARRAY * |
| 95 | sarraySort(SARRAY *saout, |
| 96 | SARRAY *sain, |
| 97 | l_int32 sortorder) |
| 98 | { |
| 99 | char **array; |
| 100 | char *tmp; |
| 101 | l_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 | /*! |
no test coverage detected