0

I run gdb with the following commands: gdb ./find ; break sort ; run 42 < numbers.txt ; print values[0]@n. However, the numbers remain unsorted. Can someone help me?

void sort(int values[], int n)
{

bool swaps;

 do
{

  swaps = false;
for (int i = 0; i < n-1; i++)
 {
   if (values[i] > values[i+1])
    {
     int tmp = values[i];
     values [i] = values[i+1];
     values[i+1] = tmp;
     swaps = true;
    }

   }
 }
 while (swaps);
 return;
}
1

You have a single for loop in your bubble sort. This will have the effect of moving only the single largest element to the end of the list, but it will stop there. The second and third largest elements (and so on) may get moved, but won't necessarily get fully sorted. You need a second for loop surrounding it to repeat the process over and over until you have sorted the list completely. This is an n*n sort. For a more detailed explanation, you should review the class material on the subject of bubble sorts.

If this answers your question, please click on the check mark to accept. Let's keep up on forum housekeeping. ;-)

1
  • Cheers ! I'm on in right now.
    – Gabriel
    Nov 20 '15 at 19:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .