1. ## Helo with bubblesort?

Hi,

I have written a structure of 5 stocks allowing user to enter info on 5 stocks, the code below is the function I have attempted to write a function using bubblesort to sort the stock names inputed in alphabetical order from A to Z. Did I do it right? And how to get the program to print out the result off the sort (i.e., the names after it gone thru the bubblesort?)

Code:
```void sortStock(struct Stock s[], int n)
{
int i, j, k;
char t[9];

for(i=0; i<n-1; i++)
for(j=0; j<n-1; j++)
if (strcmp(s[j].stock_name, s[j+1].stock_name)>0)
{
strcpy(t, s[j+1].stock_name);
strcpy(s[j].stock_name, s[j+1].stock_name);
strcpy(s[j+1].stock_name, t);
}

//    for(int k = 0; k<n; k++)
//	printf("%s", s[SIZE]);
//	printf("\n\n");
}```

2. Looks ok except for the check condition of the 2nd loop
Code:
`for(j=0; j<n-1-i; j++)`
and the arguments of the first strcpy(), as in
Code:
```strcpy(t, s[j].stock_name);
...```

3. why the "-i"? I had done the bubblesort with regular integers and n-1 was all I needed to get the proper number of times the sorting will be done to get all the values properly sorted.

4. Originally Posted by x2x3i5x
why the "-i"? I had done the bubblesort with regular integers and n-1 was all I needed to get the proper number of times the sorting will be done to get all the values properly sorted.
Once you get the largest element at the end of your array, you don't make a full pass through it again, but stop one short. Then once you get the two largest elements at the end of your array, you don't make a full pass through it again, but stop two short. Etc.

5. the n-1-i bit is not as important here. "Correctness before efficiency"
The problem, as identified by itCbitC, is that your swap is broken. Walk through the three lines that perform the swap in your head, and if that doesn't work, do it on paper, and if you still don't notice the problem, make some good use of a debugger.

6. Good catch, iMalc. The swapper portion has a flaw. I wouldn't have caught that... I've never seen it wrong before. But easy enough to do given the unusual order of arguments for noobs.

Check source / destination in each strcpy, x2x3i5x.

7. Ok, made a revision on the code. I realized I wanted stock name in [j] is to be saved in t before we check the next stock name, which is in [j+1] against the stock name in [j]. t is there just in case we need to swap the stock names in [j] and [j+1]. Also, I added a third for loop, did I do that right? I changed it from what I had in the original post. This one I have here should properly print out to screen the list of stock names that should now be alphabetical order from A-Z?

Code:
```void sortStock(struct Stock s[], int n)
{
int i, j, k;
char t[9];

for(i=0; i<n-1; i++)
for(j=0; j<n-1-i; j++)
if (strcmp(s[j].stock_name, s[j+1].stock_name)>0)
{
strcpy(t, s[j].stock_name);
strcpy(s[j].stock_name, s[j+1].stock_name);
strcpy(s[j+1].stock_name, t);
}
for(k=0; k<n; k++)
printf("%s\n", s[k].stock_name);
}```