bubble sorting a 2-d array

This is a discussion on bubble sorting a 2-d array within the C Programming forums, part of the General Programming Boards category; I am having problems with bubble sorting a 2d array. I know I need 3 loops but I do not ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Location
    unf
    Posts
    16

    bubble sorting a 2-d array

    I am having problems with bubble sorting a 2d array. I know I need 3 loops but I do not know where to put the third. any help would be greatly appreciated.

    Code:
    void bubble(int arr[])
    {
       int i, j;
    
       for (i = 0; i < SIZE - 1; i++)
       {
          for (j = SIZE - 1; j > i; j--)
             if (arr[j - 1] > arr[j])
                swap(&arr[j - 1], &arr[j]);
          printf("\nAfter pass %d: ", i);
          printArr(arr);
       }
    }
    
    void swap(int *pNum1, int *pNum2)
    {
       int temp;   // NOT int *
       temp = *pNum1;
       *pNum1 = *pNum2;
       *pNum2 = temp;
    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    181
    u dont need three loops, its just the 2 loop, with 2 two two an check the elements and swap it, look at the following code which could help you

    Code:
    /*program to arrange the array elements in increasing order*/
    #include<stdio.h>
    
    void swap(int*, int *);  // fucntion to sawp the elements
    
    int main()
    {
            int a[10];
            int i,j;
            
            for(i=0;i<10;i++)
                    scanf("%d",&a[i]);
                   
            for(i=0;i<10-1;i++)
            {
                for(j=i+1;j<10;j++)
                {
                    if(a[i]>a[j]) // check two elements of the array is i > j swap it 
                    swap(&a[i],&a[j]);
            }
       }
       
            for(i=0;i<10;i++)
            printf("%d\t",a[i]);        
            
            getchar();
    }
    
    void swap(int *num1, int *num2)  // fucntion to swap the elements 
    {
        *num1^=*num2;
        *num2^=*num1;
        *num1^=*num2;
    }
    s.s.harish

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    void swap(int *num1, int *num2)  // fucntion to swap the elements 
    {
        *num1^=*num2;
        *num2^=*num1;
        *num1^=*num2;
    }
    Don't do this. It's a stupid way to swap. Just use a temporary variable. Search the forums if you want to know why it's wrong to do.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Quote Originally Posted by quzah
    Don't do this. It's a stupid way to swap. Just use a temporary variable. Search the forums if you want to know why it's wrong to do.

    Quzah.
    Or I can just tell you, because this is one of those screwups that I never get tired of pointing out. Try the following main with your swap code. Then rewrite your swap with a temp variable and see which one really works.
    Code:
    int main(void)
    {
    	int a = 10;
    	int* b = &a;
    	printf("Before swap: a:%d b:%d\n",a,*b);
    	swap(&a,b);
    	printf("After swap: a:%d b:%d\n",a,*b);
    	return 0;
    }
    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

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    181
    fine i get the result as 0 for both a and b

    is that b'cose it don't work for msomehting like this

    swap(&a, &a);

    when i do this it wont work as expected.
    is that my thinking is correct, if not so please correct me

    thank u very much

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by stodd04
    I am having problems with bubble sorting a 2d array. I know I need 3 loops but I do not know where to put the third. any help would be greatly appreciated.
    Could you elaborate on your expected input and output?
    Code:
       int array[][3] =
       {
          {6,5,4},
          {7,9,8},
          {2,1,3},
       };
    Would you expect this to be sorted into this?
    Code:
       {
          {1,2,3},
          {4,5,6},
          {7,8,9},
       }
    How are you evaluating less than, greater than, and equal to for each "row"? And do you intend to also sort within each "row"?

    Or is the "2D array" really an array of strings?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 08:39 AM
  2. Bubble Sorting
    By yukapuka in forum C++ Programming
    Replies: 7
    Last Post: 06-13-2008, 10:44 PM
  3. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 01:04 PM
  5. sorting a randomly generated dynamic array
    By Led Zeppelin in forum C Programming
    Replies: 4
    Last Post: 05-03-2002, 10:10 AM

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