Thread: bubble sort with pointers not working

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    204

    bubble sort with pointers not working

    Hi. Here's my code:
    Code:
    #include <iostream>
    using namespace std;
    
    const int ELEMENTS = 4;
    
    int main()
    {
    	int num[4];
    	int n;
    	int i; 
    	int j;
    	int temp;
    	int *Ptr = NULL;
    	
    	for(n = 0; n < ELEMENTS; n++)
    	{
    		cout << "Type a number: ";
    		cin >> num[n];
    	}
    
    	Ptr = num;
    	for(i = 0; i < ELEMENTS; i++)
    
    		for(j = 0; j < ELEMENTS - 1; j++)
    			if(*(Ptr + i) < *((Ptr + j) - 1))
    			{
    				temp = num[j];
    				*(Ptr + j) = *((Ptr + j) - 1);
    				*((Ptr + j) -1)= temp;
    			}
    			for(i = 0; i < 4; i++)
    				cout << num[i] << endl;
    
    	return 0;
    }
    It compiles without errors but does not sort the array. What am I doing wrong? Thanks.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    2
    In every step, you are supposed to start in the opposite end of the array from where your "master" loops index starts and then work your way to what the index currently is. So in your case you should be sorting from the end to the beginning.

    Also, you are supposed to compare the element you are currently on with the next one according to the direction in which you are looping(which should be backwards in your case), not with the one situated farthest to the end / beginning.
    Code:
    #include <iostream>
    using namespace std;
    
    const int ELEMENTS = 4;
    
    int main()
    {
    	int num[4];
    	int n;
    	int i; 
    	int j;
    	int temp;
    	int *Ptr = NULL;
    	
    	for(n = 0; n < ELEMENTS; n++)
    	{
    		cout << "Type a number: ";
    		cin >> num[n];
    	}
    
    	Ptr = num;
    	for(i = 0; i < ELEMENTS; i++)
    
    		for(j = ELEMENTS - 1; j > i; j--)
    			if(*(Ptr + j) < *((Ptr + j) - 1))
    			{
    				temp = num[j];
    				*(Ptr + j) = *((Ptr + j) - 1);
    				*((Ptr + j) -1)= temp;
    			}
    	for(i = 0; i < 4; i++)
    		cout << num[i] << endl;
    
    	return 0;
    }
    That sorts the array just fine, but for consistency you might want to change
    Code:
    temp = num[j];
    to
    Code:
    temp = *(Ptr + j);
    And the indentation is a bit wrong in the end, it should be like in the above code.

    edit: pianorain's find
    Last edited by leafybug; 05-18-2005 at 10:40 AM.

  3. #3
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Code:
    const int ELEMENTS = 4;
    
    int main()
    {
        int num[4];
        <snip>
    
        for(j = ELEMENTS; j > i; j--)
            if(*(Ptr + j) < *((Ptr + j) - 1))
            {
                temp = num[j];
                <snip>
            }
    }
    See a problem here? I'll give you a hint...num[4] doesn't exist.

    Why are you using an extra pointer to begin with? It doesn't add any functionality. The whole program can be rewritten without it. Also, just to keep your array bounds consistent, you might want to declare your integer array as num[ELEMENTS].
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    2
    Yeah, sorry about that. I've edited it to fix the error.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using bubble sort to sort a matrix by sum..
    By transgalactic2 in forum C Programming
    Replies: 22
    Last Post: 12-23-2008, 12:03 AM
  2. Psuedo random numbers in a bubble sort.
    By Sir Andus in forum C++ Programming
    Replies: 10
    Last Post: 04-09-2007, 08:25 PM
  3. bubble sort help.
    By zeromx in forum C Programming
    Replies: 9
    Last Post: 10-30-2006, 07:37 AM
  4. Bubble Sort, Qucik Sort
    By insomniak in forum C Programming
    Replies: 2
    Last Post: 03-15-2003, 04:54 PM
  5. bubble sort
    By lambs4 in forum C Programming
    Replies: 3
    Last Post: 06-03-2002, 05:46 PM