Я создал структуру под названием product, и у меня есть множество продуктов. Я сделал 2 копии этого массива.
Я хочу отсортировать одну, 1-ую копию по цене продукта, и если цена 2 продуктов одинакова, я хочу отсортировать по идентику.
2-я копия, которую я хочу отсортировать это в алфавитном порядке, на основе описания продукта.
Проблема в том, что сортировка пузырьков ничего не сортирует, и я не понимаю, почему.
Программа:
#include<stdlib.h>
#include<stdio.h>
#include <string.h>
#define MAX_PROD 10000 /* max products in the sistem */
typedef struct product
{
int ident;/*idp of a product*/
char desc[64]; /* string that describes a product eg. "bread" */
int price; /* price of the product*/
int weight; /* weight of the product eg. 2kg */
int quant; /* quantity of the product in stock */
int state_prod;/*state of a product, if its 1 its in the system else is 0 and is not in the system*/
}product;
void swap(product xp,product yp);
void bubbleSort_price(product arr[], int n);
void bubbleSort_desc(product arr[], int n);
void print_array(product arr[]);
void copy_el(product source[],product dest[], int size);
product sistem[5] = {};
product sistem2[5] = {};
product sistem3[5] = {};
product sel(int i)
{
product p1 = {0,"pao",2,2,200,1};
product p2 = {1,"ovos",1,1,100,1};
product p3 = {2,"iscas",3,3,300,1};
product p4 = {3,"bacon",2,5,400,1};
product p5 = {4,"abacate",2,6,500,1};
if (i == 0)
{return p1;}
else if (i == 1)
{return p2;}
else if (i == 2)
{return p3;}
else if (i == 3)
{return p4;}
else if (i ==4)
{return p5;}
}
void ini_sys()
{
int i = 0;
for (i; i<5; i++)
{
sistem[i] = sel(i);
}
}
int main()
{
ini_sys();
copy_el(sistem,sistem2,5);
copy_el(sistem,sistem3,5);
printf("The products in the system are:\n");
print_array(sistem);
printf("\nSorted products by price in the system:\n");
bubbleSort_price(sistem2,5);
print_array(sistem2);
printf("\nSorted products by description in the system:\n");
bubbleSort_desc(sistem3,5);
print_array(sistem3);
return 0;
}
void swap(product xp,product yp)
{
product temp = xp;
xp = yp;
yp = temp;
}
void bubbleSort_price(product arr[], int n)
{
int i, j;
for (i = 0; i < n-1; i++)
{
for (j = 0; j < n-i-1; j++)
{
if (arr[j].price > arr[j+1].price)
{
swap(arr[j], arr[j+1]);
}
else if (arr[j].price == arr[j+1].price)
{
if (arr[j].ident > arr[j+1].ident)
{
swap(arr[j], arr[j+1]);
}
}
}
}
}
void bubbleSort_desc(product arr[], int n)
{
int i, j;
for (i = 0; i < n-1; i++)
for (j = 0; j < n-i-1; j++)
if (strcmp(arr[j].desc,arr[j+1].desc) > 0)
swap(arr[j], arr[j+1]);
}
void print_array(product arr[])
{
int i;
for (i = 0; i <5; i++){
printf("* %s %d %d\n",arr[i].desc,arr[i].price,arr[i].quant);}
}
void copy_el(product source[],product dest[], int size)
{
int i;
for(i=0; i<size; i++)
{
dest[i] = source[i];
}
}
Мой вывод:
The products in the system are:
* pao 2 200
* ovos 1 100
* iscas 3 300
* bacon 2 400
* abacate 2 500
Sorted products by price in the system:
* pao 2 200
* ovos 1 100
* iscas 3 300
* bacon 2 400
* abacate 2 500
Sorted products by description in the system:
* pao 2 200
* ovos 1 100
* iscas 3 300
* bacon 2 400
* abacate 2 500
Правильный вывод:
The products in the system are:
* pao 2 200
* ovos 1 100
* iscas 3 300
* bacon 2 400
* abacate 2 500
Sorted products by price in the system:
* ovos 1 100
* pao 2 200
* bacon 2 400
* abacate 2 500
* iscas 3 300
Sorted products by description in the system:
* abacate 2 500
* bacon 2 400
* iscas 3 300
* ovos 1 100
* pao 2 200