Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <stddef.h>
- #include <string.h>
- #include "Sort.h"
- /// \brief Меняет местами 2 элемента массива.
- /// \param[in] e1 - Элемент 1
- /// \param[in] e2 - Элемент 2
- void swap(size_t size, char *e1, char *e2)
- {
- char *o = malloc(size); // Промежуточное значение
- if (o)
- {
- memmove(o, e1, size);
- memmove(e1, e2, size);
- memmove(e2, o, size);
- free(o);
- }
- }
- /// \brief Сортировка.
- /// \details Модифицированная сортировка пузырьком I.
- /// \param[in] buf -Адрес первого элемента массива
- /// \param[in] num - Число элементов
- /// \param[in] size - Размер элемента
- /// \param[in] sr_name - Функция сравнения
- void mysort(void *buf, size_t num, size_t size, int (*sr_name) (const void *, const void *))
- {
- char *pa, *pb; // Указатели
- int f, h; // Флаги остановки
- int i, j; // Счетчики
- char *fr = malloc(size * num);
- memmove(fr, buf, num*size);
- h = 0;
- f = num - 1;
- pa = fr + size;
- pb = fr + 2*size;
- for (i = 0; i < num - f; ++i)
- {
- for (j = 0; j < f; ++j)
- {
- if((sr_name(pa, pb)) > 0)
- {
- swap(size, pa, pb);
- h = j;
- }
- pa += size;
- pb += size;
- }
- pa = fr + size;
- pb = fr + 2 * size;
- if (h != 0)
- {
- f = h;
- h = 0;
- }
- }
- free(buf);
- buf = fr;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement