0

I'm trying to sort an array made of random numbers from 1 to 10 in an ascending order. I've come up with this function:

void Sort(int a[10], int n)
{
    int j = 0;
    for (int i = 0; i < n-1; i++)
    {
        j = i+1;
        if (a[i] > a[j])
        {
            int aux = a[i];
            a[i] = a[j];
            a[j] = aux;
        }
    }
}

But when I try to output the array, the function doesn't seem to have worked:

Sort(array, 10);

    cout<<endl;

    for (int i = 0; i < 10; i++)
    {
        cout<<array[i]<<" ";
    }
5
  • I mean the array is not properly ordered Commented Apr 8, 2011 at 15:46
  • Is your goal to sort the array? Or is the goal to implement your own sort function? Commented Apr 8, 2011 at 15:48
  • As stated this can't possibly work (it simply doesn't do enough comparisons/swaps to be a credible sorting algorithm). Two questions: (1) Why are you implementing your own sort instead of using std::sort? (2) What was the algorithm that you had in mind when you wrote that code? Commented Apr 8, 2011 at 15:49
  • I'm trying to make my own sort function. The array is just made of random numbers ranging from 1 to and including 10. Commented Apr 8, 2011 at 15:50
  • Prefer using std::vector rather than array. The std::vector has high compliance with existing STL sorting and searching functions. Commented Apr 8, 2011 at 20:23

3 Answers 3

2

The algorithm in your Sort function is wrong. It doesn't sort at all.

Anyway, don't reinvent the wheel, better use std::sort as:

#include <algorithm>

std::sort(array, array+10);

As for your Sort function, which you want to implement using bubble-sort algorithm, possibly for learning purpose. the correct implementation is this:

void Sort(int *a, int n)
{
    for (int i = 0; i < n ; i++)
    {
       for (int j =  i + 1; j < n ; j++)
       {
           if (a[i] > a[j])
           {
              int aux = a[i];
              a[i] = a[j];
              a[j] = aux;
           }
       }
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

Looks better, but is there any specific advantage to using another for cycle for j instead of j=i+1?
@Kguy: j=i+1 is not enough. The larger numbers should bubble up from beginning to the end of the array, and for that you need to use a nested loop, so that bubbling can be done. After all, its bubble-sort algorithm. The nested loop tries to push the larger numbers to end of the array, and smaller number get shifted from end to the beginning!
@Kguy: I would suggest you to take an array of 4 or 5 integers, and follow your implementation and write down all the steps on white paper and see how things go. And also see the final array once the loop exit. Is that sorted?.. and then apply my implementation and write down all the steps on paper and see how things go. This approach is good for learning purpose.
Right, think I got it. The second loop goes through all the elements of the array to make sure the smallest one is set to the current i value, and then goes on to the next i value so the cycle repeats. Whereas my version compared the i value only with the next element, then moved on to the next i value.
@Kguy : The second loop goes through all the elements of the array to make sure the smallest one is set to the current i value .... Exactly!
|
1

You are only making n swaps. You need an outer loop on sort (assuming it's bubble sort) so that you continue doing that until you stop doing swaps.

bool Sort(int a[10], int n)
{
    bool swapped = false;
    int j = 0;
    for (int i = 0; i < n-1; i++)
    {
        j = i+1;
        if (a[i] > a[j])
        {
            int aux = a[i];
            a[i] = a[j];
            a[j] = aux;
            swapped = true;
        }
    }
    return swapped;
}

int main(int argc, char** argv) {
    int a[10] = {5,4,3,1,2,6,7,8,9,10};
    while (Sort(a,10));

    for (int i=0;i<10;++i) {
        std::cout << a[i] << std::endl;
    }
}

2 Comments

One way to do that would be to have a bool swapped;, or other variable name. Set swapped to true outside a while(swapped) loop. First thing in the while loop, set it to false. In the part where you swap entries, (and std::swap is cleaner here), set it to true. That way, you keep bubble sorting until there are no swaps, meaning that the list is sorted.
Wow, thanks a bunch guys. Been trying to get Bubble sort down for days. 3 Pages of unclear code. Bad textbook. Gotta get a new one.
0

That only does one pass over the data, here is an example showing you what happens

8 7 9 2 3 4 5

After going through your function the result would be

7 8 2 3 4 5 9

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.