jeudi 14 octobre 2021

Sorting array of objects in c++, repeating values

I'm currently trying to sort array of objects, functions work fine, but if values such as age repeat i would like to sort them basing on for an example height and if the height repeats I have to sort them by weight, the order is based on users input. I would like to make the program work as fast as possible and also make it use as little memory as possible, that is why I'm posting this quesiton, just to make sure to find the best solution. I'm stuck, I do not know how to get repeating values in this array and sort those under different criteria at the same time not sorting all the objects.

This is the code:`

#include <iostream>
using namespace std;

class Person{
public:
    string name;
    int age;
    int weight;
    int height;
};

void sortByAge(int n, Person * array);
void sortByHeight(int n, Person * array);
void sortByWeight(int n, Person * array);

int main()
{
    int n;
    cin >> n;

Person * people = new Person[n];

for(int i = 0; i < n; i++) {
    cin >> people[i].name >> people[i].age >> people[i].weight >> people[i].height;
}
cout << endl << endl;

int firstOrder, secondOrder, thirdOrder;
cout << "Enter the order of sorting, choose numbers form 1 to 3, where 1 is sorting by Age, 2 is sorting by Height and 3 is sorting by weight \n";
cin >> firstOrder >> secondOrder >> thirdOrder;

if(firstOrder == 1) {
    sortByAge(n,people);
    if(secondOrder == 2) {
        sortByHeight(n,people);
        sortByWeight(n,people);
    } else if(secondOrder == 3) {
        sortByWeight(n,people);
        sortByHeight(n,people);
    }
} else if(firstOrder == 2) {
    sortByHeight(n,people);
    if(secondOrder == 1) {
        sortByAge(n,people);
        sortByWeight(n,people);
    } else if(secondOrder == 3) {
        sortByWeight(n,people);
        sortByAge(n,people);
    }
} else if(firstOrder == 3) {
    sortByWeight(n,people);
    if(secondOrder == 1) {
        sortByAge(n,people);
        sortByHeight(n,people);
    } else if (secondOrder == 2) {
        sortByHeight(n,people);
        sortByAge(n,people);
    }
}
delete [] people;
return 0;
}

void sortByAge(int n, Person * array) {
    int y = 0;
    Person x;
    for(int i = 1; i < n; i++) {
        x = array[i];
        y = i - 1;
        while(y >= 0 && array[y].age < x.age) {
            array[y+1] = array[y];
            y--;
        }
        array[y+1] = x;
    }
}

void sortByWeight(int n, Person * array) {
    int y = 0;
    Person x;
    for(int i = 1; i < n; i++) {
        x = array[i];
        y = i - 1;
        while(y >= 0 && array[y].weight < x.weight) {
            array[y+1] = array[y];
            y--;
        }
        array[y+1] = x;
    }
}

void sortByHeight(int n, Person * array) {
    int y = 0;
    Person x;
    for(int i = 1; i < n; i++) {
        x = array[i];
        y = i - 1;
        while(y >= 0 && array[y].height < x.height) {
            array[y+1] = array[y];
            y--;
        }
        array[y+1] = x;
    }
}`

Aucun commentaire:

Enregistrer un commentaire