/ Drivers / CMSIS / DSP / Source / SupportFunctions / arm_insertion_sort_f32.c
arm_insertion_sort_f32.c
 1  /* ----------------------------------------------------------------------
 2   * Project:      CMSIS DSP Library
 3   * Title:        arm_insertion_sort_f32.c
 4   * Description:  Floating point insertion 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 insertion sort is a simple sorting algorithm that
50     *               reads all the element of the input array and removes one element 
51     *               at a time, finds the location it belongs in the final sorted list, 
52     *               and inserts it there. 
53     *
54     * @par          It's an in-place algorithm. In order to obtain an out-of-place
55     *               function, a memcpy of the source vector is performed.
56     */
57  
58  void arm_insertion_sort_f32(
59    const arm_sort_instance_f32 * S, 
60          float32_t *pSrc, 
61          float32_t* pDst, 
62          uint32_t blockSize)
63  {
64      float32_t * pA;
65      uint8_t dir = S->dir;
66      uint32_t i, j;
67      float32_t temp;
68  
69      if(pSrc != pDst) // out-of-place
70      {   
71          memcpy(pDst, pSrc, blockSize*sizeof(float32_t) );
72          pA = pDst;
73      }
74      else
75          pA = pSrc;
76   
77      // Real all the element of the input array
78      for(i=0; i<blockSize; i++)
79      {
80  	// Move the i-th element to the right position
81          for (j = i; j>0 && dir==(pA[j]<pA[j-1]); j--)
82          {
83  	    // Swap
84              temp = pA[j];
85  	    pA[j] = pA[j-1];
86  	    pA[j-1] = temp;
87          }
88      }
89  } 
90  
91  /**
92    @} end of Sorting group
93   */