Klekseusz
Klekseusz

Reputation: 63

C: How to sort a Struct by one of its element? Can't use pointers

How do I sort struct like this one:

typedef struct
{
    int weight;
    int price;
    Color color;
    Equip equip;
}Cars;

by one of it's attributes like price, or weight? Automobil array is previously declared. I can't use pointers, and any other built-in function.

Cars automobil[5]; 
Cars mobilOne={};

for(i=0; i<5; i++)
{
    if((i+1)==5)
    {
        break;
    }else
    {
        if (automobil[i].weight> automobil[i+1].weight)
        {
            mobilOne = automobil[i];
            automobil[i] = automobil[i+1];
            automobil[i+1] = mobilOne;
        }
    }
}

I tried to do this, this way, but it does not do anything... Also if someone could tell me, how can I pass a struct like this one into a function I would be really thankful!

Upvotes: 0

Views: 471

Answers (2)

JohnRowe
JohnRowe

Reputation: 49

OK, well first what you are trying to do is not quite as bad as some people might tell you as for small N bubble sort is still pretty fast. The following will do you and of course you need a double loop:

int main() {
    Cars automobil[NC];
    // Initialiase automobil here

    for (int i = 0; i < NC - 1; ++i) {
        int am = i;
        for (int j = i+1; j < NC; ++j) {
            if ( automobil[am].weight > automobil[j].weight )
                am = j;
        }

        if ( am != i) {
            Cars tmp = automobil[am];
            automobil[am] = automobil[i];
            automobil[i] = tmp;
        }
    }

    for (int i = 0; i < NC; ++i)
        printf("%d\n", automobil[i].weight);

}

Notice that we can copy structs but even here we try to do it as little as possible.

However, it's very easy to say "I'll never have more than ten cars" and then find you are trying to sort several thousand so I would urge you to learn and understand qsort():

int carsSort(const void *a, const void *b) {
    return ((Cars *) a)->weight - ((Cars *) b)->weight;
}

int main() {
    Cars automobil[NC];
    // Initialiase automobil here

    qsort(automobil, NC, sizeof *automobil, carsSort);

    for (int i = 0; i < NC; ++i)
        printf("%d\n", automobil[i].weight);
}

John

PS: in reply to "how do I pass the array to a function?" remember one of the many wise sayings of K&R: "When an array name is passed to a function, what is passed is the location of the beginning of the array".

Hence:

int carsSort(const void *a, const void *b) {
    return ((Cars *) a)->weight - ((Cars *) b)->weight;
}

void sortThem(Cars autom[]) {
    qsort(autom, NC, sizeof *autom, carsSort);
}

int main() {
    Cars automobil[NC];       
    // Initialiase automobil here

    sortThem(automobil);

    for (int i = 0; i < NC; ++i)
        printf("%d\n", automobil[i].weight);
}

Inside sortThem() "autom" is a variable whose value is the address of automobil[0].

Upvotes: 2

ryyker
ryyker

Reputation: 23218

Without going into implementation details here is a procedural algorithm for a bubble sort (not in C): Bubble Sort Algorithm. Note, as mentioned in comments, this bubble sort implementation uses nested loops.

One other item to keep in mind: In order to switch two objects, a third temporary object of the same type needs to be used. For example:

int temp
int arr1[]={2,5,7,2,9,1,8,0,5,2,1};
int count = sizeof(arr1)/sizeof(arr1[0])
for(int i = 0; i < count-1; i++ )
{
    if(arr1[i]>arr1[i+1])
    {
        temp = arr1[i];
        arr1[i] = arr1[i+1];
        arr[i+1] = temp;
    }
}

Because you are sorting on a single member of a collection of members, the assignment swapping routine will need to swap every member each time the condition for a swap exists, i.e. although determining if the swap condition exists only considers one member, swapping will include all members: weight, price, Color and Equip. And, if Color and Equip are of struct type (your post does not specify), then each member of these objects belonging to array elements being compared, will also need to be swapped.

You should look forward to eventually using pointers as this will significantly reduce the number of assignment statements needed to complete this sort.

Upvotes: 1

Related Questions