arm_bubble_sort_f32.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. #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 bubble sort algorithm is a simple comparison algorithm that
  46. * reads the elements of a vector from the beginning to the end,
  47. * compares the adjacent ones and swaps them if they are in the
  48. * wrong order. The procedure is repeated until there is nothing
  49. * left to swap. Bubble sort is fast for input vectors that are
  50. * nearly sorted.
  51. *
  52. * @par It's an in-place algorithm. In order to obtain an out-of-place
  53. * function, a memcpy of the source vector is performed
  54. */
  55. void arm_bubble_sort_f32(
  56. const arm_sort_instance_f32 * S,
  57. float32_t * pSrc,
  58. float32_t * pDst,
  59. uint32_t blockSize)
  60. {
  61. uint8_t dir = S->dir;
  62. uint32_t i;
  63. uint8_t swapped =1;
  64. float32_t * pA;
  65. float32_t temp;
  66. if(pSrc != pDst) // out-of-place
  67. {
  68. memcpy(pDst, pSrc, blockSize*sizeof(float32_t) );
  69. pA = pDst;
  70. }
  71. else
  72. pA = pSrc;
  73. while(swapped==1) // If nothing has been swapped after one loop stop
  74. {
  75. swapped=0;
  76. for(i=0; i<blockSize-1; i++)
  77. {
  78. if(dir==(pA[i]>pA[i+1]))
  79. {
  80. // Swap
  81. temp = pA[i];
  82. pA[i] = pA[i+1];
  83. pA[i+1] = temp;
  84. // Update flag
  85. swapped = 1;
  86. }
  87. }
  88. blockSize--;
  89. }
  90. }
  91. /**
  92. @} end of Sorting group
  93. */