Bubble sort two dimensional array

Ive been up all night trying to figure this out, it probably looks simple to most of you >.<

1
2
3
4
5
6
7
8
9
10
11
12
13
void bubble(int oneChoice[][COLLUMNone],int i){
     int temp;
     cout<<oneChoice[0][3]<<endl;
     for(int h=0;h<=i;h++){     
         if(oneChoice[h][3]>oneChoice[h+1][3]){
             for(int y=0;y<=3;y++){
                temp=oneChoice[h][y];
                oneChoice[h][y]=oneChoice[h+1][y];
                oneChoice[h+1][y]=temp;
             }
         }
     }
}   


im trying to sort the rows in ascending order based on the value of the 4th column.
*update
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void bubble(int oneChoice[][COLUMNone],int i){
     int temp,limit;
     limit=i-1;
     
     for(;limit>0;limit--){
        for(int h=0;h<=i;h++){     
            if(oneChoice[h][3]>oneChoice[h+1][3]){
                for(int y=0;y<=3;y++){
                   temp=oneChoice[h][y];
                   oneChoice[h][y]=oneChoice[h+1][y];
                   oneChoice[h+1][y]=temp;
                }
            }
        }
     }


for some reason it skips sets two rows to equal to zero but the rest of the array sorts fine??? so confused.
This implementation of bubble-sort is very unefficient because, it doesn't analyze the string which will be sorted so, it will do n^n operations and that means a complexity of O(n^n).
An efficient implementation of bubble-sort it's below:
1
2
3
4
5
6
7
8
9
10
11
//headers, namespaces
do{   found=0;
         for(int i=1; i<=n; i++)
        { if(array[i]>array[i+1])
           { aux=array[i];
              array[i]=array[i+1];
              array[i+1]=aux;
          }
          found=1;
        }
     }while(found);

Look at this and modify it the way you need.
Yeah I realize bubble sort is inefficient but its for an assignment. Can you show me an example of a two-dimensional array using bubble sort?
You can do it in an understandable way, like that:
Read the two-dimensional array:
1
2
3
4
5
6
for(i=1; i<=n; i++)
 {for(j=1; j<=m; j++)
   {   cout<<"v["<<i<<"]["<<j<<"]=";
        cin>>v[i][j];
   }
}

Now, you have to place the two-dimensional array into an one-dimensional array (into a vector) like that:
1
2
3
4
5
6
7
8
int x=1;
for(i=1; i<=n; i++)
{   for(j=1; j<=m; j++)
     {     k[x]=v[i][j];
            x++;
     }
}
x--;

Now you have the two-dimensional array into an one-dimensional one. You can now apply the bubble-sort implementation from my last post for that array (k).
Next, you have to put the sorted vector in the original array (v), like that:
1
2
3
4
5
6
7
x=1;
for(i=1; i<=n; i++)
{   for(j=1; j<=m; j++)
     {     v[i][j]=k[x];
            x++;
     }
}

This is an unefficient way to do that because I use 6 loops just for that sorting and overall I think that will be about 8 (including the printing on the screen).
Also, you can do it using just one traverse but it's a bit complicated because you'll have about 3 for-loops in a do-while loop, and that can damage your system.
Topic archived. No new replies allowed.