×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Bubble sorting a 2D array by a column.

Bubble sorting a 2D array by a column.

Bubble sorting a 2D array by a column.

(OP)
I have a  2D array that I need bubble sorted according to the last column, and I can't seem to get it right. Here's what i have so far:



CODE

int bubble(int twoarray[][4], int limit){
    int temp[4], index;
    for (;limit>0;limit--){
        for (index=0;index<limit;index++){
             for (int i =0;i<4;i++ ){
                 if (twoarray[index][3]>twoarray[index+1][3]){
                 for (i=0;i<4;i++){
                 temp[i]=twoarray[index][i];
                 }
                 for (i=0;i<4;i++){
                 twoarray[index+1][i]=twoarray[index][i];
                 }
                 for (i=0;i<4;i++){
                 twoarray[index+1][i]=temp[i];
                 }

Why is this not working?

RE: Bubble sorting a 2D array by a column.

Think about the last round of i-loops (when i is eq 3) and i+1 indicies in these codes. Probably, you have the same problem with index-loops...

RE: Bubble sorting a 2D array by a column.

(OP)
Argh! I even worked this out on paper, but it is not sorting!

CODE

int bubble(int twoarray[][4], int total){
    int temp[4];
      for (int index=0;index<total-1;index++ ){
        if (twoarray[index][3]>twoarray[index+1][3]){
           for (int i=0;i<4;i++){
               temp[i]=twoarray[index][i];
               twoarray[index][i]=twoarray[index+1][i];
               twoarray[index+1][i]=temp[i];
           }
        }
      }

RE: Bubble sorting a 2D array by a column.

Well, you have one pass of bubble sorting algorithm.
Only one (largest) element goes into its room.
Invent (outer) loop to proceed sorting (or see your text-book more carefully;)...

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close