📄 sort.c
字号:
/*****************************************************************************
*
* Project: Clock Example
* Name: Example
* Filename: Sort.c
* Date: 06.04.2001
* Rights: Hitex Development Tools
* Greschbachstr. 12
* 76229 Karlsruhe
*
****************************************************************************/
/******************************** DESCRIPTION ********************************
Two sorting algorithms, bubble_sort and search_sort are to be analyzed.
Both sort the global list called list, both need the function swap to
exchange two elements.
Function main calls both sorting routines in an endless loop with
changing values in list. Values for list are produced by shake based on
its two input variables. New values for the input variables are produced
by function random.
*****************************************************************************/
/******************************** module Sort.c *****************************/
#include "defines.h"
#define global extern /* to declare external variables and functions */
#undef global
#define global /* to declare global variables and functions */
#include "sort.h"
#define COUNT 20u
#define COUNT_MIN_1 19u
static uint32_t list[COUNT];
static void random2 (uint32_t *no1, uint32_t *no2)
{
(*no1)++;
*no2 += 13u;
}
static void shake (uint32_t random_no1, uint32_t random_no2)
{
uint8_t i;
for (i = 0u; i < COUNT; i++)
{
list[i] = random_no1 + random_no2;
random_no1 *= 25793u;
random_no2 *= 12391u;
}
}
static void swap (uint32_t *obj1, uint32_t *obj2)
{
uint32_t tmp;
tmp = *obj1;
*obj1 = *obj2;
*obj2 = tmp;
}
static void bubble_sort(void)
{
uint8_t i = 0u, j = 1u;
for (i = 0u; i < COUNT_MIN_1; i++)
{
for (j = COUNT_MIN_1; j > i; j--)
{
if (list[j] < list[j-1u])
{
swap (&list[j], &list[j-1u]);
}
}
}
}
static void search_sort(void)
{
uint8_t i = 1u, j = 1u;
uint16_t min_ind;
for (i = 0u; i < COUNT_MIN_1; i++)
{
min_ind = i;
for (j = i + 1u; j <= COUNT_MIN_1; j++)
{
if (list[min_ind] > list[j])
{
min_ind = j;
}
if (min_ind != i)
{
swap (&list[i], &list[min_ind]);
}
}
}
}
void sort (void)
{
uint32_t random_no1 = 0u, random_no2 = 1u;
shake (random_no1, random_no2);
bubble_sort ();
shake (random_no1, random_no2);
search_sort ();
random2 (&random_no1, &random_no2);
}
/************************************ EOF ***********************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -