|
| 1 | +/**************************************************************************** |
| 2 | + * |
| 3 | + * Copyright (c) 2025 IMProject Development Team. All rights reserved. |
| 4 | + * Authors: Juraj Ciberlin <[email protected]> |
| 5 | + * |
| 6 | + * Redistribution and use in source and binary forms, with or without |
| 7 | + * modification, are permitted provided that the following conditions |
| 8 | + * are met: |
| 9 | + * |
| 10 | + * 1. Redistributions of source code must retain the above copyright |
| 11 | + * notice, this list of conditions and the following disclaimer. |
| 12 | + * 2. Redistributions in binary form must reproduce the above copyright |
| 13 | + * notice, this list of conditions and the following disclaimer in |
| 14 | + * the documentation and/or other materials provided with the |
| 15 | + * distribution. |
| 16 | + * 3. Neither the name IMProject nor the names of its contributors may be |
| 17 | + * used to endorse or promote products derived from this software |
| 18 | + * without specific prior written permission. |
| 19 | + * |
| 20 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 23 | + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 24 | + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 25 | + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 26 | + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 27 | + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 28 | + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 29 | + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
| 30 | + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 31 | + * POSSIBILITY OF SUCH DAMAGE. |
| 32 | + * |
| 33 | + ****************************************************************************/ |
| 34 | + |
| 35 | +#include "quick_sort.h" |
| 36 | + |
| 37 | +#include "utils.h" |
| 38 | + |
| 39 | +static int32_t |
| 40 | +Partition(byte_t* buffer, int32_t start_index, int32_t end_index, int32_t element_size, bool (*compareFun)(void* first, |
| 41 | + void* second)) { |
| 42 | + int32_t pivot_pos = start_index - 1; |
| 43 | + |
| 44 | + for (int32_t i = start_index; i <= (end_index - 1); ++i) { |
| 45 | + bool compare = compareFun(&buffer[end_index * element_size], &buffer[i * element_size]); |
| 46 | + if (compare) { |
| 47 | + ++pivot_pos; |
| 48 | + Utils_swapElements(&buffer[pivot_pos * element_size], &buffer[i * element_size], (uint32_t)element_size); |
| 49 | + } |
| 50 | + } |
| 51 | + Utils_swapElements(&buffer[(pivot_pos + 1) * element_size], &buffer[end_index * element_size], (uint32_t)element_size); |
| 52 | + ++pivot_pos; |
| 53 | + return pivot_pos; |
| 54 | +} |
| 55 | + |
| 56 | +void |
| 57 | +QuickSort_sort(byte_t* buffer, int32_t number_of_elements, int32_t element_size, |
| 58 | + bool (*compareFun)(void* first, void* second)) { |
| 59 | + int32_t indexes[MAX_NUM_OF_ELEMENTS] = {0, number_of_elements - 1}; |
| 60 | + |
| 61 | + int32_t top = 1; |
| 62 | + |
| 63 | + while (top >= 0) { |
| 64 | + int32_t end_index = indexes[top]; |
| 65 | + --top; |
| 66 | + int32_t start_index = indexes[top]; |
| 67 | + --top; |
| 68 | + |
| 69 | + int32_t pivot_index = Partition(buffer, start_index, end_index, element_size, compareFun); |
| 70 | + |
| 71 | + if ((pivot_index - 1) > start_index) { |
| 72 | + ++top; |
| 73 | + indexes[top] = start_index; |
| 74 | + ++top; |
| 75 | + indexes[top] = pivot_index - 1; |
| 76 | + } |
| 77 | + |
| 78 | + if ((pivot_index + 1) < end_index) { |
| 79 | + ++top; |
| 80 | + indexes[top] = pivot_index + 1; |
| 81 | + ++top; |
| 82 | + indexes[top] = end_index; |
| 83 | + } |
| 84 | + } |
| 85 | +} |
0 commit comments