-2

Possible Duplicate:
Sorting in arrays

int A[5]={1,5,3,2,4};
for(int i=0;i<5;i++){
    for(int j=0;j<5;j++){
        if(A[j]>A[j+1])
        {
           int t=A[j];
           A[j]=A[j+1];
           A[j+1]=t;
        } 
    }
}
for(i=0;i<5;i++)
   cout<<A[i];

The desired output must be 1,2,3,4,5 but my output is 0,1,2,3,4. What's the problem in my code?

6
  • 3
    I've reformatted to make your code a bit more readable, I hope what I've done is OK? Commented Sep 26, 2010 at 10:33
  • 4
    That can't be the complete code as you can't have non-declaration statements outside of a function body. Commented Sep 26, 2010 at 10:35
  • This is exactly the same question as stackoverflow.com/questions/3794856/sorting-in-arrays Commented Sep 26, 2010 at 10:43
  • Just for fun, have you actually executed the code you provide. It does actually output 12345. :) Commented Sep 26, 2010 at 10:48
  • 2
    @bjarkef this code triggers undefined behavior, so it may output 12345 if the value at A[5] is greater/equal to 5. Commented Sep 26, 2010 at 11:09

5 Answers 5

3

Firstly, learn to indent properly. It isn't hard. Secondly, it looks like your making a sorting function.

The problem is that you are using the variable j for both for for loops. Don't. Change one to k, or another variable. I assume you are trying to reference the second j with j+1. This just adds 1 to j.

May I ask, do you have any coding experience?

And you should have specified what the problem was with your code, what you expected and what you got.

Sign up to request clarification or add additional context in comments.

3 Comments

i have used i and j for the loops and not j for both the for loops
A[j+1] refers to the next element and not add 1 to that element
and this code is sorting an array in increasing order so i am swapping two elements at a time depending upon the if statement and to check every element in a sequential manner i have used that i loop
2

Looks like you are attempting Bubble sort, why don't you read up on it . I'm sure you can get it working yourself.

Comments

0

You can use Sort instead, works great!

int A[5]={1,5,3,2,4};
vector<int> vec(A, A+5);

sort(vec.begin(), vec.end()); 

3 Comments

I think this is cheat as the author seems to learn how to sort himself :)
i have just asked what is the problem in my code and whay it is not giving the desired output don't think negative Benoit
it's not cheating unless there are rules stating he is not allowed to use it. :) And it is the easiest way I would think.. Some wheels don't need reinventing
0

You need to limit your inner loop to <4:

int A[5]={1,5,3,2,4};
for(int i=0;i<5;i++){
    for(int j=0;j<4;j++){
        if(A[j]>A[j+1])
        {
           int t=A[j];
           A[j]=A[j+1];
           A[j+1]=t;
        } 
    }
}
for(i=0;i<5;i++)
   cout<<A[i];

2 Comments

after changing j with 4 the output is 2 3 4 5 0
Please update your question with the new code. Did you copy and paste my code?
0

Your program will not produce that output. It will die at A[j+1] when j == 4. In the last loop you need to write for(int i = 0; i < 5; i++). Secondly, your logic doesn't have much sense. If you want to sort the array this is the code:

for(int i = 0; i < 5; i++){
    for(int j = i + 1; j < 5; j++){
        if(A[j] < A[i])
        {
            int t = A[j];
            A[j] = A[i];
            A[i] = t;
        } 
    }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.