Help With Bubble Sort

This is a discussion on Help With Bubble Sort within the C Programming forums, part of the General Programming Boards category; Okay, I'm back again. From what I understand this bubble sort, should sort the entire struct in ascending order according ...

1. Help With Bubble Sort

Okay, I'm back again. From what I understand this bubble sort, should sort the entire struct in ascending order according to the price member... But it just prints out the struct in its original form for me. Probably just a stupid mistake, but can anyone point it out?

Code:
```#include <stdio.h>
#include <conio.h>

#define NUM 5

struct inv {
char partID[6];
int quantity;
float price;
};

void invSort(struct inv items[NUM]);
void invPrint(struct inv items[NUM]);

main()
{
struct inv items[NUM] = {{"HDWR9", 13, 4.53},
{"WDGT4", 2, 15.82},
{"POPL0", 4, 8.32},
{"RTYN6", 7, 5.40},
{"LOUN4", 61, 7.43}};

printf("Inventory\n\n");
invSort(items);
invPrint(items);

printf("\n\nPress any key to continue...");
getch();
return 0;
}

void invSort(struct inv items[NUM])
{
int inner, outer;
struct inv temp;
int didSwap;

for (outer = 0; outer < (NUM - 1); outer++) {
didSwap = 0;
for (inner = outer; inner < NUM; inner++) {
if (items[outer].price > items[inner].price) {
temp = items[outer];
items[outer] = items[inner];
items[inner] = temp;
didSwap = 1;
}
}
if (!didSwap) {
break;
}
}
return;
}

void invPrint(struct inv items[NUM])
{
int ctr = 0;

for (ctr = 0; ctr < NUM; ctr++) {
printf("Part ID: %s, Quantity: %d, Price: \$%6.2f\n",
items[ctr].partID, items[ctr].quantity, items[ctr].price);
}
return;
}```

2. Compare and contrast:
Code:
```void invSort(struct inv items[NUM])
{
int inner, outer;
struct inv temp;
int didSwap;

for (outer = NUM - 1; outer > 0; outer--) {
didSwap = 0;
for (inner = 0; inner < outer; inner++) {
if (items[inner].price > items[inner + 1].price) {
temp = items[inner];
items[inner] = items[inner + 1];
items[inner + 1] = temp;
didSwap = 1;
}
}
if (!didSwap) {
break;
}
}
return;
}```
Write down the execution of both your version and mine to see how they work. Nothing beats manual tracing like that for understanding an algorithm.

3. Gah, this book is upsetting me. That code is almost exact, word for word from the book. I don't understand why it would be wrong!

4. That code is almost exact, word for word from the book. I don't understand why it would be wrong!
This isn't by any chance a book by Herbert Schildt would it?

Quzah.

5. Umm, nope, it's this one...
http://www.amazon.com/exec/obidos/tg...glance&s=books

I also have this one, but it's quite complex so I'm learning all the basics before i dive into it:
http://www.amazon.com/exec/obidos/tg...glance&s=books

6. Hmm this works aswell:

Code:
```void invSort(struct inv items[NUM])
{
int inner, outer;
struct inv temp;
int didSwap;

for (outer = (NUM -1); outer >= 0; outer--) {
didSwap = 0;
for (inner = 1; inner <= outer; inner++) {
if (items[inner - 1].price > items[inner].price) {
temp = items[inner - 1];
items[inner - 1] = items[inner];
items[inner] = temp;
didSwap = 1;
}
}
if (!didSwap) {
break;
}
}
return;
}```

7. books!

Originally Posted by Explicit
Umm, nope, it's this one...
http://www.amazon.com/exec/obidos/tg...glance&s=books

I also have this one, but it's quite complex so I'm learning all the basics before i dive into it:
http://www.amazon.com/exec/obidos/tg...glance&s=books
Buy Schaums outlines series especially B.S.Gottfried's "Programming with C". It is decent for beginners. Then you can move to K&R, the bible of C

8. >It is decent for beginners.
Decent for the price, but I wouldn't recommend it over better beginner's texts such as "Pointers on C" by Kenneth Reek.