# Need help with Bubblesort

• 11-13-2009
kburyanek
Need help with Bubblesort
Can someone please help me with my bubblesort. It is not sorting and also prints 3 times.

Code:

``` #include <stdio.h> #include <stdlib.h> #include<time.h> #define SIZE 6 #define MAX 53 void printArray(int N[], int T); void fillArray(int N[], int T); int main() { srand(time(NULL)); int PICKED[] = {0,0,0,0,0,0}; fillArray(PICKED, SIZE); printArray(PICKED, SIZE); return 0; } void fillArray(int N[], int T) { for(int P=0; P<T; P++) { N[P] = (int) (rand()% 52+1); for(int y=0; y<P; y++) { if(N[y] == N[P]) { N[P] = (int) (rand()%52 +1); P=-1; } } } } void printArray(int N[], int T)         {                 for(int P=0; P<SIZE-1; P++)                 {                         if(N[P]>N[P+1])                         {                                 int T = N[P+1];                                 N[P+1] = N[P];                                 N[P] = T; printf("\nLOTTO PICKS\n"); for(int P=0; P<SIZE; P++) {         printf("%d ", N[P]); printf("\n"); }}}}```
• 11-13-2009
MK27
Actually it is sorting. Your mistake is not to indent properly, because if you did, it is easy to see why it also repeatedly prints out:
Code:

```void printArray(int N[], int T) {         for(int P=0; P<SIZE-1; P++) {                 if(N[P]>N[P+1]) {                         int T = N[P+1];                         N[P+1] = N[P];                         N[P] = T;                         printf("\nLOTTO PICKS\n");                         for(int P=0; P<SIZE; P++) {                                 printf("%d ", N[P]);                                 printf("\n");                         }                 }         } }```
If you don't indent properly
1. you will continue to make stupid mistakes like this
2. people will not take you seriously

Also, you should not declare variables like this:
Code:

`for(int P=0; P<T; P++)`
Unless your intention is to have this compile ONLY as c99.
• 11-13-2009
kburyanek
I changed and it is not sorting
• 11-13-2009
kburyanek
It is sorting but not from lower number to high number. My instructor wants the bubblesort but in his example, the numbers have to be low to high. I do not know how to get it to do that part
• 11-13-2009
Code:

``` #include <stdio.h> #include <stdlib.h> #include<time.h> #define SIZE 6 #define MAX 53 void printArray(int N[], int T); void fillArray(int N[], int T); int main() { srand(time(NULL)); int PICKED[] = {0,0,0,0,0,0}; fillArray(PICKED, SIZE); printArray(PICKED, SIZE); return 0; } void fillArray(int N[], int T) { for(int P=0; P<T; P++) { N[P] = (int) (rand()% 52+1); for(int y=0; y<P; y++) { if(N[y] == N[P]) { N[P] = (int) (rand()%52 +1); P=-1; } } } } /* This isn't bubble sort, but it's in the same class as bubble sort */ void printArray(int N[], int T) //T is assigned a value in your sorting loop, so bringing it in as a parameter, makes no sense. {     int j, P;     for(P=0; P<SIZE-1; P++)     {         //you need an inner for loop, like:         for(j = P + 1; j < SIZE; j++)         {             if(N[P]>N[j])             {                       T = N[j];                      N[P] = N[j];                 N[P] = T;             }         }     }     printf("\nLOTTO PICKS\n");     for(int P=0; P<SIZE; P++)       printf("%d ", N[P]);     printf("\n"); }  // ==> }}}} this is a crappy style for code. Each brace should be directly //beneath the first word of the block of code that it's associated with.```
• 11-13-2009
MK27
Quote:

Originally Posted by kburyanek
It is sorting but not from lower number to high number. My instructor wants the bubblesort but in his example, the numbers have to be low to high. I do not know how to get it to do that part

This was the last group of output when I ran it:
Code:

```LOTTO PICKS 9 20 26 37 42 46```
Maybe you should post the code you are using as it exists now -- and if it isn't properly indented EVERYWHERE, I won't bother replying, because that makes it harder for me.
• 11-13-2009
kburyanek
Adak, I changed to the indentions that you have is this is the output that I have now.

LOTTO PICKS
14 14 14 14 14 14
• 11-13-2009
kburyanek
Thanks for all of your help. New program.

Code:

```#include <stdio.h> #include <stdlib.h> #include<time.h> #define SIZE 6 #define MAX 53 void sortArray(int N[], int T); void fillArray(int N[], int T); int main(void) { srand(time(NULL)); int PICKED[] = {0,0,0,0,0,0}; fillArray(PICKED, SIZE); sortArray(PICKED, SIZE); return 0; } void fillArray(int N[], int T) { for(int P=0; P<SIZE; P++) {         N[P] = rand() % 52+1; for(int PASS=0; PASS<P; PASS++) {         if(N[PASS] == N[P])                 {                         N[P] = rand() % 52 +1;                         PASS=-1;                         }                 }         } } void sortArray(int N[], int T) {     int PASS, P;     for(P=0; P<SIZE-1; P++)     {         for(PASS = P + 1; PASS < SIZE; PASS++)         {             if(N[P]>N[PASS])             {                       T = N[PASS];                                 N[PASS] = N[P];                                 N[P] = T;             }         }     }     printf("\nLOTTO PICKS\n\n");     for(int P=0; P<SIZE; P++)       printf("%d ", N[P]);     printf("\n"); }```
• 11-13-2009
I believe this is the problem:

Code:

```    printf("\nLOTTO PICKS\n\n");     for(int P=0; P<SIZE; P++)       printf("%d ", N[P]);```
The variable P was already declared earlier.

I'll check it out for anything else.

Another problem is, in the function fillarray(), inside the loop, you're setting PASS = -1, every time through the loop.

Did you mean PASS -= 1 ? (same thing as the simpler --PASS, or PASS--)

If you have trouble fixing it (you won't), let me know. After that fix, it works fine AFAIK. :)
• 11-13-2009
iMalc
Quote:

Originally Posted by kburyanek
Code:

```void sortArray(int N[], int T) {     int PASS, P;     for(P=0; P<SIZE-1; P++)     {         for(PASS = P + 1; PASS < SIZE; PASS++)         {             if(N[P]>N[PASS])             {                 T = N[PASS];                 N[PASS] = N[P];                 N[P] = T;             }         }     } }```

This is now a correct sorting algorithm but it is not bubble sort, because you are no longer always comparing adjacent elements.

It is important to have all of your indentation correct always. This function now looks okay, but your whole program should always have correct indentation. Otherwise you're shooting yourself in the foot and detering would-be helpers from helping you. Replace all tabs with a fixed number of spaces before posting.

Also please don't use all capital letters for variable names. The common convention is that all uppercase symbols are for macros. "CamelCase" is more preferable.