It this post you can find a function in C which is a able to find X minimum or maximum elements in a given array, order the output either ascended or descended and get either distinct elements or duplications. Of course this function is far away from optimal solutions as it is better to use specific algorithms depending on your needs, however for fast experimentation it is useful. You can use this function either to get the X mins/maxes but also to order an array or get all the distinct elements of it. The required arguments are described and passed using a simple structure.
Below is the source code and a simple example.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
////////////////////////////////////////////////////////////////////
typedef struct
{
bool is_mins; // 1 = find mins, 0 = find maxs;
bool is_ascent; // 1 = place them in asc order, 0 = place them in desc order;
bool is_distinct; // 1 = add only distinct elements, 0 = use all
int* e_array; // the main(full) array
int e_array_size; // size of the main array
int* m_array; // the output array
int m_array_size; // max size of the output array
}findmms_config_t;
////////////////////////////////////////////////////////////////////
void array_manipulation_invert(int* arr, int arr_size);
int find_mmsw(findmms_config_t* arguments);
////////////////////////////////////////////////////////////////////
void array_manipulation_invert(int* arr, int arr_size)
{
int tmp;
for (int i = 0; i < arr_size / 2; i++)
{
tmp = arr[i];
arr[i] = arr[arr_size - (i + 1)];
arr[arr_size - (i + 1)] = tmp;
}
}
////////////////////////////////////////////////////////////////////
int find_mmsw(findmms_config_t* arguments)
{
// use locals to srink the naming as long as the final output will not change
bool is_mins = arguments->is_mins;
bool is_ascent = arguments->is_ascent;
bool is_distinct = arguments->is_distinct;
int *e_array = arguments->e_array;
int *m_array = arguments->m_array;
int m_array_size = arguments->m_array_size;
int e_array_size = arguments->e_array_size;
// some extra locals
int m_array_actual_size = 0;
int initializer = is_mins ? INT_MAX : INT_MIN;
// init m_array with max_field_val if looking for min or min_field_val if looking for max
for (int i = 0; i < arguments->m_array_size; i++)
arguments->m_array[i] = initializer;
// actual algorithm
for (int i = 0; i < e_array_size; i++)
{
for (int j = 0; j < m_array_size; j++)
{
if (m_array[j] == e_array[i] && is_distinct != 0)
{
break;
}
else if (m_array[j] > e_array[i] ? is_mins : !is_mins)
{
memmove(&m_array[j + 1], &m_array[j], (m_array_size - (j +1)) * sizeof(int));
m_array[j] = e_array[i];
m_array_actual_size++;
break;
}
}
}
m_array_actual_size = m_array_actual_size > m_array_size
? m_array_size : m_array_actual_size;
if (is_mins != is_ascent)
array_manipulation_invert(m_array, m_array_actual_size);
return m_array_actual_size;
}
////////////////////////////////////////////////////////////////////
int main()
{
int elements[] =
{ 5,8,1,3,6,15,69,23,
12,54,2345,543,234,
2543,546,65,22,324,
35,65,61,64,34,434,
3,62,32,52,6432,63,
47,45,34,542,52,12,
435,6534,6453,4871
};
int m_mins[5];
findmms_config_t config =
{
.is_mins =1,
.is_ascent = 1,
.is_distinct=1,
.e_array = elements,
.e_array_size= sizeof(elements) / sizeof(elements[0]),
.m_array = m_mins,
.m_array_size = 5
};
int actual_size = find_mmsw(&config);
for (int i = 0; i < actual_size; i++)
printf("%d ", m_mins[i]);
return 0;
}