/ Drivers / CMSIS / DSP / Source / SupportFunctions / arm_selection_sort_f32.c
arm_selection_sort_f32.c
  1  /* ----------------------------------------------------------------------
  2   * Project:      CMSIS DSP Library
  3   * Title:        arm_selection_sort_f32.c
  4   * Description:  Floating point selection sort
  5   *
  6   * $Date:        23 April 2021
  7   * $Revision:    V1.9.0
  8   *
  9   * Target Processor: Cortex-M and Cortex-A cores
 10   * -------------------------------------------------------------------- */
 11  /*
 12   * Copyright (C) 2010-2021 ARM Limited or its affiliates. All rights reserved.
 13   *
 14   * SPDX-License-Identifier: Apache-2.0
 15   *
 16   * Licensed under the Apache License, Version 2.0 (the License); you may
 17   * not use this file except in compliance with the License.
 18   * You may obtain a copy of the License at
 19   *
 20   * www.apache.org/licenses/LICENSE-2.0
 21   *
 22   * Unless required by applicable law or agreed to in writing, software
 23   * distributed under the License is distributed on an AS IS BASIS, WITHOUT
 24   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 25   * See the License for the specific language governing permissions and
 26   * limitations under the License.
 27   */
 28  
 29  #include "arm_sorting.h"
 30  
 31  /**
 32    @ingroup groupSupport
 33   */
 34  
 35  /**
 36    @addtogroup Sorting
 37    @{
 38   */
 39  
 40  /**
 41     * @private
 42     * @param[in]  S          points to an instance of the sorting structure.
 43     * @param[in]  pSrc       points to the block of input data.
 44     * @param[out] pDst       points to the block of output data
 45     * @param[in]  blockSize  number of samples to process.
 46     *
 47     * @par        Algorithm
 48     *               The Selection sort algorithm is a comparison algorithm that
 49     *               divides the input array into a sorted and an unsorted sublist 
 50     *               (initially the sorted sublist is empty and the unsorted sublist
 51     *               is the input array), looks for the smallest (or biggest)
 52     *               element in the unsorted sublist, swapping it with the leftmost
 53     *               one, and moving the sublists boundary one element to the right.
 54     *
 55     * @par          It's an in-place algorithm. In order to obtain an out-of-place
 56     *               function, a memcpy of the source vector is performed.
 57     */
 58  
 59  void arm_selection_sort_f32(
 60    const arm_sort_instance_f32 * S, 
 61          float32_t * pSrc, 
 62          float32_t * pDst, 
 63          uint32_t blockSize)
 64  {
 65      uint32_t i, j, k;
 66      uint8_t dir = S->dir;
 67      float32_t temp;
 68  
 69      float32_t * pA;
 70  
 71      if(pSrc != pDst) // out-of-place
 72      {
 73          memcpy(pDst, pSrc, blockSize*sizeof(float32_t) );
 74          pA = pDst;
 75      }
 76      else
 77          pA = pSrc;
 78  
 79      /*  Move the boundary one element to the right */
 80      for (i=0; i<blockSize-1; i++)
 81      {
 82          /* Initialize the minimum/maximum as the first element */
 83          k = i;
 84  
 85          /* Look in the unsorted list to find the minimum/maximum value */
 86          for (j=i+1; j<blockSize; j++)
 87          {
 88              if (dir==(pA[j] < pA[k]) )
 89              {
 90                  /* Update value */
 91                  k = j;
 92              }
 93          }
 94      
 95          if (k != i) 
 96          {
 97              /* Swap the minimum/maximum with the leftmost element */
 98              temp=pA[i];
 99  	    pA[i]=pA[k];
100  	    pA[k]=temp;
101          }
102      }
103  }
104  
105  /**
106    @} end of Sorting group
107   */