/*
 * File: fkfcbaaippppfkfk_merge.c
 *
 * Code generated for Simulink model 'BSER_CellBalcCtl'.
 *
 * Model version                  : 1.424F
 * Simulink Coder version         : 8.11F (R2016b) 25-Aug-2016
 * C/C++ source code generated on : Mon Oct 14 16:47:41 2019
 */

#include "rtwtypes.h"
#include "fkfcbaaippppfkfk_merge.h"

/* Function for Chart: '<S12>/BSER_CellBalSort' */
void fkfcbaaippppfkfk_merge(int32_T idx[12], real32_T x[12], int32_T offset,
  int32_T np, int32_T nq, int32_T iwork[12], real32_T xwork[12])
{
  int32_T n;
  int32_T q;
  int32_T qend;
  int32_T iout;
  int32_T exitg1;
  if (!((np == 0) || (nq == 0))) {
    n = np + nq;
    for (q = 0; q + 1 <= n; q++) {
      iwork[q] = idx[offset + q];
      xwork[q] = x[offset + q];
    }

    n = 0;
    q = np;
    qend = np + nq;
    iout = offset - 1;
    do {
      exitg1 = 0;
      iout++;
      if (xwork[n] >= xwork[q]) {
        idx[iout] = iwork[n];
        x[iout] = xwork[n];
        if (n + 1 < np) {
          n++;
        } else {
          exitg1 = 1;
        }
      } else {
        idx[iout] = iwork[q];
        x[iout] = xwork[q];
        if (q + 1 < qend) {
          q++;
        } else {
          q = iout - n;
          while (n + 1 <= np) {
            idx[(q + n) + 1] = iwork[n];
            x[(q + n) + 1] = xwork[n];
            n++;
          }

          exitg1 = 1;
        }
      }
    } while (exitg1 == 0);
  }
}

/*
 * File trailer for generated code.
 *
 * [EOF]
 */
