#include <stdio.h>
#include <stdlib.h>

// Цвета.
int a[10] = { 5, 3, 1, 0, 6, 9, 8, 2, 7, 4 };

// Веса.
int b[10] = { 30, 97, 72, 31, 89, 28, 56, 83, 99, 66 };

struct bottle { int weight; int color; };

static int compare_structures(const void *e1, const void *e2)
{
	// Преобразовываем в указатели на склянки.
	const struct bottle *b1 = e1;
	const struct bottle *b2 = e2;
	
	// Сравниваем.
	// return (b1->weight < b2->weight) ? -1 : (b1->weight > b2->weight) ? 1 : 0; 
	return b1->weight - b2->weight;
}

static void using_structures(void)
{
	// Создаем и заполняем массив структур. В реальности этого делать не нужно,
	// нужно сразу хранить в таком виде.
	
	struct bottle bottles[10];
	
	for (size_t i = 0; i < 10; i++)
	{
		bottles[i].color = a[i];
		bottles[i].weight = b[i];
	}
	
	// Сортируем.
	qsort(bottles, 10, sizeof(bottles[0]), compare_structures);
	
	// Выводим.
	for (size_t i = 0; i < 10; i++)
		printf("Bottle: color %i, weight %i\n", bottles[i].color, bottles[i].weight);
}

static int compare_with_indices(const void *e1, const void *e2)
{
	// Достаем индексы.
	size_t i1 = *(const size_t *) e1;
	size_t i2 = *(const size_t *) e2;
	
	// Сравниваем.
	// return (b[i1] < b[i2]) ? -1 : (b[i1] > b[i2]) ? 1 : 0;
	return b[i1] - b[i2];
}

static void using_indices(void)
{
	// Наша сортированная вьюшка с индексами.
	int sorted_by_weight[10];
	
	// Заполняем.
	for (size_t i = 0; i < 10; i++)
		sorted_by_weight[i] = i;
	
	// Сортируем.
	qsort(sorted_by_weight, 10, sizeof(sorted_by_weight[0]), compare_with_indices);
	
	// Выводим.
	for (size_t i = 0; i < 10; i++)
	{
		size_t j = sorted_by_weight[i];
		printf("Bottle %zu: color %i, weight %i\n", j, a[j], b[j]);
	}
}

int main(void)
{
	printf("Using structures:\n");
	using_structures();
	printf("Using indices:\n");
	using_indices();
	return 0;
}
