Need help with Bubblesort

This is a discussion on Need help with Bubblesort within the C Programming forums, part of the General Programming Boards category; Can someone please help me with my bubblesort. It is not sorting and also prints 3 times. Code: #include <stdio.h> ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    47

    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");
    
    }}}}

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    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.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    47
    I changed and it is not sorting

  4. #4
    Registered User
    Join Date
    Sep 2009
    Posts
    47
    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

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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.
    Last edited by Adak; 11-13-2009 at 09:05 AM.

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by kburyanek View Post
    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.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User
    Join Date
    Sep 2009
    Posts
    47
    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

  8. #8
    Registered User
    Join Date
    Sep 2009
    Posts
    47
    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");
    
    }

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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.
    Last edited by Adak; 11-13-2009 at 12:19 PM.

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,269
    Quote Originally Posted by kburyanek View Post
    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.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem: Bubblesort - "strlen" in the function...
    By Petike in forum C Programming
    Replies: 15
    Last Post: 01-24-2008, 08:20 PM
  2. scanf in bubblesort
    By agentsmith in forum C Programming
    Replies: 3
    Last Post: 12-18-2007, 04:09 PM
  3. Circular bubblesort
    By PiCoMiKe in forum C Programming
    Replies: 2
    Last Post: 04-21-2006, 06:06 PM
  4. C++ bubblesort and structure help
    By ReignFire in forum C++ Programming
    Replies: 2
    Last Post: 10-17-2005, 02:25 AM
  5. bubblesort
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 10-02-2001, 10:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21