Thread: Simple Sort Question

  1. #1
    Registered User
    Join Date
    Feb 2005
    Location
    Manhattan, KS
    Posts
    3

    Simple Sort Question

    I'm attempting to sort a pointer array using a bubbleSort (yes I know how inefficient it is but for the purpose of this program it'll work just fine) and I'm having trouble getting the sort to iterate through my pointer array. This program requires that no array notation be used i.e. '[ ]' brackets, just pointer arithmetic. The user also defines the size of the pointer array with a command line argument ( 'a.out 5' where 5 would be the array size). I believe the problem in my sort lies in the if() statement but I'm unsure on how to fix it. Can anyone help?

    The program's fairly unrefined at this point so sorry for the mess:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    
    
    void bubbleSort(int *p, int array_size);
    
    int main(int argc, char **argv)
    {
    
        int i, j, q;
    	int *p, *b;
    	++argv;
    	q=atoi(*argv);
    	
    
    	p= calloc(q, sizeof(int));
    	b=p;
    	
    	srand( (unsigned)time( NULL ) );
    	
    	if (q>0){
    	printf("Unsorted:\n");
        	for (i = 0; i < q; i++) {
            	*b = (rand()%100);
            	printf("%d\n",*b);
    			b++;
        		}
    	}
    	else {
    		printf("Invalid Entry\n");
    		return 0;
    	}
    	
    	bubbleSort(b, q);
    	
    	for (i = 0; i < q; i++)
    	{
        		printf("%i\n", *p);
    	}
    	
    
    	free(b);
    }
    
    
    void bubbleSort(int *p, int array_size)
    {
      int i, j, temp;
    
      for (i = (array_size - 1); i >= 0; i--)
      {
        for (j = 1; j <= i; j++)
        {
          if (*p-- > *p)
          {
            temp = *p--;
            *p-- = *p;
            *p = temp;
          }
        }
      }
    }

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    void bubbleSort(int *p, int array_size)
    {
      int i, j, temp;
    
      for (i = (array_size - 1); i > 0; i--)
      {
        for (j = 0; j < i; j++)
        {
          if (*(p+j) > *(p+j+1))
          {
            temp = *(p+j);
            *(p+j) = *(p+j+1);
            *(p+j+1) = temp;
          }
        }
      }
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Code:
    	int i, j, q;
    	int *p, *b;
    It might help if you used some slightly more descriptive names. It can get rather tedious trying to tell apart all those p's,q's, and b's.
    Code:
    	++argv;
    	q=atoi(*argv);
    This just looks like a bad idea. What if there isn't an argument? And why increment argv? Try something like this:
    Code:
    	if(argc < 2)
    		return 1;
    	q=atoi(*(argv+1));
    Code:
    	for (i = 0; i < q; i++)
    	{
        		printf("%i\n", *p);
    	}
    I bet this doesn't work like you're hoping. Hint: make the same change that hk_mp5kpdw did to your bubbleSort.
    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
    Feb 2005
    Location
    Manhattan, KS
    Posts
    3

    printing sorted array

    I think that the changes hk suggested work, however, when I attempt to print out the sorted array it still only displays the first value of the array. For example

    camaro$ a.out 5
    Unsorted:
    12
    58
    8
    1
    90
    12
    12
    12
    12
    12

    where the last 5 digits are where the array is supposed to print sorted.

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    for (i = 0; i < q; i++)
    {
        printf("%i\n", *(p+i));
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    184
    hope this code helps a bit

    Code:
    void bubblesort(int *p, int array_size)
    {
    int i, j, temp;
        
        for(i=0;i<10-1;i++)
        {
                for(j=i+1;j<10;j++)
                {
                            if(*(p+i) > *(p+j))
                            {
                                temp=*(p+i);
                                *(p+i)=*(p+j);
                                *(p+j)=temp;
                        }
             }
    }    
                     
    }

  7. #7
    Registered User
    Join Date
    Feb 2005
    Location
    Manhattan, KS
    Posts
    3

    It works

    Through some slight Divine intervention (a.k.a my astute girlfriend) and the help of you guys, it works.

    Thanks
    Peace.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question regarding variables
    By Flakster in forum C++ Programming
    Replies: 10
    Last Post: 05-18-2005, 08:10 PM
  2. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2005, 11:41 PM
  3. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 09:46 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 06:58 PM