/ Drivers / CMSIS / DSP / Source / SupportFunctions / arm_bubble_sort_f32.c
arm_bubble_sort_f32.c
  1  /* ----------------------------------------------------------------------
  2   * Project:      CMSIS DSP Library
  3   * Title:        arm_bubble_sort_f32.c
  4   * Description:  Floating point bubble 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 "dsp/support_functions.h"
 30  #include "arm_sorting.h"
 31  
 32  /**
 33    @ingroup groupSupport
 34   */
 35  
 36  /**
 37    @addtogroup Sorting
 38    @{
 39   */
 40  
 41  /**
 42     * @private
 43     * @param[in]  S          points to an instance of the sorting structure.
 44     * @param[in]  pSrc       points to the block of input data.
 45     * @param[out] pDst       points to the block of output data
 46     * @param[in]  blockSize  number of samples to process.
 47     *
 48     * @par        Algorithm
 49     *               The bubble sort algorithm is a simple comparison algorithm that
 50     *               reads the elements of a vector from the beginning to the end,
 51     *               compares the adjacent ones and swaps them if they are in the 
 52     *               wrong order. The procedure is repeated until there is nothing 
 53     *               left to swap. Bubble sort is fast for input vectors that are
 54     *               nearly sorted.
 55     *
 56     * @par          It's an in-place algorithm. In order to obtain an out-of-place
 57     *               function, a memcpy of the source vector is performed
 58     */
 59  
 60  void arm_bubble_sort_f32(
 61    const arm_sort_instance_f32 * S, 
 62          float32_t * pSrc, 
 63          float32_t * pDst, 
 64          uint32_t blockSize)
 65  {
 66      uint8_t dir = S->dir;
 67      uint32_t i;
 68      uint8_t swapped =1;
 69      float32_t * pA;
 70      float32_t temp;
 71  
 72      if(pSrc != pDst) // out-of-place
 73      {
 74  	memcpy(pDst, pSrc, blockSize*sizeof(float32_t) );
 75  	pA = pDst;
 76      }
 77      else
 78  	pA = pSrc;
 79  
 80      while(swapped==1) // If nothing has been swapped after one loop stop
 81      {
 82  	swapped=0;
 83  
 84          for(i=0; i<blockSize-1; i++)
 85  	{
 86  	    if(dir==(pA[i]>pA[i+1]))
 87  	    {
 88  		// Swap
 89  		temp = pA[i];
 90  		pA[i] = pA[i+1];
 91  		pA[i+1] = temp;
 92  
 93  		// Update flag
 94  		swapped = 1;
 95  	    }
 96  	}
 97  
 98  	blockSize--;
 99      }
100  }
101  
102  /**
103    @} end of Sorting group
104   */