arm_selection_sort_f32.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. #include "arm_sorting.h"
  29. /**
  30. @ingroup groupSupport
  31. */
  32. /**
  33. @addtogroup Sorting
  34. @{
  35. */
  36. /**
  37. * @private
  38. * @param[in] S points to an instance of the sorting structure.
  39. * @param[in] pSrc points to the block of input data.
  40. * @param[out] pDst points to the block of output data
  41. * @param[in] blockSize number of samples to process.
  42. *
  43. * @par Algorithm
  44. * The Selection sort algorithm is a comparison algorithm that
  45. * divides the input array into a sorted and an unsorted sublist
  46. * (initially the sorted sublist is empty and the unsorted sublist
  47. * is the input array), looks for the smallest (or biggest)
  48. * element in the unsorted sublist, swapping it with the leftmost
  49. * one, and moving the sublists boundary one element to the right.
  50. *
  51. * @par It's an in-place algorithm. In order to obtain an out-of-place
  52. * function, a memcpy of the source vector is performed.
  53. */
  54. void arm_selection_sort_f32(
  55. const arm_sort_instance_f32 * S,
  56. float32_t * pSrc,
  57. float32_t * pDst,
  58. uint32_t blockSize)
  59. {
  60. uint32_t i, j, k;
  61. uint8_t dir = S->dir;
  62. float32_t temp;
  63. float32_t * pA;
  64. if(pSrc != pDst) // out-of-place
  65. {
  66. memcpy(pDst, pSrc, blockSize*sizeof(float32_t) );
  67. pA = pDst;
  68. }
  69. else
  70. pA = pSrc;
  71. /* Move the boundary one element to the right */
  72. for (i=0; i<blockSize-1; i++)
  73. {
  74. /* Initialize the minimum/maximum as the first element */
  75. k = i;
  76. /* Look in the unsorted list to find the minimum/maximum value */
  77. for (j=i+1; j<blockSize; j++)
  78. {
  79. if (dir==(pA[j] < pA[k]) )
  80. {
  81. /* Update value */
  82. k = j;
  83. }
  84. }
  85. if (k != i)
  86. {
  87. /* Swap the minimum/maximum with the leftmost element */
  88. temp=pA[i];
  89. pA[i]=pA[k];
  90. pA[k]=temp;
  91. }
  92. }
  93. }
  94. /**
  95. @} end of Sorting group
  96. */