arm_heap_sort_f32.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_heap_sort_f32.c
  4. * Description: Floating point heap 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. static void arm_heapify(float32_t * pSrc, uint32_t n, uint32_t i, uint8_t dir)
  31. {
  32. /* Put all the elements of pSrc in heap order */
  33. uint32_t k = i; // Initialize largest/smallest as root
  34. uint32_t l = 2*i + 1; // left = 2*i + 1
  35. uint32_t r = 2*i + 2; // right = 2*i + 2
  36. float32_t temp;
  37. if (l < n && dir==(pSrc[l] > pSrc[k]) )
  38. k = l;
  39. if (r < n && dir==(pSrc[r] > pSrc[k]) )
  40. k = r;
  41. if (k != i)
  42. {
  43. temp = pSrc[i];
  44. pSrc[i]=pSrc[k];
  45. pSrc[k]=temp;
  46. arm_heapify(pSrc, n, k, dir);
  47. }
  48. }
  49. /**
  50. @ingroup groupSupport
  51. */
  52. /**
  53. @addtogroup Sorting
  54. @{
  55. */
  56. /**
  57. * @private
  58. * @param[in] S points to an instance of the sorting structure.
  59. * @param[in] pSrc points to the block of input data.
  60. * @param[out] pDst points to the block of output data
  61. * @param[in] blockSize number of samples to process.
  62. *
  63. * @par Algorithm
  64. * The heap sort algorithm is a comparison algorithm that
  65. * divides the input array into a sorted and an unsorted region,
  66. * and shrinks the unsorted region by extracting the largest
  67. * element and moving it to the sorted region. A heap data
  68. * structure is used to find the maximum.
  69. *
  70. * @par It's an in-place algorithm. In order to obtain an out-of-place
  71. * function, a memcpy of the source vector is performed.
  72. */
  73. void arm_heap_sort_f32(
  74. const arm_sort_instance_f32 * S,
  75. float32_t * pSrc,
  76. float32_t * pDst,
  77. uint32_t blockSize)
  78. {
  79. float32_t * pA;
  80. int32_t i;
  81. float32_t temp;
  82. if(pSrc != pDst) // out-of-place
  83. {
  84. memcpy(pDst, pSrc, blockSize*sizeof(float32_t) );
  85. pA = pDst;
  86. }
  87. else
  88. pA = pSrc;
  89. // Build the heap array so that the largest value is the root
  90. for (i = blockSize/2 - 1; i >= 0; i--)
  91. arm_heapify(pA, blockSize, i, S->dir);
  92. for (i = blockSize - 1; i >= 0; i--)
  93. {
  94. // Swap
  95. temp = pA[i];
  96. pA[i] = pA[0];
  97. pA[0] = temp;
  98. // Restore heap order
  99. arm_heapify(pA, i, 0, S->dir);
  100. }
  101. }
  102. /**
  103. @} end of Sorting group
  104. */