/*
 * File: fkfkdbaaaaimfkfc_sort.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"
#include "fkfkdbaaaaimfkfc_sort.h"

/* Function for Chart: '<S12>/BSER_CellBalSort' */
void fkfkdbaaaaimfkfc_sort(real32_T x[12], int32_T idx[12])
{
  int32_T iwork[12];
  real32_T xwork[12];
  real32_T x4[4];
  int8_T idx4[4];
  int32_T ib;
  int8_T perm[4];
  int32_T i3;
  int32_T i4;
  int32_T tailOffset;
  int32_T bLen;
  x4[0] = 0.0F;
  idx4[0] = 0;
  x4[1] = 0.0F;
  idx4[1] = 0;
  x4[2] = 0.0F;
  idx4[2] = 0;
  x4[3] = 0.0F;
  idx4[3] = 0;
  ib = 0;
  for (bLen = 0; bLen < 12; bLen++) {
    idx[bLen] = 0;
    ib++;
    idx4[ib - 1] = (int8_T)(bLen + 1);
    x4[ib - 1] = x[bLen];
    if (ib == 4) {
      if (x4[0] >= x4[1]) {
        ib = 1;
        tailOffset = 2;
      } else {
        ib = 2;
        tailOffset = 1;
      }

      if (x4[2] >= x4[3]) {
        i3 = 3;
        i4 = 4;
      } else {
        i3 = 4;
        i4 = 3;
      }

      if (x4[ib - 1] >= x4[i3 - 1]) {
        if (x4[tailOffset - 1] >= x4[i3 - 1]) {
          perm[0] = (int8_T)ib;
          perm[1] = (int8_T)tailOffset;
          perm[2] = (int8_T)i3;
          perm[3] = (int8_T)i4;
        } else if (x4[tailOffset - 1] >= x4[i4 - 1]) {
          perm[0] = (int8_T)ib;
          perm[1] = (int8_T)i3;
          perm[2] = (int8_T)tailOffset;
          perm[3] = (int8_T)i4;
        } else {
          perm[0] = (int8_T)ib;
          perm[1] = (int8_T)i3;
          perm[2] = (int8_T)i4;
          perm[3] = (int8_T)tailOffset;
        }
      } else if (x4[ib - 1] >= x4[i4 - 1]) {
        if (x4[tailOffset - 1] >= x4[i4 - 1]) {
          perm[0] = (int8_T)i3;
          perm[1] = (int8_T)ib;
          perm[2] = (int8_T)tailOffset;
          perm[3] = (int8_T)i4;
        } else {
          perm[0] = (int8_T)i3;
          perm[1] = (int8_T)ib;
          perm[2] = (int8_T)i4;
          perm[3] = (int8_T)tailOffset;
        }
      } else {
        perm[0] = (int8_T)i3;
        perm[1] = (int8_T)i4;
        perm[2] = (int8_T)ib;
        perm[3] = (int8_T)tailOffset;
      }

      idx[bLen - 3] = idx4[perm[0] - 1];
      idx[bLen - 2] = idx4[perm[1] - 1];
      idx[bLen - 1] = idx4[perm[2] - 1];
      idx[bLen] = idx4[perm[3] - 1];
      x[bLen - 3] = x4[perm[0] - 1];
      x[bLen - 2] = x4[perm[1] - 1];
      x[bLen - 1] = x4[perm[2] - 1];
      x[bLen] = x4[perm[3] - 1];
      ib = 0;
    }
  }

  if (ib > 0) {
    perm[1] = 0;
    perm[2] = 0;
    perm[3] = 0;
    if (ib == 1) {
      perm[0] = 1;
    } else if (ib == 2) {
      if (x4[0] >= x4[1]) {
        perm[0] = 1;
        perm[1] = 2;
      } else {
        perm[0] = 2;
        perm[1] = 1;
      }
    } else if (x4[0] >= x4[1]) {
      if (x4[1] >= x4[2]) {
        perm[0] = 1;
        perm[1] = 2;
        perm[2] = 3;
      } else if (x4[0] >= x4[2]) {
        perm[0] = 1;
        perm[1] = 3;
        perm[2] = 2;
      } else {
        perm[0] = 3;
        perm[1] = 1;
        perm[2] = 2;
      }
    } else if (x4[0] >= x4[2]) {
      perm[0] = 2;
      perm[1] = 1;
      perm[2] = 3;
    } else if (x4[1] >= x4[2]) {
      perm[0] = 2;
      perm[1] = 3;
      perm[2] = 1;
    } else {
      perm[0] = 3;
      perm[1] = 2;
      perm[2] = 1;
    }

    for (bLen = 12; bLen - 11 <= ib; bLen++) {
      idx[bLen - ib] = idx4[perm[bLen - 12] - 1];
      x[bLen - ib] = x4[perm[bLen - 12] - 1];
    }
  }

  for (ib = 0; ib < 12; ib++) {
    xwork[ib] = 0.0F;
    iwork[ib] = 0;
  }

  ib = 3;
  bLen = 4;
  while (ib > 1) {
    if ((ib & 1) != 0) {
      ib--;
      tailOffset = bLen * ib;
      if (12 - tailOffset > bLen) {
        fkfcbaaippppfkfk_merge(idx, x, tailOffset, bLen, 12 - (tailOffset + bLen),
          iwork, xwork);
      }
    }

    ib >>= 1;
    tailOffset = 1;
    while (tailOffset <= ib) {
      fkfcbaaippppfkfk_merge(idx, x, 0, bLen, bLen, iwork, xwork);
      tailOffset = 2;
    }

    bLen <<= 1;
  }

  if (12 > bLen) {
    fkfcbaaippppfkfk_merge(idx, x, 0, bLen, 12 - bLen, iwork, xwork);
  }
}

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