arm_insertion_sort_f32.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. #include "dsp/support_functions.h"
  29. #include "arm_sorting.h"
  30. /**
  31. @ingroup groupSupport
  32. */
  33. /**
  34. @addtogroup Sorting
  35. @{
  36. */
  37. /**
  38. * @private
  39. * @param[in] S points to an instance of the sorting structure.
  40. * @param[in] pSrc points to the block of input data.
  41. * @param[out] pDst points to the block of output data
  42. * @param[in] blockSize number of samples to process.
  43. *
  44. * @par Algorithm
  45. * The insertion sort is a simple sorting algorithm that
  46. * reads all the element of the input array and removes one element
  47. * at a time, finds the location it belongs in the final sorted list,
  48. * and inserts it there.
  49. *
  50. * @par It's an in-place algorithm. In order to obtain an out-of-place
  51. * function, a memcpy of the source vector is performed.
  52. */
  53. void arm_insertion_sort_f32(
  54. const arm_sort_instance_f32 * S,
  55. float32_t *pSrc,
  56. float32_t* pDst,
  57. uint32_t blockSize)
  58. {
  59. float32_t * pA;
  60. uint8_t dir = S->dir;
  61. uint32_t i, j;
  62. float32_t temp;
  63. if(pSrc != pDst) // out-of-place
  64. {
  65. memcpy(pDst, pSrc, blockSize*sizeof(float32_t) );
  66. pA = pDst;
  67. }
  68. else
  69. pA = pSrc;
  70. // Real all the element of the input array
  71. for(i=0; i<blockSize; i++)
  72. {
  73. // Move the i-th element to the right position
  74. for (j = i; j>0 && dir==(pA[j]<pA[j-1]); j--)
  75. {
  76. // Swap
  77. temp = pA[j];
  78. pA[j] = pA[j-1];
  79. pA[j-1] = temp;
  80. }
  81. }
  82. }
  83. /**
  84. @} end of Sorting group
  85. */