×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

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!

*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

sort function for array elements

sort function for array elements

sort function for array elements

(OP)
Hi I'm trying to sort an array's elements into sequential order.  When I run my program I am able to read the input from my input file, and then store that input in my array, but I can't get it to sort properly. I've pasted my sort function below.  if you have any ideas please let me know.  Thanks!

void sort (int arr[], int size, int& num_used)
{
    bool sorted = false;
   while (sorted == false)
   {
       sorted = true;
      for (int index = 0; index < size - 1 && index < num_used - 1; index++)
      {
          if (arr[index] > arr[index+1])
         {
             int tmp = arr[index];
            arr[index] = arr[index+1];
            arr[index+1] = tmp;
            sorted = false;
         }//end if
      }//end for
   }//end while
}//end sort func.  

RE: sort function for array elements

I haven't looked at the entire program cautiously, but one thing instantly caught my eye.

Inside the 'for loop', why are you stating the while condition as  "index < arr.size() - 1". Shouldn't it be "index <= arr.size() - 1". From your while condition, the procedure will not check the last element. Imagine 20 array elements. Your statment says, index < 19. You forget to compare the last two elements. Another valid statement is "index < array.size()"
And even after you corrected it, your "sorted" variable will still read false because of where you have put it. You might want to work on the program again because I am not quite sure if 'tis right.
I am working on a proj. of my own, so I don't have the time to look. Im sure someone else might help.

Narayanan Srinivasan
coolie91@hotmail.com
 
Brothers and Sisters from India, if you have any recent news regarding F-1 to H-1B visa transfer, please e-mail me. Thanks.

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