Thread: Bubble sort, Help!!!!

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    10

    Question Bubble sort, Help!!!!

    Hi

    I have been stuck trying to work this but something is wrong.

    If any one can give me a hand i'd really appreciate it.
    Last edited by chema124; 01-24-2003 at 03:02 AM.

  2. #2
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807

    Salem...

    >for (i = 0, sorted = 1; i < n-1 ; i++)

    This can be write like this too, no?
    for (i=0, sorted=1; i < n ; ++i );

  3. #3
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807

    ...

    hmmm
    Code:
    #include <stdio.h>
    
    #define MAX 10 //array max size
    
    int main(void)
    {
    	int nArray[MAX] = {1,2,3,4,5,6,7,8,9,10};
    	int nI;
    
    	for(nI = 0; nI < MAX; ++nI)
    		printf("%d\n",nArray[nI]);
    
    	getchar(); //wait for the user to press a char
    	return 0;
    }
    This works fine.. he prints well all the elements of the array,
    same with nI++.

    About your comparation, the two programs printed until 9.
    Last edited by Vber; 01-21-2003 at 07:36 AM.

  4. #4
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807

    I still cant get it salem..

    for (i = 0, sorted = 1; i < n-1 ; i++)
    I dont think that is needed here n-1, using the < operator (not the <=) this will alone go until n-1, I tried looping an array of 10 elements with your code, and he counted until 8 (0 - 8). but he should go until 9 (0-9) so I did change the n-1 to i<n and this works fine..

  5. #5
    eafe
    Guest
    I think what is needed is too write a correct bubblesort. Bubblesort is a nested loop. You are counting form size of array down, and then swapping from 0 to the count. It is not a for in a while but a for in a for.

  6. #6
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807

    Ohh, now I get it Salem.

    Now you explained why, thanks.
    Because, in my book they don't write it like that

  7. #7
    Registered User
    Join Date
    Jan 2003
    Posts
    99
    I think you got this part of the code fixed but when getting input from the user that will go into an array, you do not use the address of operator (&). Since an array is a form of a pointer it is not needed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bubble sort not working... wats d prob?
    By Huskar in forum C Programming
    Replies: 8
    Last Post: 03-31-2009, 11:59 PM
  2. help with debug (bubble sort algorithm)
    By lbraglia in forum C Programming
    Replies: 5
    Last Post: 10-19-2007, 05:24 PM
  3. Bubble Sort... which type?
    By gflores in forum C++ Programming
    Replies: 8
    Last Post: 08-15-2004, 04:48 AM
  4. Bubble Sort, Qucik Sort
    By insomniak in forum C Programming
    Replies: 2
    Last Post: 03-15-2003, 04:54 PM
  5. optimizing bubble sort
    By Sargnagel in forum C Programming
    Replies: 14
    Last Post: 01-23-2003, 06:27 AM